Tuesday, March 22, 2011

Structures and Unions

Design a structure student_record to contain name, date of birth and total marks obtained. Use the date structure designed in previous program to represent the date of birth.
Develop a program to read data for 10 students in a class and list them rank-wise.

#include <stdio.h>
#include <string.h>
struct student_record
{
char name[20];
int marks;
struct
{
int day;
int month;
int year;
}birth;
}student[10];
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("Student name:");
scanf("%s",&student[i].name);
printf("Date of Birth:");
scanf("%d %d %d",&student[i].birth.day,&student[i].birth.month,&student[i].birth.year);
printf("Marks obtained:");
scanf("%d",&student[i].marks);
}
printf("\nStudent Name      DOB      Marks\n");
for(i=1;i<=10;i++)
printf("%-15s %2d/%2d/%4d %-5d\n",student[i].name,student[i].birth.day,student[i].
birth.month,student[i].birth.year,student[i].marks);
getch();
}

Structures and Unions

Define a structure called cricket that will describe the following information:
Player name
team name
batting average
Using cricket, declare an array player with 50 elements and write a program to read the information about all the 50 players and print a team-wise list containing names of players with their batting average.

#include <stdio.h>
struct cricket
{
char name[20];
char team[25];
int batting;
};
void main()
{
int i=1,j,temp;
char dummy[25];
struct cricket player[60];
clrscr();
printf("This program is designed for collect cricket player information\n");
while(i<=50)
{
printf("Player Name:");
scanf("%s",player[i].name);
printf("Team Name:");
scanf("%s",player[i].team);
printf("Batting Average:");
scanf("%d",&player[i].batting);
i++;
}
clrscr();
for(i=1;i<50;i++)
for(j=i+1;j<=50;j++)
{
if(strcmp(player[i].team,player[j].team)>0)
{
strcpy(dummy,player[i].team);
strcpy(player[i].team,player[j].team);
strcpy(player[j].team,dummy);
strcpy(dummy,player[i].name);
strcpy(player[i].name,player[j].name);
strcpy(player[j].name,dummy);
temp=player[i].batting;
player[i].batting=player[j].batting;
player[j].batting=temp;
}
}
printf("Team Name       Player Name     Batting Average\n");
for(i=1;i<=50;i++)
printf("%-15s %-15s %10d\n",player[i].team,player[i].name,player[i].batting);
getch();
}

Structures and Unions

Define a structure that can describe an 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:
  • To print out hotels of a given grade in order of charges
  • To print out hotels with room charges less than a given value
#include <stdio.h>
struct hotel
{
char name[20];
char add[20];
int grade;
int arc;
int rooms;
};
void output();
void out();
struct hotel inn[]={
{"PLAZA","G-99,DELHI",3,4500,50},
{"MYUR","E-45,NOIDA",4,5000,100},
{"RAJDOOT","H-44,DELHI",2,4000,50},
{"SAMRATH","B-75,BOMBAY",5,6000,200},
{"SURYA","A-77,NOIDA",1,3500,150}
};
void main()
{
int go;
clrscr();
printf("Enter 1 for grade search\n");
printf("Enter 2 to search by charge:");
scanf("%d",&go);
switch(go)
{
case 1: output();
break;
case 2: out();
break;
default:printf("Wrong input");
break;
}
getch();
}
void output()
{
int gr,i;
printf("Enter Grade 1 to 5:");
scanf("%d",&gr);
if(gr>=1||gr<=5)
{
for(i=0;i<=4;i++)
{
if(inn[i].grade==gr)
printf("Hotel Name: %s\nAddress:%s\nGrade:%d\nAverage Room charge:%d\n\
Number of Rooms:%d",inn[i].name,inn[i].add,inn[i].grade,inn[i].arc,inn[i].rooms);
}
}
else
printf("Wrong grade input!");
}
void out()
{
int ch,i=0;
printf("Enter the Room charges not greater than 6000:");
scanf("%d",&ch);
while(i<5)
{
if(inn[i].arc<ch)
printf("Hotel Name: %s\nAddress:%s\nGrade:%d\nAverage Room charge:%d\n\
Number of Rooms:%d\n",inn[i].name,inn[i].add,inn[i].grade,inn[i].arc,inn[i].rooms);
i++;
}
}

OUTPUT
Enter 1 for grade search
Enter 2 to search by charge: 1
Enter grade 1 to 5: 1
Hotel Name : SURYA
Address: A-77, NOIDA
Grade: 1
Average Room Charge: 3500
Number of Rooms: 150

Enter 1 for grade search
Enter 2 to search by charge: 1
Enter grade 1 to 5: 2
Enter the Room Charges not greater than 6000: 4500
Hotel Name : RAJDOOT
Address: H-44, DELHI
Grade: 2
Average Room Charge: 4000
Number of Rooms: 50
Hotel Name : SURYA
Address: A-77, NOIDA
Grade: 1
Average Room Charge: 3500
Number of Rooms: 150