1. 33
Set 3 Q1 Define a structure called cricket that will describe the following
information.
Player name, Team name, Batting Average
Using cricket declare an array player with n elements and write a program to read
the information about all the n elements and print a team-wise listing containing
names of players with their batting average
Program
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
struct cricket
{
char plyname[20];
char teamname[20];
float batavg;
};
int main()
{
struct cricket *player;
int i, no,j,k,nop;
char team[20][20], temp[20][20], temp1[20];
printf("Enter number of players ");
scanf("%d", &no);
nop=no;
player = (struct cricket*) malloc (no * sizeof(struct cricket));
2. 34
for(i = 0; i < no; i++)
{
printf("Details of Player %dn",i+1);
fflush(stdin);
printf("Enter the Player Name : ");
gets((player+i)->plyname);
fflush(stdin);
printf("Enter the Team Name : ");
gets((player+i)->teamname);
fflush(stdin);
printf("Enter the Batting Average : ");
scanf("%f",&((player+i))->batavg);
}
for(i = 0; i < no; i++)
{
strcpy(team[i],(player+i)->teamname);
strcpy(temp[i],team[i]);
}
for (i = 0; i < no; i++)
{
for (j = i + 1; j < no;)
{
if (strcmp(temp[j],team[i])==0)
{
for (k = j; k < no; k++)
{
4. 36
Output
Enter the Player Name : dhoni
Enter the Team Name : australia
Enter the Batting Average : 89
Details of Player 3
Enter the Player Name : ricky
Enter the Team Name : australia
Enter the Batting Average : 56
Details of Player 4
Enter the Player Name : ganguly
Enter the Team Name : india
Enter the Batting Average : 62
Details of Player 5
Enter the Player Name : sreesanth
Enter the Team Name : kenya
Enter the Batting Average : 82
Player's Details:
Country Name Batting Average
india
sachin 78.00
ganguly 62.00
australia
dhoni 89.00
ricky 56.00
kenya
sreesanth 82.00
5. 37
Set 3 Q2 Define a structure that can describe a Hotel. It should have members
that include the name, address, grade, average room charge and number of rooms.
Write functions to perform the following operations:
a)To print out hotels of a given grade in order of charges.
b)To print out hotels with room charges less than a given value.
Program
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct details
{
char name[40],address[40],grade;
float rate;
int num;
};
void search1(struct details *ob,char g,int n);
void search2(struct details *ob,float r,int n);
int main()
{
struct details *hotel;
int n,i;
char g;
6. 38
float rate;
puts("Enter the number of hotels:");
scanf("%d",&n);
hotel = (struct details*) malloc (n * sizeof(struct details));
for(i=0;i<n;i++)
{
printf("Details of hotel %dn",i+1);
fflush(stdin);
printf("Enter the hotel name:");
gets((hotel+i)->name);
fflush(stdin);
printf("Enter the hotel address:");
gets((hotel+i)->address);
fflush(stdin);
printf("Enter the hotel grade:-('a','b','c','d')");
scanf("%c",&(hotel+i)->grade);
fflush(stdin);
puts("Enter the room charge:");
scanf("%f",&(hotel+i)->rate);
fflush(stdin);
puts("Enter the room numbers:");
scanf("%d",&(hotel+i)->num);
}
fflush(stdin);
7. 39
printf("Search for specific hotelsn");
printf("Enter the grade for hotel('a','b','c','d'):");
scanf("%c",&g);
search1(hotel,g,n);
printf("nEnter the maximum charge:");
scanf("%f",&rate);
search2(hotel,rate,n);
return 0;
}
void search1(struct details *ob,char g,int n)
{
int i,j,x=0;
int flag=1;
char final[n][40],temp[40];
float r[n],t;
for(i=0;i<n;i++)
{
if(g==ob[i].grade)
{
strcpy(final[x],ob[i].name);
r[x]=ob[i].rate;
x++;
flag=0;
}
}
for(i=0;i<x;i++)
9. 41
{
int i,x=0;
int flag=1;
char final[n][40];
float r[n];
for(i=0;i<n;i++)
{
if(r1>ob[i].rate)
{
r[x]=ob[i].rate;
strcpy(final[x],ob[i].name);
x++;
flag=0;
}
}
if (flag==0)
{
printf("Hotel names below the specified RATE.");
for(i=0;i<x;i++)
{
printf("n%stt%.2f",final[i],r[i]);
}
}
else
printf("No hotel room below %0.2f available", r1);
}
10. 42
Output
Enter the number of hotels:
5
Details of hotel 1
Enter the hotel name:rudra
Enter the hotel address:calicut
Enter the hotel grade:-('a','b','c','d')a
Enter the room charge:
250
Enter the room numbers:
10
Details of hotel 2
Enter the hotel name:agni
Enter the hotel address:kochi
Enter the hotel grade:-('a','b','c','d')a
Enter the room charge:
500
Enter the room numbers:
20
Details of hotel 3
Enter the hotel name:prithvi
Enter the hotel address:pathanamthitta
Enter the hotel grade:-('a','b','c','d')b
Enter the room charge:
1000
Enter the room numbers:
11. 43
5
Details of hotel 4
Enter the hotel name:solo
Enter the hotel address:trivandrum
Enter the hotel grade:-('a','b','c','d')c
Enter the room charge:
100
Enter the room numbers:
30
Details of hotel 5
Enter the hotel name:sagar
Enter the hotel address:trivandrum
Enter the hotel grade:-('a','b','c','d')a
Enter the room charge:
500
Enter the room numbers:
5
Search for specific hotels
Enter the grade for hotel('a','b','c','d'):a
Hotel names with specified GRADE in order of charge
rudra 250.00
agni 500.00
sagar 500.00
Enter the maximum charge:300
Hotel names below the specified RATE.
rudra 250.00
solo 100.00
12. 44
Set 3 Q3 Define a structure named mea_class which include the following
members:
Roll number, Name, mark of 5 subjects.
Write a program to calculate the aggregate percentage and the grade of each
student, and print the details in a neat format. The constraint for grading is as
follows:
Aggregate % Grade
>=95 A++
>=85 and <95 A+
>=80 and <85 A
>=75 and <80 B++
>=70 and <75 B+
>=60 and <70 B
>=50 and <60 C
<50 F
Program
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct mca_class
{
int rollno;
char name[40];
int mark[5];
float per;
14. 46
fflush(stdin);
printf("Student Roll No:");
scanf("%d",&(student+i)->rollno);
fflush(stdin);
printf("Enter the Student name:");
gets((student+i)->name);
fflush(stdin);
printf("Enter the mark of subjects out of 100 n");
total=0;
for (j=0;j<5;j++)
{
printf("Enter the mark of subject %d:",j+1);
scanf("%d",&(student+i)->mark[j]);
total=total+(student+i)->mark[j];
}
per=total/5;
student[i].per=per;
printf("%0.2f,",per);
if (per>=95)
strcpy(gr,"A++");
else if ((per>=85)&&(per<95))
strcpy(gr,"A+");
else if ((per>=80)&&(per<85))
strcpy(gr,"A");
else if ((per>=75)&&(per<80))
strcpy(gr,"B++");
else if ((per>=70)&&(per<75))
17. 49
Enter the mark of subject 5:78
Student Roll No:2
Enter the Student name:binu
Enter the mark of subjects out of 100
Enter the mark of subject 1:48
Enter the mark of subject 2:23
Enter the mark of subject 3:17
Enter the mark of subject 4:14
Enter the mark of subject 5:85
Student Roll No:3
Enter the Student name:eva
Enter the mark of subjects out of 100
Enter the mark of subject 1:12
Enter the mark of subject 2:10
Enter the mark of subject 3:09
Enter the mark of subject 4:7
Enter the mark of subject 5:05
Student Roll No:4
Enter the Student name:joshi
Enter the mark of subjects out of 100
Enter the mark of subject 1:45
Enter the mark of subject 2:99
Enter the mark of subject 3:98
Enter the mark of subject 4:96
Enter the mark of subject 5:93
Student Roll No:5
Enter the Student name:santhi
18. 50
Enter the mark of subjects out of 100
Enter the mark of subject 1:78
Enter the mark of subject 2:99
Enter the mark of subject 3:91
Enter the mark of subject 4:85
Enter the mark of subject 5:23
List of students:
=========================
Roll No Name Mark 1 Mark 2 Mark 3 Mark 4 Mark 5 Per Grade
=========================================================
1 aakash 80 45 35 14 78 50.40 C
2 binu 48 23 17 14 85 37.40 F
3 eva 12 10 9 7 5 8.60 F
4 joshi 45 99 98 96 93 86.20 A+
5 santhi 78 99 91 85 23 75.20 B++
19. 51
Set 3Q5 Define a structure named employee consisting of the following members:
Employee ID, Name, Department, Basic Salary, DA (in %), HRA(in%).
Write a program to prepare the pay-slip of the employees of a particular
department, using array of structures.
Program
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct employee
{
int empid;
char name[40];
char dept[40];
float basic;
float da;
float hra;
float gross;
};
void read (struct employee *emp,int n);
void display(struct employee *emp,char* dept,int n);
int main()
{
20. 52
struct employee *emp;
int n;
char dept[40];
puts("Enter the number of employees");
scanf("%d",&n);
emp = (struct employee*) malloc (n * sizeof(struct employee));
read (emp,n);
puts("Enter the dept name of employees whose pay slip is to be displayed:
n");
fflush(stdin);
gets(dept);;
display (emp,dept,n);
free(emp);
return 0 ;
}
void read (struct employee *emp,int n)
{
int i;
float da,hra;
printf("Enter da rate: ");
scanf("%f",&da);
21. 53
printf("Enter hra rate: ");
scanf("%f",&hra);
for(i=0;i<n;i++)
{
printf("Enter employee ID: ");
scanf("%d",&(emp+i)->empid);
printf("Enter name of the employee: ");
fflush(stdin);
gets((emp+i)->name);
printf("Enter Department name: ");
fflush(stdin);
gets((emp+i)->dept);
printf("Enter Basic salary: n");
scanf("%f",&(emp+i)->basic);
(emp+i)->da = ((emp+i)->basic*da)/100;
(emp+i)->hra = ((emp+i)->basic*hra)/100;
(emp+i)->gross = (emp+i)->basic + (emp+i)->da + (emp+i)->hra;
}
}
void display(struct employee *emp,char* dept,int n)
{
int i,found =0;
22. 54
if(emp == NULL)
{
printf("ERROR !!! NULL pointer !!!n");
return;
}
else
{
printf("nPay-slip of employees of department : %sn",dept);
printf("n");
printf("Emp IDtNametBasictDAtHRAtGross Salaryn");
printf("===============================================
===========n");
for(i=0;i<n;i++)
{
if(strcmp((emp+i)->dept,dept)==0)
{
printf("%dt%st%.2ft%.2ft%.2ft%.2fn",(emp+i)-
>empid,(emp+i)->name,(emp+i)->basic,(emp+i)->da,(emp+i)->hra,(emp+i)-
>gross);
found =1;
}
}
if(found ==0)
printf("nPay-slips NOT FOUND for the requested Dept-
%s!!!!!!!!n",dept);
printf("n");
}
23. 55
}
Output
Enter the number of employees
5
Enter da rate: 5
Enter hra rate: 10
Enter employee ID: 1
Enter name of the employee: sarun
Enter Department name: it
Enter Basic salary:
1000
Enter employee ID: 2
Enter name of the employee: sajan
Enter Department name: sales
Enter Basic salary:
2000
Enter employee ID: 3
Enter name of the employee: joby
Enter Department name: it
Enter Basic salary:
5000
Enter employee ID: 4
Enter name of the employee: ginu
Enter Department name: it
Enter Basic salary:
2000
24. 56
Enter employee ID: 5
Enter name of the employee: shibu
Enter Department name: marketing
Enter Basic salary:
5400
Enter the dept name of employees whose pay slip is to be displayed:
it
Pay-slip of employees of department : it
Emp ID Name Basic DA HRA Gross Salary
=========================================================
1 sarun 1000.00 50.00 100.00 1150.00
3 joby 5000.00 250.00 500.00 5750.00
4 ginu 2000.00 100.00 200.00 2300.00
25. 57
Set 3 Q5Write a program to accept records of the different states using array of
structures. The structure should contain state_name, population, literacy_rate and
per_capita_income. Assume suitable data. Display the state whose literacy rate is
highest and whose per capita income is highest. (use typedef in the program).
Program
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct statedemography
{
char state_name[50];
long int population;
float literacy_rate;
float per_capita_income;
}statedetails;
void readstate(statedetails * , int );
void printstate(statedetails * , int);
int main(void)
{
int n;
statedetails *state;
26. 58
printf("Enter the number of states: ");
scanf("%d", &n);
state = (statedetails*)malloc(sizeof(statedetails)*n);
readstate(state, n);
printstate(state, n);
free(state);
return 0;
}
void readstate(statedetails *state, int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Enter name of the state: n");
fflush(stdin);
gets((state+i)->state_name);
printf("nEnter population: n");
scanf("%ld",&(state+i)->population);
printf("Enter literacy rate: n");
scanf("%f",&(state+i)->literacy_rate);
printf("Enter per capita income: n");
28. 60
maxpercapIncome = (state+i)->per_capita_income;
}
for(i=0;i<n;i++)
{
if ((state+i)->literacy_rate == maxLiteracy)
printf("State %s",(state+i)->state_name);
printf(" have Maximum literacy
rate:%.2fn",maxLiteracy);
}
printf("n");
for(i=0;i<n;i++)
{
if ((state+i)->per_capita_income == maxpercapIncome)
printf("State %s ",(state+i)->state_name);
printf(" have Maximum per capita
income:%.2fn",maxpercapIncome);
}
printf("n");
}
}
29. 61
Output
Enter the number of states: 5
Enter name of the state: kerala
Enter population: 15000
Enter literacy rate: 99
Enter per capita income: 5000
Enter name of the state: bihar
Enter population: 58000
Enter literacy rate: 85
Enter per capita income: 1500
Enter name of the state: gujarat
Enter population: 90000
Enter literacy rate: 45
Enter per capita income: 7400
Enter name of the state: punjab
Enter population: 56000
Enter literacy rate: 23
Enter per capita income: 4800
Enter name of the state: goa
Enter population: 96000
Enter literacy rate: 93
Enter per capita income: 5200
State kerala have Maximum literacy rate:99.00
State gujarat have Maximum per capita income:7400.00