Practical / Solution
Array of Structures — Exercise 1
Problem
Write a C program to store and display the details of 3 students using
an array of structures.
Program
#include <stdio.h>
struct Student
{
int roll;
char name[50];
float marks;
};
int main()
{
struct Student s[3];
int i;
printf("Enter details of 3 students:\n");
for (i = 0; i < 3; i++)
{
printf("\nStudent %d\n", i + 1);
printf("Enter roll number: ");
scanf("%d", &s[i].roll);
printf("Enter name: ");
scanf("%49s", s[i].name);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("\nStudent Records\n");
for (i = 0; i < 3; i++)
{
printf("\nRoll = %d\n", s[i].roll);
printf("Name = %s\n", s[i].name);
printf("Marks = %.2f\n", s[i].marks);
}
return 0;
}
Explanation
An array of structures stores multiple structure variables of the same
type. Here, s[3] stores the records of three students.
Expected Output
Student Records
Roll = 101, Name = Rahul, Marks = 82.00
Roll = 102, Name = Priya, Marks = 91.00
Roll = 103, Name = Amit, Marks = 76.00
Array of Structures — Exercise 2
Problem
Write a C program to store the details of 5 employees using an array
of structures and display all employee records.
Program
#include <stdio.h>
struct Employee
{
int id;
char name[50];
float salary;
};
int main()
{
struct Employee e[5];
int i;
for (i = 0; i < 5; i++)
{
printf("\nEmployee %d\n", i + 1);
printf("Enter ID: ");
scanf("%d", &e[i].id);
printf("Enter name: ");
scanf("%49s", e[i].name);
printf("Enter salary: ");
scanf("%f", &e[i].salary);
}
printf("\nEmployee Records\n");
for (i = 0; i < 5; i++)
{
printf("\nID = %d\n", e[i].id);
printf("Name = %s\n", e[i].name);
printf("Salary = %.2f\n", e[i].salary);
}
return 0;
}
Explanation
The declaration struct Employee e[5] creates an array
containing five employee structure records.
Expected Output
ID = 501, Name = Amit, Salary = 45000.00
ID = 502, Name = Neha, Salary = 52000.00
ID = 503, Name = Ravi, Salary = 48000.00
ID = 504, Name = Pooja, Salary = 60000.00
ID = 505, Name = Karan, Salary = 55000.00
Array of Structures — Exercise 3
Problem
Write a C program to find the student who has obtained the highest
marks from an array of 5 students.
Program
#include <stdio.h>
struct Student
{
int roll;
char name[50];
float marks;
};
int main()
{
struct Student s[5];
int i;
int highestIndex = 0;
for (i = 0; i < 5; i++)
{
printf("Enter roll, name and marks for student %d: ",
i + 1);
scanf("%d %49s %f",
&s[i].roll,
s[i].name,
&s[i].marks);
}
for (i = 1; i < 5; i++)
{
if (s[i].marks > s[highestIndex].marks)
{
highestIndex = i;
}
}
printf("\nStudent with highest marks:\n");
printf("Roll = %d\n", s[highestIndex].roll);
printf("Name = %s\n", s[highestIndex].name);
printf("Marks = %.2f\n", s[highestIndex].marks);
return 0;
}
Explanation
The program compares the marks member of each structure
and stores the index of the student having the highest marks.
Expected Output
Student with highest marks:
Roll = 103
Name = Priya
Marks = 94.00
Array of Structures — Exercise 4
Problem
Write a C program to find the employee having the lowest salary from
an array of employees.
Program
#include <stdio.h>
struct Employee
{
int id;
char name[50];
float salary;
};
int main()
{
struct Employee e[5];
int i;
int lowestIndex = 0;
for (i = 0; i < 5; i++)
{
printf("Enter ID, name and salary for employee %d: ",
i + 1);
scanf("%d %49s %f",
&e[i].id,
e[i].name,
&e[i].salary);
}
for (i = 1; i < 5; i++)
{
if (e[i].salary < e[lowestIndex].salary)
{
lowestIndex = i;
}
}
printf("\nEmployee with lowest salary:\n");
printf("ID = %d\n", e[lowestIndex].id);
printf("Name = %s\n", e[lowestIndex].name);
printf("Salary = %.2f\n", e[lowestIndex].salary);
return 0;
}
Explanation
Each employee record is stored in the array. The program compares the
salary member and keeps track of the employee having the minimum salary.
Expected Output
Employee with lowest salary:
ID = 503
Name = Ravi
Salary = 32000.00
Array of Structures — Exercise 5
Problem
Write a C program to calculate the total and average marks of 5
students using an array of structures.
Program
#include <stdio.h>
struct Student
{
int roll;
char name[50];
float marks;
};
int main()
{
struct Student s[5];
int i;
float total = 0;
float average;
for (i = 0; i < 5; i++)
{
printf("Enter roll, name and marks: ");
scanf("%d %49s %f",
&s[i].roll,
s[i].name,
&s[i].marks);
total += s[i].marks;
}
average = total / 5;
printf("\nTotal Marks = %.2f\n", total);
printf("Average Marks = %.2f\n", average);
return 0;
}
Explanation
The program accesses the marks member of every structure
element and adds the values to calculate the total and average.
Expected Output
Total Marks = 420.00
Average Marks = 84.00
Array of Structures — Exercise 6
Problem
Write a C program to search for an employee by employee ID in an
array of structures.
Program
#include <stdio.h>
struct Employee
{
int id;
char name[50];
float salary;
};
int main()
{
struct Employee e[5];
int i;
int searchId;
int found = 0;
for (i = 0; i < 5; i++)
{
printf("Enter ID, name and salary: ");
scanf("%d %49s %f",
&e[i].id,
e[i].name,
&e[i].salary);
}
printf("\nEnter employee ID to search: ");
scanf("%d", &searchId);
for (i = 0; i < 5; i++)
{
if (e[i].id == searchId)
{
printf("\nEmployee Found\n");
printf("ID = %d\n", e[i].id);
printf("Name = %s\n", e[i].name);
printf("Salary = %.2f\n", e[i].salary);
found = 1;
break;
}
}
if (!found)
{
printf("\nEmployee not found.\n");
}
return 0;
}
Explanation
The program checks the ID of each structure element against the
entered search ID. When a match is found, the employee details are displayed.
Expected Output
Enter employee ID to search: 503
Employee Found
ID = 503
Name = Ravi
Salary = 48000.00
Array of Structures — Exercise 7
Problem
Write a C program to search for a student using roll number from an
array of structures and display the student's marks.
Program
#include <stdio.h>
struct Student
{
int roll;
char name[50];
float marks;
};
int main()
{
struct Student s[5];
int i;
int roll;
int found = 0;
for (i = 0; i < 5; i++)
{
printf("Enter roll, name and marks: ");
scanf("%d %49s %f",
&s[i].roll,
s[i].name,
&s[i].marks);
}
printf("\nEnter roll number to search: ");
scanf("%d", &roll);
for (i = 0; i < 5; i++)
{
if (s[i].roll == roll)
{
printf("\nStudent Found\n");
printf("Name = %s\n", s[i].name);
printf("Marks = %.2f\n", s[i].marks);
found = 1;
break;
}
}
if (!found)
{
printf("\nStudent not found.\n");
}
return 0;
}
Explanation
The roll number is used as the search key. The program checks every
structure in the array until a matching roll number is found.
Expected Output
Enter roll number to search: 102
Student Found
Name = Priya
Marks = 91.00
Array of Structures — Exercise 8
Problem
Write a C program to sort 5 students in descending order according
to their marks using an array of structures.
Program
#include <stdio.h>
struct Student
{
int roll;
char name[50];
float marks;
};
int main()
{
struct Student s[5];
struct Student temp;
int i, j;
for (i = 0; i < 5; i++)
{
printf("Enter roll, name and marks: ");
scanf("%d %49s %f",
&s[i].roll,
s[i].name,
&s[i].marks);
}
// Sort students by marks
for (i = 0; i < 4; i++)
{
for (j = i + 1; j < 5; j++)
{
if (s[i].marks < s[j].marks)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
printf("\nStudents in descending order of marks:\n");
for (i = 0; i < 5; i++)
{
printf("%d %s %.2f\n",
s[i].roll,
s[i].name,
s[i].marks);
}
return 0;
}
Explanation
Structure variables can be exchanged just like ordinary variables.
Here, the complete student structure is swapped to sort students
according to marks.
Expected Output
Students in descending order of marks:
103 Priya 94.00
102 Rahul 88.00
105 Neha 84.00
101 Amit 78.00
104 Ravi 72.00
Array of Structures — Exercise 9
Problem
Write a C program to store details of 5 products and find the product
having the highest price.
Program
#include <stdio.h>
struct Product
{
int id;
char name[50];
float price;
};
int main()
{
struct Product p[5];
int i;
int highestIndex = 0;
for (i = 0; i < 5; i++)
{
printf("Enter product ID, name and price: ");
scanf("%d %49s %f",
&p[i].id,
p[i].name,
&p[i].price);
}
for (i = 1; i < 5; i++)
{
if (p[i].price > p[highestIndex].price)
{
highestIndex = i;
}
}
printf("\nProduct with highest price:\n");
printf("ID = %d\n", p[highestIndex].id);
printf("Name = %s\n", p[highestIndex].name);
printf("Price = %.2f\n", p[highestIndex].price);
return 0;
}
Explanation
The program compares the price of every product and stores the index
of the product with the maximum price.
Expected Output
Product with highest price:
ID = 205
Name = Laptop
Price = 65000.00
Array of Structures — Exercise 10
Problem
Write a C program to create a simple student record system using an
array of structures. Store student details, display all records and
calculate the highest marks.
Program
#include <stdio.h>
struct Student
{
int roll;
char name[50];
float marks;
};
int main()
{
struct Student s[5];
int i;
int highestIndex = 0;
for (i = 0; i < 5; i++)
{
printf("\nEnter details of student %d\n", i + 1);
printf("Roll number: ");
scanf("%d", &s[i].roll);
printf("Name: ");
scanf("%49s", s[i].name);
printf("Marks: ");
scanf("%f", &s[i].marks);
}
/* Find highest marks */
for (i = 1; i < 5; i++)
{
if (s[i].marks > s[highestIndex].marks)
{
highestIndex = i;
}
}
printf("\n==============================\n");
printf(" STUDENT RECORDS\n");
printf("==============================\n");
for (i = 0; i < 5; i++)
{
printf("Roll : %d\n", s[i].roll);
printf("Name : %s\n", s[i].name);
printf("Marks : %.2f\n", s[i].marks);
printf("------------------------------\n");
}
printf("\nTop Student\n");
printf("Name : %s\n", s[highestIndex].name);
printf("Marks : %.2f\n", s[highestIndex].marks);
return 0;
}
Explanation
This example combines the main operations performed on an array of
structures: storing records, displaying records, traversing the array
and finding the student with the highest marks.
Expected Output
==============================
STUDENT RECORDS
==============================
Roll : 101 | Name : Amit | Marks : 78.00
Roll : 102 | Name : Rahul | Marks : 88.00
Roll : 103 | Name : Priya | Marks : 94.00
Roll : 104 | Name : Ravi | Marks : 72.00
Roll : 105 | Name : Neha | Marks : 84.00
Top Student
Name : Priya
Marks : 94.00