Structure
Structure
Chap 8: Structures
• Structure Definition, Declaration and Initialization of Variables, Accessing Structure
members
• Nested Structure
• Array of Structures and Array within the structure
• Structures and Functions
MCQ:
1. Structure can contain elements of the same data type
A
A. TRUE
B. FALSE
Solution: Structure can contain elements of same datatype and different datatype as well.
Page 1
4. What is the correct syntax to access the value of struct variable book{ price, page }?
A
A. printf("%d%d", [Link], [Link]);
B. printf("%d%d", [Link], [Link]);
C. printf("%d%d", price::book, page::book);
D. printf("%d%d", price->book, page->book);
Solution: we can use dot sign (.) to access simple structure variables.
(Note : When we are suing pointers with structure we can use -> sign but that you will study
in detail in 10th chapter. Here in given case answer is only dot for simple variable).
Page 2
A. TAJ
B. Empty
C. Compiler Error
D. None of the above
Solution:
int main()
{
/*creating structure named as hotel which can be used inside main only because it is declared
in main. But this will give an error because instead of keyword struct we are using
structure.*/
structure hotel
{
int items;
char name[10];
}a;
strcpy([Link], "TAJ");
[Link]=10;
printf("%s", [Link]);
return 0;
}
};
return 0;
}
C
Page 3
A. It is wrong to define an empty structure
B. Member variables can be added to a structure even after its first definition.
C. There is no use of defining an empty structure
D. None of the above
Solution:
When you declare any structure variable, memory will be allocated as per the total size
occupied by each member. If you declare empty structure then its instance can not be given
memory at runtime. So it will through an error that member not declared.
Page 4
printf("%d",s.t.b);
}
struct stud
{
int f(5);
};
int main()
{
struct stud s;
printf("%d",s.f(5));
}
You cannot use function as a structure member.
Page 5
A. struct temp
{
}s;
main()
{
}
B. struct temp
{
};
struct temp s;
main()
{
}
C. struct temp s;
struct temp
{
};
main()
{
}
Page 6
B. Structure members cannot be initialized at the time of declaration
C. Only integer members of structure can be initialized at the time of declaration
D. None of the above
Solution: This may be accepted in CPP files but not in C that is .c files. Following code will
throw an error in .c file.
#include<stdio.h>
struct temp
{
char name[10] = "komal";
int num = 20;
}a;
int main()
{
prinf("%s",[Link]);
prinf("\n%d",[Link]);
}
Page 7
Practical:
1. WAP to demonstrate simple structure str which is having int, float and char
members and show how can you declare, initialize and access the structure
members.
#include<stdio.h>
struct str
{
int a;
float b;
char c;
};
int main()
{
struct str s; // declaration of structure variable
printf("Enter value of a: ");
scanf("%d", &s.a);
printf("Enter value of b: ");
scanf("%f", &s.b);
printf("Enter value of c: ");
fflush(stdin);
scanf("%c", &s.c);
printf("\nValue of structure s is:");
printf("\na = %d\n b = %f\n c = %c",s.a,s.b,s.c);
2. WAP having structure with following elements: Name of employee, basic, DA,
TA, deduction, net_salary. Display individual elements of structure in proper
format
Method – 1:
#include<stdio.h>
Page 8
struct emp
{
char name[100];
int basic;
int da;
int ta;
int deduction;
int net_salary;
};
int main()
{
struct emp e;
printf("Enter name: ");
gets([Link]);
printf("Enter value of basic: ");
scanf("%d", &[Link]);
printf("Enter value of DA: ");
scanf("%d", &[Link]);
printf("Enter value of TA: ");
scanf("%d", &[Link]);
printf("Enter value of Deduction: ");
scanf("%d", &[Link]);
e.net_salary = [Link]+[Link]+[Link];
printf("\nEmployee Details:");
printf("\n Name\t = %s\n Basic\t = %d\n DA\t = %d\n TA\t = %d\n Deduction\t =
%d\n Net\t = %d",[Link], [Link], [Link], [Link], [Link], e.net_salary); }
Method – 2:
#include <stdio.h>
struct salary
Page 9
{
char name[20];
float basic, da, hra, ta, others;
float pf,gross_salary;
float net_salary;
}sal;
int main (void)
{
//Input basic salary
printf("Enter name of Employe\n");
gets([Link]);
printf("Input Basic Salary: ");
scanf("%f", &[Link]);
//Calculate DA
[Link] = [Link] * 25.0 / 100.0;
//Calculate HRA
[Link] = [Link] * 15.0 / 100.0;
//Calculate PF
[Link] = sal.gross_salary * 10.0 / 100.0;
Page 10
//Print result
printf("Basic Salary: %.2f\n", [Link]);
printf("DA: %.2f\n", [Link]);
printf("HRA: %.2f\n", [Link]);
printf("Gross Salary: %.2f\n", sal.gross_salary);
printf("PF: %.2f\n", [Link]);
printf("Net Salary: %.2f", sal.net_salary);
return 0;
}
3. WAP which declares a structure named “student” with rollno, name, address
and marks [5]. The structural element marks should be an array to store marks
in each of the five subjects. The program then should read and print data for one
student using proper format.
#include<stdio.h>
struct student
{
int rollno;
char name[100];
char add[100];
int m[5];
};
int main()
{
struct student s1; // where s1 is a instance of structure student
Page 11
fflush(stdin);
gets([Link]);
printf("\nEnter Addrss: ");
fflush(stdin);
gets([Link]);
printf("\nEnter Marks: ");
for(int j=0;j<5;j++)
{
printf("\nMarks %d : ", j+1);
scanf("%d", &s1.m[j]);
}
printf("\nStudent details:");
printf("\nRoll no\t: %d", [Link]);
printf("\nName\t: %s", [Link]);
printf("\nAddress\t: %s", [Link]);
for(int j=0;j<5;j++)
{
printf("\nMarks %d : ", j+1);
printf("%d", s1.m[j]);
}
Page 12
char add[100];
int m[5];
};
int main()
{
struct student s1; // where s1 is a instance of structure student
int total=0;
printf("Enter student details :");
printf("\nEnter Roll number: ");
scanf("%d", &[Link]);
printf("\nEnter Name: ");
fflush(stdin);
gets([Link]);
printf("\nEnter Addrss: ");
fflush(stdin);
gets([Link]);
printf("\nEnter Marks: ");
for(int j=0;j<5;j++)
{
printf("\nMarks %d : ", j+1);
scanf("%d", &s1.m[j]);
}
printf("\nStudent details:");
printf("\nRoll no\t: %d", [Link]);
printf("\nName\t: %s", [Link]);
printf("\nAddress\t: %s", [Link]);
for(int j=0;j<5;j++)
{
printf("\nMarks %d : ", j+1);
Page 13
printf("%d", s1.m[j]);
total = total+ s1.m[j];
}
printf("\nTotal\t: %d", total);
4. WAP which declares a structure named “Student” with rollno, name and
percentage as members. Read data of 5 students from user and display them in
proper format.
#include<stdio.h>
struct student
{
int rollno;
char name[100];
char add[100];
int m[5];
};
int main()
{
struct student s[5]; // where s is an array instance of structure student
int i;
for(i=0; i<5;i++)
{
printf("Enter student details :");
printf("\nEnter Roll number: ");
scanf("%d", &s[i].rollno);
printf("\nEnter Name: ");
fflush(stdin);
gets(s[i].name);
Page 14
printf("\nEnter Addrss: ");
fflush(stdin);
gets(s[i].add);
printf("\nEnter Marks: ");
for(int j=0;j<5;j++)
{
printf("\nMarks %d : ", j+1);
scanf("%d", &s[i].m[j]);
}
}
for(i=0;i<5;i++)
{
printf("\nStudent details:");
printf("\nRoll no\t: %d", s[i].rollno);
printf("\nName\t: %s", s[i].name);
printf("\nAddress\t: %s", s[i].add);
for(int j=0;j<5;j++)
{
printf("\nMarks %d : ", j+1);
printf("%d", s[i].m[j]);
}
}
}
Page 15
5. Write a C program to store information (Name, Roll No., and Marks) of n
students using structure and display in proper format
#include<stdio.h>
struct student
{
int rollno;
char name[100];
int marks;
};
int main()
{
int n;
printf(“Enter value of n - ”);
scanf(“%d”, &n);
struct student s[n]; // where s is an array instance of structure student
int i;
for(i=0; i<n;i++)
{
printf("Enter student details :");
printf("\nEnter Roll number: ");
scanf("%d", &s[i].rollno);
printf("\nEnter Name: ");
fflush(stdin);
gets(s[i].name);
printf("\nEnter Marks: ");
scanf("%d", &s[i].marks);
Page 16
for(i=0;i<n;i++)
{
printf("\nStudent details:");
printf("\nRoll no\t: %d", s[i].rollno);
printf("\nName\t: %s", s[i].name);
printf("\nMarks\t: %d", s[i].marks);
}
}
6. WAP using structure to enter rollno, marks of the three subjects for 3 student
and find total obtained by each student
#include<stdio.h>
struct student
{
int rollno;
char name[100];
char add[100];
int m[3];
};
int main()
{
struct student s[3]; // where s is an array instance of structure student
int i;
int total;
for(i=0; i<3;i++)
{
printf("Enter student details :");
printf("\nEnter Roll number: ");
scanf("%d", &s[i].rollno);
Page 17
printf("\nEnter Name: ");
fflush(stdin);
gets(s[i].name);
printf("\nEnter Addrss: ");
fflush(stdin);
gets(s[i].add);
printf("\nEnter Marks: ");
for(int j=0;j<3;j++)
{
printf("\nMarks %d : ", j+1);
scanf("%d", &s[i].m[j]);
}
}
for(i=0;i<3;i++)
{
printf("\nStudent details:");
printf("\nRoll no\t: %d", s[i].rollno);
printf("\nName\t: %s", s[i].name);
printf("\nAddress\t: %s", s[i].add);
total = 0;
for(int j=0;j<3;j++)
{
printf("\nMarks %d : ", j+1);
printf("%d", s[i].m[j]);
total = total + s[j].m[j];
}
printf("\nTotal\t: %d", total);
}
}
Page 18
7. Define structure data type called time_struct containing three member’s integer
hour, integer minute and integer second. Develop a program that would assign
values to the individual number and display the time in the following format: 16:
40:51
Method – 1:
#include<stdio.h>
struct time_struct
{
int h;
int m;
int s;
};
int main()
{
struct time_struct t1; // where s is an array instance of structure
Method – 2:
Page 19
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
//Data input from user
printf("\nEnter the time in hour, minute and second format");
scanf("%d%d%d",&[Link],&[Link],&[Link]);
Page 20
else if([Link]<=9 && [Link]<=9 && [Link]>9)
printf("\n0%d:0%d:%d",[Link],[Link],[Link]);
else if([Link]<=9 && [Link]>9 && [Link]<=9)
printf("\n0%d:%d:0%d",[Link],[Link],[Link]);
else if([Link]>9 && [Link]<=9 && [Link]<=9)
printf("\n%d:0%d:0%d",[Link],[Link],[Link]);
else
printf("\n%d:%d:%d",[Link],[Link],[Link]);
}
}
8. Write structure that named book which consists of three data fields, namely
name, price and pages. How can we access structure variables?
#include<stdio.h>
struct book
{
char name[100];
float price;
int page;
};
int main()
{
struct book b;
Page 21
printf("\nEnter Book pages :");
scanf("%d", &[Link]);
9. Write a program using structure to store student marks details. Use a function to
calculate total and percentage of this student and display the same.
#include<stdio.h>
struct student
{
int rollno;
char name[100];
int m[3];
};
float per(int b)
{
return (float)b/3;
}
Page 22
int main()
{
struct student s[3]; // where s is an array instance of structure student
int i;
for(i=0; i<3;i++)
{
printf("Enter student details :");
printf("\nEnter Roll number: ");
scanf("%d", &s[i].rollno);
printf("\nEnter Name: ");
fflush(stdin);
gets(s[i].name);
printf("\nEnter Marks: ");
for(int j=0;j<3;j++)
{
printf("\nMarks %d : ", j+1);
scanf("%d", &s[i].m[j]);
}
}
for(i=0;i<3;i++)
{
printf("\nStudent details:");
printf("\nRoll no\t: %d", s[i].rollno);
printf("\nName\t: %s", s[i].name);
printf("\nTotal %d : ", total(s[i]));
printf("\nResutl %.2f : ", per(total(s[i])));
}
}
Page 23
10. Write a program that demonstrate the Nested structure.
#include<stdio.h>
struct Address{
char Apartment[500];
char Street[500];
char City[100];
char State[100];
int Zipcode;
};
int main(){
//Declaring variable for For loop//
int i;
//Reading User I/p//
for (i=1;i<3;i++){//Declaring function to accept 2 people's data//
printf("Enter the Name of person %d : ",i);
gets(p[i].Name);
printf("Enter the Age of person %d : ",i);
scanf("%d",&p[i].Age);
scanf("%c",&p[i].temp);//Clearing Buffer//
Page 24
printf("Enter the Gender of person %d : ",i);
scanf("%c",&p[i].Gender);
scanf("%c",&p[i].temp);//Clearing Buffer//
printf("Enter the City of person %d : ",i);
gets(p[i].a[i].City);
printf("Enter the State of person %d : ",i);
gets(p[i].a[i].State);
printf("Enter the Zip Code of person %d : ",i);
scanf("%d",&p[i].a[i].Zipcode);
scanf("%c",&p[i].temp);//Clearing Buffer//
}
//Printing O/p//
for (i=1;i<3;i++){
printf("The Name of person %d is : %s\n",i,p[i].Name);
printf("The Age of person %d is : %d\n",i,p[i].Age);
printf("The Gender of person %d is : %c\n",i,p[i].Gender);
printf("The City of person %d is : %s\n",i,p[i].a[i].City);
printf("The State of person %d is : %s\n",i,p[i].a[i].State);
printf("The Zip code of person %d is : %d\n",i,p[i].a[i].Zipcode);
}
}
Method 2:
#include<stdio.h>
//Declaring outer and inter structures//
struct Person//Main Structure//{
char Name[500];
int Age;
char Gender;
char temp;//To clear buffer//
Page 25
struct Address//Nested Structure//{
char Apartment[500];
char Street[500];
char City[100];
char State[100];
int Zipcode;
}a[20];//Nested Structure Variable//
}p[20];//Main Structure Variable//
void main(){
//Declaring variable for For loop//
int i;
//Reading User I/p//
for (i=1;i<3;i++){//Declaring function to accept 2 people's data//
printf("Enter the Name of person %d : ",i);
gets(p[i].Name);
printf("Enter the Age of person %d : ",i);
scanf("%d",&p[i].Age);
scanf("%c",&p[i].temp);//Clearing Buffer//
printf("Enter the Gender of person %d : ",i);
scanf("%c",&p[i].Gender);
scanf("%c",&p[i].temp);//Clearing Buffer//
printf("Enter the City of person %d : ",i);
gets(p[i].a[i].City);
printf("Enter the State of person %d : ",i);
gets(p[i].a[i].State);
printf("Enter the Zip Code of person %d : ",i);
scanf("%d",&p[i].a[i].Zipcode);
scanf("%c",&p[i].temp);//Clearing Buffer//
}
//Printing O/p//
Page 26
for (i=1;i<3;i++){
printf("The Name of person %d is : %s\n",i,p[i].Name);
printf("The Age of person %d is : %d\n",i,p[i].Age);
printf("The Gender of person %d is : %c\n",i,p[i].Gender);
printf("The City of person %d is : %s\n",i,p[i].a[i].City);
printf("The State of person %d is : %s\n",i,p[i].a[i].State);
printf("The Zip code of person %d is : %d\n",i,p[i].a[i].Zipcode);
}
}
Page 27
Some advance practical of structures
1. C Program to Add Two Distances (in inch-feet system) using Structures.
#include <stdio.h>
struct Distance {
int feet;
float inch;
} d1, d2, result;
int main() {
// take first distance input
printf("Enter 1st distance\n");
printf("Enter feet: ");
scanf("%d", &[Link]);
printf("Enter inch: ");
scanf("%f", &[Link]);
// adding distances
[Link] = [Link] + [Link];
[Link] = [Link] + [Link];
Page 28
printf("\nSum of distances = %d\'-%.1f\"", [Link], [Link]);
return 0;
}
Output:
Enter 1st distance
Enter feet: 23
Enter inch: 8.6
Page 29
result = add(n1, n2);
printf("Sum = %.1f + %.1fi", [Link], [Link]);
return 0;
}
int main()
Page 30
{
struct Time difference(struct Time, struct Time);
struct Time start,stop,diff;
printf("Enter Start time hr min sec : ");
scanf("%d%d%d",&[Link],&[Link],&[Link]);
diff=difference(start,stop);
printf("Difference : %d :%d : %d\n",[Link],[Link],[Link]);
}
struct Time difference(struct Time a, struct Time b)
{
struct Time diff;
while([Link]>[Link])
{
--[Link];
[Link]+=60;
}
while([Link]>[Link])
{
--[Link];
[Link]+=60;
}
printf("Start Time : %d : %d : %d\n",[Link],[Link],[Link]);
printf("Stop Time : %d : %d : %d\n",[Link],[Link],[Link]);
[Link]=[Link] - [Link];
[Link]=[Link] - [Link];
Page 31
[Link]=[Link] - [Link];
return diff;
}
Output:
Enter the start time.
Enter hours, minutes and seconds: 13
34
55
Enter the stop time.
Enter hours, minutes and seconds: 8
12
15
Time Difference: 13:34:55 - 8:12:15 = 5:22:40
int main()
{
Page 32
struct Rectangle rec;
printf("Enter width : ");
scanf("%d", &[Link]);
printf("Enter height : ");
scanf("%d", &[Link]);
areaOfRectangle(rec);
}
5. Consider there are two structures Employee (depended structure) and another
structure called Organisation (Outer structure).
The structure Organisation has the data members like
organisation_name,organisation_number.
The Employee structure is nested inside the structure Organisation and it has the data
members like employee_id, name, salary.
Display the details of organization with its employees using nested stricture.
Method 1:
// C program to implement
// the above approach
#include <stdio.h>
#include <string.h>
// Declaration of the
// dependent structure
struct Employee
{
int employee_id;
char name[20];
int salary;
};
// Declaration of the
Page 33
// Outer structure
struct Organisation
{
char organisation_name[20];
char org_number[20];
// Driver code
int main()
{
// Structure variable
struct Organisation org;
Page 34
printf("Employee id : %d\n", [Link].employee_id);
printf("Employee name : %s\n", [Link]);
printf("Employee Salary : %d\n", [Link]);
}
Output:
The size of structure organisation : 68
Organisation Name : LJU
Organisation Number : GFG123768
Employee id : 101
Employee name : Robert
Employee Salary : 400000
Method 2:
// C program to implement
// the above approach
#include <stdio.h>
#include <string.h>
Page 35
{
int employee_id;
char name[20];
int salary;
// Driver code
int main()
{
struct Organisation org;
Page 36
Output:
The size of structure organisation : 68
Organisation Name : LJU
Organisation Number : GFG123768
Employee id : 101
Employee name : Robert
Employee Salary : 400000
Method 3:
Passing nested structure to function - Pass the nested structure variable at once.
// C program to implement
// the above approach
#include <stdio.h>
// Declaration of the inner
// structure
struct Employee
{
int employee_id;
char name[20];
int salary;
};
Page 37
// Nested structure
struct Employee emp;
};
// Driver code
int main()
{
struct Organisation org = {"LJU", "GFG111", {278, "Paul",5000}};
// Organisation structure variable
// is passed to function show
show(org);
}
Page 38
Output:
Printing the Details :
Organisation Name : LJU
Organisation Number : GFG111
Employee id : 278
Employee name : Paul
Employee Salary : 5000
Method 4: - Pass the nested structure members as arguments into the function:
// C program to implement
// the above approach
#include <stdio.h>
// Declaration of the inner
// structure
struct Employee
{
int employee_id;
char name[20];
int salary;
};
// Declaration of the Outer
// structure
struct Organisation
{
char organisation_name[20];
char org_number[20];
// Nested structure
struct Employee emp;
};
// Function show is expecting
Page 39
// members of both structures
void show(char organisation_name[],
char org_number[],
int employee_id,
char name[], int salary);
// Driver code
int main()
{
struct Organisation org = {"LJU", "GFG111", {278, "Paul",5000}};
// Data members of both the structures
// are passed to the function show
show(org.organisation_name, org.org_number, [Link].employee_id, [Link],
[Link]);
}
Page 40
Output:
Printing the Details :
Organisation Name : LJU
Organisation Number : GFG111
Employee id : 278
Employee name : Paul
Employee Salary : 5000
6. Write a Program to create Student Structure and accept name, roll no and marks of 3
subjects and print it in descending order of their total result. (Note: One can also
modify it by taking roll number as key and print student name in ascending order roll
number wise).
#include<stdio.h>
#include<conio.h>
struct student
{
char name[30];
int rollno;
int sub[3];
int total;
};
int main()
{
int i, n, j;
struct student st[20], temp;
printf("Enter number of students data you want to enter:\n");
scanf("%d",&n);
for(i=0;i < n;i++)
{
printf("Enter name of student %d\n",(i+1));
scanf("%s",&st[i].name);
printf("Enter Roll No of student %d\n",(i+1));
scanf("%d",&st[i].rollno);
printf("Enter marks for 3 subjects of student %d\n",(i+1));
scanf("%d%d%d",&st[i].sub[0],&st[i].sub[1],&st[i].sub[2]);
st[i].total = (st[i].sub[0]+st[i].sub[1]+st[i].sub[2]);
printf("Total Marks of %d student = %d\n",(i+1), st[i].total);
}
for(i=0;i < (n-1);i++)
{
for(j=0;j < (n-i-1);j++)
Page 41
Qb-1
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
char name[20];
char addr[50];
int marks[5]; //array
};
void main()
{
struct student stu; //structure variable declare
int i,sum=0;
printf("Enter roll no: \n");
scanf("%d",&[Link]);
fflush(stdin);
printf("Enter name: \n");
gets([Link]);
printf("Enter address: \n");
fflush(stdin);
gets([Link]);
printf("Enter 5 subjects marks \n");
for(i=0;i<5;i++)
{
scanf("%d",&[Link][i]);
sum=sum+[Link][i];
}
printf("Entered details are as below:\n");
printf("%d\n",[Link]);
printf("%s\n",[Link]);
printf("%s\n",[Link]);
for(i=0;i<5;i++)
{
printf("%d\n",[Link][i]);
}
printf("Total is %d\n",sum);
}
QB-2 Program for Storing information
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];
int main() {
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 10; i++) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 10; i++) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}
QB-3 Program for Storing information
#include <stdio.h>
struct student {
char firstName[50];
int roll;
float per;
} s[5];
int main() {
int i;
printf("Enter information of students:\n");
// storing information
for (i = 0; i < 5; i++) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter Percentage: ");
scanf("%f", &s[i].per);
}
printf("Displaying Information:\n\n");
// displaying information
for (i = 0; i < 5; i++) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Percentage: %.1f", s[i].per);
printf("\n");
}
return 0;
}
Qb-4 Program to create Student Structure and accept name, roll no and marks of 3
subjects and print in descending order
#include<stdio.h>
#include<conio.h>
struct student
{
char name[30];
int rollno;
int sub[3];
int total;
};
void main()
{
int i, n, j;
struct student st[20], temp;
printf("Enter number of students data you want to enter:\n");
scanf("%d",&n);
for(i=0;i < n;i++)
{
printf("Enter name of student %d\n",(i+1));
scanf("%s",&st[i].name);
printf("Enter Roll No of student %d\n",(i+1));
scanf("%d",&st[i].rollno);
printf("Enter marks for 3 subjects of student %d\n",(i+1));
scanf("%d%d%d",&st[i].sub[0],&st[i].sub[1],&st[i].sub[2]);
st[i].total = (st[i].sub[0]+st[i].sub[1]+st[i].sub[2]);
printf("Total Marks of %d student = %d\n",(i+1), st[i].total);
}
for(i=0;i < (n-1);i++)
{
for(j=0;j < (n-i-1);j++)
{
if(st[j].total < st[j+1].total)
{
temp = st[j];
st[j] = st[j+1];
st[j+1] = temp;
}
}
}
printf("\n\n\n\t\t******Sorted in descending order******");
for(i=0; i < n;i++)
{
printf("\nName of student: %s",st[i].name);
printf("\nRoll No of student: %d",st[i].rollno);
printf("\nTotal of student: %d\n",st[i].total);
}
getch();
}
Qb-5- 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
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
}sal;
int main (void)
{
//Calculate HRA
[Link] = [Link] * 15.0 / 100.0;
//Calculate PF
[Link] = sal.gross_salary * 10.0 / 100.0;
//Print result
printf("Basic Salary: %.2f\n", [Link]);
printf("DA: %.2f\n", [Link]);
printf("HRA: %.2f\n", [Link]);
printf("Gross Salary: %.2f\n", sal.gross_salary);
printf("PF: %.2f\n", [Link]);
printf("Net Salary: %.2f", sal.net_salary);
return 0;
}
Qb-7
#include<stdio.h>
struct student
{
char name[50];
int sub1;
int sub2;
int sub3,total;
float per;
};
void display(struct student s1);
void main()
{
struct student s1;
// function prototype
[Link]=[Link]/3;
printf("\nPercentage: %f", [Link]);
}
{
if(st[j].total < st[j+1].total)
{
temp = st[j]; st[j] = st[j+1]; st[j+1] = temp;
}
}
}
printf("\n\n\n\t\t******Sorted in descending order******");for(i=0; i < n;i++)
{
printf("\nName of student: %s",st[i].name); printf("\nRoll No of student:
%d",st[i].rollno);printf("\nTotal of student: %d\n",st[i].total);
}
Try yourself:
7. Define a struct to store name, array marks[5] and a character grade. Write a program to display the
details of student whose name is entered by user.
9. Declare a structure Point. Input the coordinates of a point variable and determine the quadrant in which it lies.
Q1 --> x=positive y=positive Q2 --> x=negative
y=positive Q3 --> x=negative y=negative Q4 -->
x=positive y=negative
Page 42
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner