We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11
Name Anuharsh Vijay Pawar
UID no. 2022300004
Experiment No. 8
AIM: Structures Program 1
PROBLEM A structure “Cricket” consisting of following:
STATEMENT : i) Player name ii) name of the country number of matches played iii) number of hundreds scored Make a programme to read records for n players and to prepare a list according to the following heads: i) players name ii) countries name iii) number of matches played iv) number of hundreds Scored
ALGORITHM: Step 1: Declare struct player {
char name[50]; char country[50]; int number; int hundreds; }; Step 2: Declare void cricket(int n, struct player p[n]) Step 3: Write void cricket(int n, struct player p[n]) function Step 4: Use for loop and print name, country, number of matches played, number of hundreds scored of n player Step 5: Read n and take number of players from user Step 6: Read struct player pArray[n] Step 7: Use for loop and take name, country, number of matches played, number of hundreds scored of n player Step 8: Call cricket (n,pArray) PROGRAM: #include <stdio.h> struct player { char name[50]; char country[50]; int number; int hundreds; }; void cricket(int n, struct player p[n]); int main() { int n; printf("Enter no of players:\n"); scanf("%d",&n); struct player pArray[n];
for(int i=0;i<n;i++) {
printf("Enter name of player:\n");
scanf("%s",pArray[i].name);
printf("Enter country of player:\n");
scanf("%s",pArray[i].country); printf("Enter number of matches played:\n"); scanf("%d",&pArray[i].number); printf("Enter number of hundreds scored:\n"); scanf("%d",&pArray[i].hundreds); } cricket( n,pArray); return 0; } void cricket(int n, struct player p[n]) { for(int i=0;i<n;i++) { printf("Information of players:\n "); printf("Name of player:%s\n",p[i].name); printf("Name of country:%s\n",p[i].country); printf("Number of matches played:%d\ n",p[i].number); printf("Number of hundreds scored:%d\ n",p[i].hundreds); } } Program 2 PROBLEM An airline reservation system maintains records for STATEMENT : possible flights 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: Step 1: Declare struct airline { char s[40]; int start_time; char d[40]; int arr_time; int seats; }; Step 2: Declare struct airline getdata(struct airline a) function Step 3: Write struct airline getdata(struct airline a) function Step 4: Take data starting point, start time, destination, arrival time, number of seats from user Step 5: Declare void bookticket(int x, struct airline a[x]) Step 6: Write void bookticket(int x, struct airline a[x]) Step 7: Read n=1, check=0 and run while loop which has condition n Step 8: Take starting point, destination and number of seats from customer/user Step 9: Run for loop till i<n Step 10: Check if confition if((strncmp(temp, a[i].s, 3)==0) && (strncmp(temp2, a[i].d, 3)==0) && (temp3 <= a[i].seats)) Step 11: If above condition is true, then print flight is available and a[i].seats -= temp3 also print flight details check++ and break Step 12: Check another if condition if(check ==0) Step 13: If above condition is true, then print sorry we are unable to book tickets Step 14: print to continue booking press 1 and to exit print 0 take number from user Step 15: Write main function Step 16: Read n and take number of flights from user Step 17: Call function struct airline a[n]; Step 18: Use for loop and getdata from user Step 19: Call function booktickets (n,a) PROGRAM: #include<stdio.h> #include<string.h> struct airline { char s[40]; int start_time; char d[40]; int arr_time; int seats; }; struct airline getdata(struct airline a) { char temp1[4]; char temp2[4]; printf("Starting point:"); scanf("%s", temp1); strcpy(a.s, temp1); printf("Start Time:"); scanf("%d", &a.start_time); printf("Destination:"); scanf("%s", temp2); strcpy(a.d, temp2); printf("Arrival Time:"); scanf("%d", &a.arr_time); printf("Number of seats:"); scanf("%d", &a.seats); return a; } void bookticket(int x, struct airline a[x]) { printf("\nWelcome to airline reservation system!\n"); int n=1; while(n) { char temp1[4]; char temp2[4]; int temp3; char temp[4]; getchar(); printf("Starting point:"); scanf("%s", temp1); strcpy(temp, temp1); printf("Destination:"); scanf("%s", temp2); printf("Number of seats:"); scanf("%d", &temp3); int check=0; for(int i=0; i<x; i++) { if((strncmp(temp, a[i].s, 3)==0) && (strncmp(temp2, a[i].d, 3)==0) && (temp3 <= a[i].seats)) { printf("\nFlight is available\n"); a[i].seats -= temp3; printf("Flight details:\n"); printf("Start point %s\n", a[i].s); printf("Destination %s\n", a[i].d); printf("Time: %d to %d\n", a[i].start_time, a[i].arr_time); printf("Tickets booked: %d\n\n",temp3); check++; break; } } if(check==0) printf("Sorry! We are unable to book the flight\n"); printf("To continue booking press 1\nTo exit press 0\ n"); scanf("%d",&n); } } int main() { int n; printf("Enter the number of flights\n"); scanf("%d", &n); struct airline a[n]; for(int i=0; i<n; i++) { a[i]= getdata(a[i]); printf("\n"); } bookticket(n,a); return 0;
RESULT: CONCLUSION: It helps to learn about structures. As well as it also helps in logical thinking.