Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Muhammad Souban Javed FA20-BEE-2C-146

Lab 12:
Structures

In-Lab task:

4.1. If we have the structure of the student given as and suppose we have ten students struct student
{
char name[20];
int eng;
int math;
int phys;
double mean;
};
a) We want to calculate mean of three subjects of each person.
b) We want to determine grade based on mean of three subjects and table shown in below. Confirm by
executing it.
Mean Grade
90 <= x <= 100 S
80 <= x < 90 A
70 <= x < 80 B
60 <= x < 70 C
x < 60 D

Answer:
#include <stdio.h>

struct Marks {
float eng_marks, maths_marks, phy_marks;
};

int main()
{

struct Marks marks[3];

for(int i=0; i<4; i++){


printf("Student %d\n",i+1);
printf("English marks :\n");
scanf("%f", &marks[i].eng_marks);
printf("Maths marks :\n");
scanf("%f", &marks[i].maths_marks);
printf("Physics marks :\n");
scanf("%f", &marks[i].phy_marks);
}

for(int i=0; i<3; i++) {


printf("Student %d\n",i+1);
float grade = (marks[i].eng_marks + marks[i].maths_marks + marks[i].phy_marks)/3;
if(grade >=90 && grade <=100)
{
printf("grade = S \n");
}
if(grade >=80 && grade <=90)
{
printf("grade = A \n");
}
if(grade >=70 && grade <=80)
{
printf("grade = B \n");
}
if(grade >=60 && grade <=70)
{
printf("grade = C \n");
}
if(grade <=60)
{
printf("grade = D \n");
}
}
return 0;
}
4.2. Write a program that compares two given dates. To store date use structure, say date that contains three
members namely date, month and year. If the dates are equal, then display message as "Equal" otherwise
"Unequal".
Answer:
#include<stdio.h>

struct date
{
int day;
int month;
int year;
};

void main()
{
struct date d1,d2;

printf("Enter first date(DD/MM/YYYY):\n");


scanf("%d%d%d",&d1.day,&d1.month,&d1.year);
printf("Enter second date(DD/MM/YYYY):\n");
scanf("%d%d%d",&d2.day,&d2.month,&d2.year);

if((d1.day==d2.day)&&(d1.month==d2.month)&&(d1.year==d2.year))
printf("The dates are equal\n");
else
printf("The dates are unequal\n");

4.3. A record contains name of a footballer, his age, number of games he has played, and the average number of
goals has scored in each match. Create an array of structure to hold records of 20 such players and then write a
program to read these records. Program would be able to tell the name of player that has played maximum
number of matches, the name of youngest player and a player name who scored is lowest.
Answer:

#include<stdio.h>

struct cricketer

int avegoals;

int gamesplayed;

char name[25];

}player[100];

void main()

int i,n;

printf("Enter the no of Football players\n");

scanf("%d",&n);

printf("Enter player Name , Number of games played and Average goals in played games\n");

for(i=0;i<n;i++)

scanf("%s %d %d",player[i].name,&player[i].gamesplayed,&player[i].avegoals);

printf("\nName\t\tGames played\t\tAverage goals\n");

for(i=0;i<n;i++)

printf("%s \t\t %d \t\t %d\n",player[i].name,player[i].gamesplayed,player[i].avegoals);

}
4.4. There is a structure called employee that holds information like employee code, name, date of joining.
Write a program to create an array of the structure and enter some data into it. Then ask the user to enter current
date. Display the names of those employees whose tenure is 3 or more than 3 years according to the given
current date.
Answer:
#include<stdio.h>

struct employee
{
int dd,mm,yy;
char code[10],name[10];
}emp[10];
int main()
{
int n,i,dd,mm,yy;
printf("Enter Numbers of Employees : ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("\nEnter Emp Code : ");
scanf("%s",emp[i].code);
printf("Enter Emp Name : ");
scanf("%s",emp[i].name);
printf("Enter Join Date (dd mm yyyy) : ");
scanf("%d%d%d",&emp[i].dd,&emp[i].mm,&emp[i].yy);
}
printf("\nEnter current Date : ");
scanf("%d%d%d",&dd,&mm,&yy);
printf("Employees working for more than 3 years \n");
for(i=0;i<n;++i)
if((yy - emp[i].yy) >= 3)
printf("\nCode : %s Name : %s Date of Join : %d/%d/%d",
emp[i].code,emp[i].name, emp[i].dd,emp[i].mm,emp[i].yy);
return 0;
}
Home tasks
5.1. Write a menu driven program that depicts the working of a library. The menu options should be:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of specified book
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Create a structure called library to hold accession number, title of the book, author name, price of the book, and
flag indicating whether book is issued or not.

You might also like