Experiment 8
Experiment 8
AIM:
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.
PROGRAM: #include<stdio.h>
struct player{
char name[25];
int age;
int active;
int married;
char city[20];
};
struct family{
char wife[25];
int child;
};
struct children{
char name[10];
};
int main()
{
struct player Players[5];
struct family fam[5];
for(int i = 0; i < 5; i++)
{
printf("Enter player name: ");
scanf("%s", Players[i].name);
printf("Enter player age: ");
scanf("%d", &Players[i].age);
printf("Is player active? ");
scanf("%d", &Players[i].active);
printf("Is player married? ");
scanf("%d", &Players[i].married);
printf("Enter player city: ");
scanf("%s", Players[i].city);
printf("\n");
}
int count = 0;
int married[5];
printf("\n");
struct flight{
char Start[4];
char End[4];
char Stime[5];
char Atime[5];
char Seats[4];
};
int main()
{
int flag = 0;
char start[4];
char End[4];
printf("Enter the starting point : \n");
scanf("%s", start);
printf("Enter the ending point : \n");
scanf("%s", End);
int count[20];
int counter = 0;
for(int i = 0; i < 20; i++)
{
if(strcasecmp(start, flights[i].Start) == 0 &&
strcasecmp(End , flights[i].End) == 0 && flights[i].Seats > 0)
{
flag = 1;
printf("%d. Available flight : %s to %s time :
%s to %s\n",counter + 1 , flights[i].Start , flights[i].End , flights[i].Stime,
flights[i].Atime);
count[counter] = i;
counter++;
}
}
if(flag == 0)
{
printf("Sorry no flights are available !\n");
return 0;
}
int selected_flight;
while(1)
{
int no;
printf("Select which flight you want : \n");
scanf("%d", &no);
int flight = count[no - 1];
printf("The details of the flight are :\n");
printf("Start : %s\n",flights[flight].Start);
printf("End : %s\n",flights[flight].End);
printf("Dept Time : %s\n",flights[flight].Stime);
printf("Seats Available : %s\n",flights[flight].Seats);
char choice[2];
printf("\n Do you want to book the flight ?
(Yes/No/Stop)\n");
scanf("%s", choice);
if(strcasecmp(choice,"Yes") == 0)
{
printf("Selected flight time = %s to %s
\n",flights[flight].Stime, flights[flight].Atime);
break;
}
else if(strcasecmp(choice,"Stop") == 0)
{
break;
}
else if(strcasecmp(choice,"No") == 0)
continue;
}
}
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.
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();
printf("Enter no. of games lost:\n");
scanf("%d",&details[i].num_games_lost);
getchar();
printf("Enter no. of goals scored:\n");
scanf("%d",&details[i].num_goals);
getchar();
printf("Enter points:\n");
scanf("%d",&details[i].points);
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("Team Name\tGames Played\tGames Won\tGames
Drawn\t\tGames Lost\t\tGoals Scored\t\tPoints\n");
for (int i = 0; i < n; i++)
{
printf("%-15s\t%-15d\t%-15d\t%-15d\t%-15d\t%-15d\t%-
15d\n",details[i].team_name,details[i].num_games_played,details[i].num_g
ames_won,details[i].num_games_draw,
details[i].num_games_lost,details[i].num_goals,details[i].points);
}
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).
int main() {
printf("\nFintech Limited\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;
};
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--;
return 0;
}
RESULT:
Program A1
PROBLEM Create a structure student with data member as name, Roll No. & KT.
STATEMENT: Accept and Display details for five students and display the details of
Students with Highest Number of KTs.
PROGRAM: #include<stdio.h>
#include<string.h>
void main()
{
struct student
{
char name[50];
int roll_no;
int kt;
};
struct student data[5];
printf("Enter the data of students:\n");
for (int i=0;i<5;i++)
{
printf("\n%d.\n", i + 1);
printf("Enter the name of the student:\n");
scanf("%[^\n]",data[i].name);
getchar();
printf("Enter the roll no.:\n");
scanf("%d",&data[i].roll_no);
getchar();
printf("Enter the no. of KT:\n");
scanf("%d",&data[i].kt);
getchar();
}
printf("Following are the details of all students:\n");
printf("Name\t\t\t\tRoll no\t\t\tKT\n");
for(int i=0;i<5;i++)
{
printf("%s\t\t\t\t%d\t\t\t%d\n",data[i].name,data[i].roll_no,data[i].kt);
}
int j=0;
for (int i=1;i<5;i++)
{
if(data[i].kt>data[j].kt)
{
j=i;
}
}
printf("The details of the student with highest no of kt is :\n");
printf("Name :\n");
printf("%s\n",data[j].name);
printf("Roll no.:\n");
printf("%d\n",data[j].roll_no);
printf("The no of KT:\n");
printf("%d\n",data[j].kt);
}
RESULT:
Program A2
PROBLEM Write a C Program To maintain a record of bank customer’s with four
STATEMENT: fields (Customer ID, Customer Name, Address and ACC-Num). Read
and display the bank customer details. Note: Using array of structures.
PROGRAM:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
struct bank
{
int custid;
char name[100];
char address[500];
int acc_no;
};
int n;
printf("Enter the no. of details to be entered;\n");
scanf("%d",&n);
getchar();
struct bank data[n];
for(int i=0;i<n;i++)
{
printf("\n%d.\n", i + 1);
printf("Enter the customer id;\n");
scanf("%d",&data[i].custid);
getchar();
printf("Enter the name of customer:\n");
scanf("%[^\n]",data[i].name);
getchar();
printf("Enter the address:\n");
scanf("%[^\n]",data[i].address);
getchar();
printf("Enter the account no:\n");
scanf("%d",&data[i].acc_no);
getchar();
}
int check;
int flag=0;
printf("***************Display of Details***************\n");
printf("Enter the customer id:\n");
scanf("%d",&check);
for(int i=0;i<n;i++)
{
if (check==data[i].custid)
{
flag=1;
printf("Customer ID matched:\n");
printf("NAME :%s\n",data[i].name);
printf("ADDRESS :%s\n",data[i].address);
printf("ACCOUNT NO. :%d\n",data[i].acc_no);
break;
}
}
if (flag==0)
{
printf("Customer id not found.\n");
}
printf("******************Thank you***********************");
}
RESULT:
CONCLUSION: In this experiment, I learned about the various use cases of structures and
unions, as well as their associated functions. I explored how to access and
declare elements, how to initialize structures and unions, and other key
concepts. Additionally, I gained practical knowledge on implementing
structures and unions in programs.