Experiment-3: Array of Structures in C++
Aim of the Experiment:
To understand how to use arrays of structures in C++ by writing a program that stores, displays,
and processes data of multiple students (e.g., name, roll number, and marks).
Theory:
Structures in C++ allow the grouping of variables of different types under a single name. When
combined with arrays, structures enable the creation of a collection of records, making it easier
to handle multiple entities with similar data.
1. A structure is declared using the struct keyword, which groups related variables.
2. An array of structures is an array where each element is a structure.
3. Data for each structure element can be accessed using the dot operator (e.g.,
array[index].member).
Procedure:
1. Define the structure: Use the struct keyword to define a structure with relevant fields (e.g.,
name, roll number, and marks).
2. Declare an array of structures: Create an array where each element is of the structure type.
3. Input the number of records: Ask the user to specify the number of students whose data will be
entered.
4. Input data for each record:
o Use a loop to take inputs for each field of the structure for every student.
o Store the data in the corresponding element of the structure array.
5. Display data:
o Use a loop to traverse the array of structures and print the details of each student.
6. Perform additional processing:
o For example, calculate the total and average marks of all students.
7. Test the program: Run the program with different numbers of records and inputs.
8. Analyze the results: Ensure the data is correctly stored, displayed, and processed.
9. End the program: Ensure proper program termination and data integrity.
Result:
The program successfully demonstrates how to use an array of structures to store and manage
student records, display the data, and calculate the total and average marks.
The program organizes student data efficiently using an array of structures. Each student’s record
includes their name, roll number, and marks. After taking inputs, the program displays the data and
computes the total and average marks. For example, with three students, Alice, Bob, and Charlie, the
program calculates the total marks as 253.5 and the average marks as 84.5. This demonstrates a
practical application of structures in C++.