0% found this document useful (0 votes)
6 views24 pages

Experiment 8

Uploaded by

nachiketkale03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views24 pages

Experiment 8

Uploaded by

nachiketkale03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Name CHINMAY HARSHAD KARMALKAR

UID no. 2024800053


Experiment No. 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];

for(int i = 0; i < 5; i++)


{
if(Players[i].active == 1 && Players[i].married == 1)
{
printf("Enter name of wife of %s :",
Players[i].name);
scanf("%s", fam[i].wife);
printf("Enter the no of children of Player %s :",
Players[i].name);
scanf("%d", &fam[i].child);
}
}

struct children child[5][10];

for(int i = 0; i < 5; i++)


{
if(Players[i].active == 1 && Players[i].married == 1)
{
for(int j = 0; j < fam[i].child; j++)
{
printf("Enter the name of %s's child no %d:
",Players[i].name, j + 1);
scanf("%s", child[i][j].name);
}
}
}

printf("\n");

for(int i = 0; i < 5; i++)


{
if(Players[i].active == 1 && Players[i].married == 1)
{
printf("The wife of %s is :
%s\n",Players[i].name,fam[i].wife);
for(int j = 0; j < fam[i].child; j++)
{
printf("Name of %s's child number %d is :
%s\n",Players[i].name, j + 1, child[i][j].name);
}
}
}
}
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)
PROGRAM: #include<stdio.h>
#include<string.h>
#include<stdbool.h>

struct flight{
char Start[4];
char End[4];
char Stime[5];
char Atime[5];
char Seats[4];
};

int main()
{

struct flight flights[20] = {


{"MUM","DEL","900","1100","20"},
{"HYD","DEL","1000","1215","10"},
{"KER","MUM","1300","1500","30"},
{"MUM","BLR","930","1130","5"},
{"BLR","DEL","800","1100","10"},
{"HYD","BLR","945","1100","10"},
{"NGP","PUN","700","815","40"},
{"MUM","GWT","1200","1500","2"},
{"MUM","NGP","1300","1430","40"},
{"PUN","BLR","900","1030","10"},
{"MUM","MAD","1100","1300","15"},
{"MAD","DEL","1400","1650","20"},
{"BLR","MUM","1400","1600","3"},
{"PUN","MAD","1700","1840","15"},
{"PUN","DEL","1600","1810","22"},
{"MUM","DEL","1830","2030","5"},
{"MUM","BLR","1700","1900","10"},
{"DEL","HYD","1500","1715","15"},
{"GWT","DEL","900","1120","20"},
{"BLR","MUM","2100","2300","15"}
};

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).

PROGRAM: #include <stdio.h>


#include <stdlib.h>
#include <string.h>

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;
};

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 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy