Unit 8 - Structure
8.1 - Write a program in c using structure to enter roll no, marks of the three subjects for 3 students and find total obtained by each student. [S-15]
#include <stdio.h>
struct Marks
{
int roll_no;
char name[30];
float sub1_marks, sub2_marks, sub3_marks;
};
int main()
{
struct Marks marks[5];
for(int i=1; i<4; i++)
{
printf("Student %d\n",i);
printf("Enter roll no. : ");
scanf("%d", &marks[i].roll_no);
printf("Enter name : ");
scanf("%s",marks[i].name);
printf("Enter Subject1 marks : ");
scanf("%f", &marks[i].sub1_marks);
printf("Enter Subject2 marks : ");
scanf("%f", &marks[i].sub2_marks);
printf("Enter Subject3 marks : ");
scanf("%f", &marks[i].sub3_marks);
}
for(int i=1; i<4; i++)
{
printf("Student %d\n",i);
float total = (marks[i].sub1_marks + marks[i].sub2_marks + marks[i].sub3_marks);
printf("Total : %f\n", total);
}
return 0;
}
8.2 - Define a structure named “Student” with roll_no, name and percentage as members. Read data of 5 students from user and display them in proper format. [W-13]
8.3 - Define a structure called cricket that will describe the following information: a. Player name b. Team name c. Batting average [W-17]
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct cricket
{
char pname[20];
char tname[20];
float bavg;
};
int main()
{
struct cricket s[5],t;
int i,j,n=5;
float p;
printf("\nEnter data of %d players\n",n);
for(i=0;i<n;i++)
{
printf("\nEnter PName TName BAvg for player-%d = ",i+1);
scanf("%s %s %f",s[i].pname,s[i].tname,&p);
s[i].bavg=p;
}
for(i=1;i<=n-1;i++)
{
for(j=1;j<=n-i;j++)
{
if(strcmp(s[j-1].tname,s[j].tname)>0)
{
t=s[j-1];
s[j-1]=s[j];
s[j]=t;
}
}
}
printf("\nAfter teamwise sorting... Player list is ");
for(i=0;i<n;i++)
{
printf("\n%-20s %-20s %.2f",s[i].pname,s[i].tname,s[i].bavg);
}
getch();
return 0;
}
8.4 - Define a structure data type called time_struct containing three member’s integer hours, minutes, second. Develop a program that would assign values to individual member and display the time in following format : HH:MM:SS [W-18]
#include <stdio.h>
struct time_struct
{
int hour;
int minute;
int second;
}t;
int main(void)
{
printf("\n Enter Hour : ");
scanf("%d",&t.hour);
printf("\n Enter Minute: ");
scanf("%d",&t.minute);
printf("\n Enter Second : ");
scanf("%d",&t.second);
printf("\n Time %d:%d:%d",t.hour%24,t.minute%60,t.second%60);
return 0;
}
8.5 - Define a structure “personal” that would contain person name, date of joining and salary. Using this structure read information of 5 people and print the same on screen. Also display sum of salary of all 5 people. [S-20]
#include <stdio.h>
struct personal
{
char name[20];
char doj[10];
float salary;
}p[5];
int main(void)
{
int i=1;
for(i=1;i<=5;i++)
{
printf("\nEnter Person Name : ");
scanf("%s",p[i].name);
printf("Enter Person Date of Joining (dd-mm-yyyy) : ");
scanf("%s",p[i].doj);
printf("Enter Person Salary : ");
scanf("%f",&p[i].salary);
}
for(i=1;i<=5;i++)
{
printf("\nPerson %d Detail",i);
printf("\nName = %s",p[i].name);
printf("\nDOJ = %s",p[i].doj);
printf("\nSalary = %.2f",p[i].salary);
}
{
float sum = (p[1].salary + p[2].salary + p[3].salary + p[4].salary + p[5].salary);
printf("\n\nSum of salary : %f\n", sum);
}
return 0;
}
No comments:
Post a Comment
If you have any doubts or suggestions, Please let me know