Name Vedant Mane
UID no. 2024800067
Experiment No. 8
AIM: To Apply the concepts of structures/union to solve a given problem.
Program 1
PROBLEM A men’s sports club keeps elaborate computerized records of all its
STATEMENT : members. The records contain typical information such as age, address, etc.
of each person. But there is also information about whether a member is an
active playing member, about whether he is married, and so on; if he is
married the record contains information about his wife’s name, the no. of
children and their names. Write a program which demonstrates how such a
system might be implemented. Show how the names of the wives of all
active playing members might be printed.
ALGORITHM: -----------------------------------------------------------------------------------------
FLOWCHART: -----------------------------------------------------------------------------------------
PROGRAM: #include <stdio.h>
#include <string.h>
int main() {
int i,a;
printf("Dhanesh's Men Sporting Club\n");
printf("\nEnter number of members\n");
scanf("%d",&a);
// Define structs
struct sport {
char name[100];
int age;
int height;
int x; // Active status
int n; // Marital status
};
struct wife {
char name1[100];
int n1; // Has children
};
struct child {
int n2; // Number of children
char name2[100][100]; // Names of children
};
struct sport s[a]; // Array of 5 sport members
struct wife w[a]; // Array of wives for each member
struct child c[a]; // Array of children for each member
// Loop to enter details for each member
for (i = 0; i < a; i++) {
printf("\n%d.\n", i + 1);
printf("\nEnter name: ");
scanf(" %[^\n]", s[i].name); // Read string with spaces
printf("\nEnter age: ");
scanf("%d", &s[i].age);
printf("\nEnter height: ");
scanf("%d", &s[i].height);
// Handle active status with switch-case
printf("\nEnter the index number that represents your presence in the
Club\n1.Active\n2.Not active\n");
scanf("%d", &s[i].x);
switch (s[i].x) {
case 1:
printf("Status: Active\n");
break;
case 2:
printf("Status: Not active\n");
break;
default:
printf("Invalid input\n");
}
// Handle marital status with switch-case
printf("\nEnter the index number that represents your marital
status\n1.Married\n2.Unmarried\n");
scanf("%d", &s[i].n);
switch (s[i].n) {
case 1:
printf("Marital status: Married\n");
break;
case 2:
printf("Marital status: Unmarried\n");
break;
default:
printf("Invalid input\n");
}
if (s[i].n == 1) { // Married
printf("\nEnter your wife's name: ");
scanf(" %[^\n]", w[i].name1); // Read wife name
// Handle children with switch-case
printf("\nEnter the index number that represents if you have any
children\n1.Yes\n2.No\n");
scanf("%d", &w[i].n1);
switch (w[i].n1) {
case 1:
printf("You have children.\n");
printf("\nEnter number of children: ");
scanf("%d", &c[i].n2); // Number of children
for (int j = 0; j < c[i].n2; j++) {
printf("\nEnter the name of child %d: ", j + 1);
scanf(" %[^\n]", c[i].name2[j]); // Read child's name
}
break;
case 2:
printf("You have no children.\n");
break;
default:
printf("Invalid input\n");
}
}
printf("\nThank you for giving your information!\n");
}
// Optionally, print all data entered for verification
for (i = 0; i < a; i++) {
printf("\nMember %d\n", i + 1);
printf("Name: %s\n", s[i].name);
printf("Age: %d\n", s[i].age);
printf("Height: %d\n", s[i].height);
// Print active status
printf("Active status: ");
switch (s[i].x) {
case 1:
printf("Active\n");
break;
case 2:
printf("Not active\n");
break;
default:
printf("Invalid input\n");
}
// Print marital status
printf("Marital status: ");
switch (s[i].n) {
case 1:
printf("Married\n");
break;
case 2:
printf("Unmarried\n");
break;
default:
printf("Invalid input\n");
}
if (s[i].n == 1) { // If married
printf("Wife's name: %s\n", w[i].name1);
if (w[i].n1 == 1) { // If has children
printf("Number of children: %d\n", c[i].n2);
for (int j = 0; j < c[i].n2; j++) {
printf("Child %d: %s\n", j + 1, c[i].name2[j]);
}
} else {
printf("No children.\n");
}
}
}
return 0;
}
RESULT:
Program 2
PROBLEM An airline reservation system maintains records for possible flights
STATEMENT : consisting of
STARTING POINT 3 character code
DESTINATION 3 character code
STARTING TIME integer on scale 0001 – 2400
ARRIVAL TIME integer on scale 0001 – 2400
SEATS positive integer in suitable range.
Your program is to read 20 such records followed by queries of the form
STARTING
POINT– DESTINATION, one to a line. For each query, find whether there
is a possible flight with a seat available; if so, reduce the number of seats
by one and print out the flight details (or an apology).
ALGORITHM: -------------------------------------------------------------------------------------------
FLOWCHART: -------------------------------------------------------------------------------------------
PROGRAM: #include <stdio.h>
#include <string.h>
int main()
{
int i, a, j, k;
printf("Welcome to Airline Reservation System of Nachiket
Airways\n");
struct records
{
char start[4];
char end[4];
char starttime[5];
char endtime[5];
unsigned int seats;
};
struct queries
{
char start[4];
char end[4];
};
struct records r[20] =
{
{"DEL", "BOM", "1000", "1200", 30},
{"DEL", "BLR", "1100", "1300", 25},
{"DEL", "CCU", "1200", "1400", 20},
{"BOM", "DEL", "1300", "1500", 15},
{"BOM", "BLR", "1400", "1600", 40},
{"BOM", "CCU", "1500", "1700", 10},
{"BLR", "DEL", "1600", "1800", 35},
{"BLR", "BOM", "1700", "1900", 50},
{"BLR", "CCU", "1800", "2000", 45},
{"CCU", "DEL", "1900", "2100", 20},
{"CCU", "BOM", "2000", "2200", 30},
{"CCU", "BLR", "2100", "2300", 25},
{"DEL", "BOM", "0600", "0800", 60},
{"DEL", "BLR", "0700", "0900", 40},
{"DEL", "CCU", "0800", "1000", 30},
{"BOM", "DEL", "0900", "1100", 50},
{"BOM", "BLR", "1000", "1200", 20},
{"BOM", "CCU", "1100", "1300", 15},
{"BLR", "DEL", "1200", "1400", 25},
{"BLR", "COK", "1200", "1430", 0}
};
struct queries q[5];
printf("\n\n");
printf("NO\tSTR\tDES\tDEPT\tARVL\tSEATS\n");
for (i = 0; i < 20; i++)
{
printf("%d\t%s\t%s\t%s\t%s\t%u\n", i + 1, r[i].start, r[i].end,
r[i].starttime, r[i].endtime, r[i].seats);
}
printf("\nQueries\n");
for (i = 0; i < 5; i++) // Loop should run for 5 queries, as q[5] array is
defined
{
printf("Query No. %d\n\n ", i + 1);
printf("Please enter your queries in the form STARTING POINT-
DESTINATION\n");
scanf("%s", q[i].start);
printf("-");
scanf("%s", q[i].end);
for (j = 0; j < 20; j++) // Loop through 20 flight records
{
if (strcmp(q[i].start, r[j].start) == 0 && strcmp(q[i].end, r[j].end) ==
0)
{
printf("\nFlight for your query found\n");
if (r[j].seats != 0)
{
printf("\nEnter number of seats you want\n");
scanf("%d", &k);
if (r[j].seats >= k)
{
printf("Seats available and booked\n");
r[j].seats -= k;
break;
}
else
{
printf("Sadly there are not enough seats\n");
break;
}
}
else
{
printf("\nSadly the seats are unavailable\n");
}
}
}
printf("\nThank you for using our services\n\n\n");
}
return 0;
}
RESULT:
Program 3
PROBLEM A league table consists of a set of N records each representing the
STATEMENT: performance of a team. A record contains team name, no. of games played,
no. of games won, no. of games drawn, no. of games lost, no. of goals
scored and no. of points awarded (2 for a win and 1 for a draw). Write a
program which inputs a positive integer N, N records of the form above, a
positive integer M, the results of M games in the form, team1 goals scored
team2 goals scored. Based on the results of these M games, the program
should update the records and display the updated records.
ALGORITHM: -------------------------------------------------------------------------------------------
FLOWCHART: -------------------------------------------------------------------------------------------
PROGRAM: #include<stdio.h>
#include<string.h>
int main()
{
struct league
{
char team_name[100];
int num_games_played;
int num_games_won;
int num_games_draw;
int num_games_lost;
int num_goals;
int points;
};
int n;
printf("Enter the number of records to be taken:\n");
scanf("%d",&n);
getchar();
struct league details[n];
for(int i=0;i<n;i++)
{
printf("\n%d.\n", i + 1);
printf("Enter the name of the team:\n");
scanf("%s",details[i].team_name) ;
getchar();
printf("Enter no. of games played:\n");
scanf("%d",&details[i].num_games_played);
getchar();
printf("Enter no. of games won:\n");
scanf("%d",&details[i].num_games_won);
getchar();
printf("Enter no. of games drawn:\n");
scanf("%d",&details[i].num_games_draw);
getchar();
// Calculate games lost based on the number of games played, won, and
drawn
details[i].num_games_lost = details[i].num_games_played -
details[i].num_games_won - details[i].num_games_draw;
// Calculate points based on the number of games won and drawn
details[i].points = (details[i].num_games_won * 2) +
(details[i].num_games_draw * 1);
printf("Enter no. of goals scored:\n");
scanf("%d",&details[i].num_goals);
getchar();
}
int m;
printf("Enter the no. of games;\n");
scanf("%d",&m);
for(int i=0;i<m;i++)
{
char team1[100],team2[100];
int team1_goals,team2_goals;
printf("Enter the result of the game %d (team1 ,goal scored ,team2
goals scored)",i);
scanf("%s%d%s%d",team1,&team1_goals,team2,&team2_goals);
int idx1=-1,idx2=-1;
for(int j=0;j<n;j++)
{
if(strcmp(details[j].team_name,team1)==0)
{
idx1=j;
}
if(strcmp(details[j].team_name,team2)==0)
{
idx2=j;
}
}
if(idx1!=-1 && idx2!=-1)
{
details[idx1].num_games_played++;
details[idx2].num_games_played++;
details[idx1].num_goals+=team1_goals;
details[idx2].num_goals+=team2_goals;
if(team1_goals>team2_goals)
{
details[idx1].num_games_won++;
details[idx2].num_games_lost++;
details[idx1].points += 2;
}
else if(team1_goals<team2_goals)
{
details[idx2].num_games_won++;
details[idx1].num_games_lost++;
details[idx2].points += 2;
}
else
{
details[idx1].num_games_draw++;
details[idx2].num_games_draw++;
details[idx1].points += 1;
details[idx2].points += 1;
}
}
else
{
printf("Teams are not found \n");
}
}
printf("\nUpdated League Table:\n");
printf("----------------------------------------------------------\n");
printf("| Team Name | Games Played | Games Won | Games Drawn |
Games Lost | Goals Scored | Points |\n");
printf("----------------------------------------------------------\n");
for (int i = 0; i < n; i++)
{
printf("| %-10s | %-12d | %-9d | %-12d | %-10d | %-12d | %-6d
|\n",details[i].team_name,details[i].num_games_played,details[i].num_gam
es_won,details[i].num_games_draw,details[i].num_games_lost,details[i].nu
m_goals,details[i].points);
}
printf("----------------------------------------------------------\n");
return 0;
}
RESULT:
Program 4
PROBLEM A record in an organisation’s payroll consists of one line for each employee
STATEMENT: consisting of:
NAME (20 characters), GENDER (1 character M or F), SALARY
(integer), DATE OF BIRTH (3 integers YEAR MONTH DAY).Write a
program which will input 10 such records. Your program must then take in
5amendments in the record set which will be in the same form as the record
structure itself. The amendments can contain new employees to be added
(name different from existing ones), employees left (salary given as 0) and
update of salary(more or less). Your program must then incorporate these
amendments and also remove those employees who have reached
retirement age(Age 60).
ALGORITHM: -------------------------------------------------------------------------------------------
FLOWCHART: -------------------------------------------------------------------------------------------
PROGRAM : #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
printf("\nNachiket Group of Companies\n\nEmployee Database\n");
struct emplo {
char name[21];
char gen;
unsigned long long int sal;
unsigned int date;
unsigned int mon;
unsigned int yr;
unsigned int age;
};
struct emplo e[15];
int i, j, k, a, b, t, c, d;
// Input initial employee data
for (i = 0; i < 10; i++) {
printf("\n%d.\n", i + 1);
printf("\nEmployee name (20 character limit)\n");
getchar(); // To clear the buffer
scanf("%[^\n]", e[i].name);
printf("\nEnter gender (M for Male & F for Female)\n");
getchar(); // To clear the buffer
scanf("%c", &e[i].gen);
printf("\nEnter Salary\n");
scanf("%llu", &e[i].sal);
printf("\nEnter DOB\nDate:");
scanf("%u", &e[i].date);
printf("\nMonth:");
scanf("%u", &e[i].mon);
printf("\nYear:");
scanf("%u", &e[i].yr);
}
char xname[21], sname[21];
printf("\nAmendment section\n");
for (j = 0; j < 5; j++) {
printf("\n%d.\n", j + 1);
printf("\nEnter the index number that represents the Amendment
type\n");
printf("\n1. Add new employee\n2. Employee left\n3. Update
salary\n");
scanf("%d", &k);
switch (k) {
case 1:
i++;
printf("\nEmployee name (20 character limit)\n");
getchar();
scanf("%[^\n]", e[i].name);
printf("\nEnter gender (M for Male & F for Female)\n");
getchar();
scanf("%c", &e[i].gen);
printf("\nEnter Salary\n");
scanf("%llu", &e[i].sal);
printf("\nEnter DOB\nDate:");
scanf("%u", &e[i].date);
printf("\nMonth:");
scanf("%u", &e[i].mon);
printf("\nYear:");
scanf("%u", &e[i].yr);
break;
case 2:
printf("\nEnter the name of the employee that left the
company\n");
getchar();
scanf("%[^\n]", xname);
for (b = 0; b <= i; b++) {
t = 0;
if (strcmp(xname, e[b].name) == 0) {
printf("\nEmployee found\n");
e[b].sal = 0;
printf("\nEmployee removed successfully\n");
break;
} else {
t++;
}
}
if (t == i + 1) {
printf("\nEmployee not found\n");
}
break;
case 3:
printf("\nEnter the name of the employee to update salary\n");
getchar();
scanf("%[^\n]", sname);
for (c = 0; c <= i; c++) {
t = 0;
if (strcmp(sname, e[c].name) == 0) {
printf("\nEmployee found\n");
printf("\nPrevious salary of the employee: %llu\n", e[c].sal);
printf("\nNew salary of the employee: ");
scanf("%llu", &e[c].sal);
printf("\nSalary updated\n");
break;
} else {
t++;
}
}
if (t == i + 1) {
printf("\nEmployee not found\n");
}
break;
default:
printf("\nEnter valid Index number\n");
break;
}
printf("\n*****************************************************
************************\n");
}
// Age calculation and retiree check
for (d = 0; d <= i; d++) {
e[d].age = 2024 - e[d].yr;
if (12 < e[d].mon || (12 == e[d].mon && 20 < e[d].date))
e[d].age--;
if (e[d].age > 60) {
printf("\n%s has retired and hence deducted from the database\n",
e[d].name);
e[d].sal = 0;
}
}
// Final Employee List Output in Tabular Format
printf("\nFinal Employee List\n");
printf("| %-20s | %-10s | %-15s | %-12s | %-5s |\n", "Name", "Gender",
"Salary", "DOB (DD-MM-YYYY)", "Age");
printf("------------------------------------------------------------------------\n");
for (int v = 0; v <= i; v++) {
printf("| %-20s | %-10c | %-15llu | %-2u-%2u-%4u | %-5u |\n",
e[v].name, e[v].gen, e[v].sal, e[v].date, e[v].mon, e[v].yr,
e[v].age);
}
return 0;
}
RESULT:
Program 5
PROBLEM Create a structure student with data member as name, Roll No. & KT. Accept
STATEMENT: and Display details for five students and display the details of Students with
Highest Number of KTs.
ALGORITHM: -------------------------------------------------------------------------------------------
FLOWCHART: -------------------------------------------------------------------------------------------
PROGRAM: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
int i, j, k;
unsigned int max;
printf("\nRandom Engineering College\nStudents with KT list\n");
struct students
{
char name[50];
unsigned long long int roll;
unsigned int kt;
};
struct students s[5];
for (i = 0; i < 5; i++)
{
printf("\n%d.\n", i + 1);
printf("\nEnter name of student\n");
getchar(); // Clear the buffer before reading string
scanf("%[^\n]", s[i].name);
printf("\nEnter Roll number of the student\n");
scanf("%llu", &s[i].roll);
printf("\nEnter number of KT's given to the student\n");
scanf("%u", &s[i].kt);
}
max = s[0].kt;
k = 0;
for (j = 1; j < 5; j++)
{
if (max < s[j].kt)
{
max = s[j].kt;
k = j;
}
}
printf("\nDetails of the student with most KTs\n");
printf("\nName: %s\n", s[k].name);
printf("\nRoll number: %llu\n", s[k].roll);
printf("\nNumber of KTs: %u\n", s[k].kt);
return 0;
}
RESULT:
Program 6
PROBLEM Write a C Program To maintain a record of bank customer’s with four fields
STATEMENT : (Customer ID, Customer Name, Address and ACC-Num). Read and display the
bank customer details. Note: Using array of structures.
ALGORITHM : ---------------------------------------------------------------------------------------------------------
FLOWCHART : ---------------------------------------------------------------------------------------------------------
PROGRAM : #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
printf("\nWelcome NK Cooperative Bank Customer Database\n");
printf("\nEnter the number of customers to be put\n");
unsigned int n;
scanf("%u",&n);
struct customer
{
char id[50];
char name[50];
char add[1000];
unsigned long long int accn;
};
struct customer c[n];
int i,j,k;
for (i=0;i<n;i++)
{
printf("\n%d.\n",i+1);
printf("\nEnter the ID of the customer\n");
getchar();
scanf("%s",c[i].id);
printf("\nEnter the name of the customer\n");
getchar();
scanf("%[^\n]",c[i].name);
printf("\nEnter the address of the customer\n");
getchar();
scanf("%[^\n]",c[i].add);
printf("\nEnter the Account Number of the customer\n");
scanf("%llu",&c[i].accn);
}
printf("\n\n");
printf("\n\nCustomer Details:\n");
printf("| %-10s | %-20s | %-40s | %-15s |\n", "Customer ID", "Name",
"Address", "Account Number");
for (i = 0; i < n; i++)
{
printf("| %-10s | %-20s | %-40s | %-15llu |\n", c[i].id, c[i].name, c[i].add,
c[i].accn);
}
return 0;
}
RESULT:
CONCLUSION: In conclusion, these programs effectively showcase the application of
structures and unions in handling diverse data management tasks.
They provide practical examples of organizing and manipulating
complex data, ensuring efficient record-keeping and operations.