Introduction
An array of structs in C++ allows you to manage and manipulate collections of related data in an organized way. By defining a struct and then creating an array of that struct, you can efficiently handle multiple records, such as student information, employee details, etc.
Defining a Struct and Creating an Array of Structs
To define an array of structs, you first define a struct with the necessary members, and then create an array of that struct type.
Example: Defining a Struct and Creating an Array
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
double grade;
};
int main() {
// Create an array of Student structs
Student students[3] = {
{"Aarav", 20, 88.5},
{"Siya", 21, 92.0},
{"Rohan", 19, 75.0}
};
// Print student data
for (int i = 0; i < 3; i++) {
cout << "Student " << i + 1 << ": " << students[i].name
<< ", Age: " << students[i].age
<< ", Grade: " << students[i].grade << endl;
}
return 0;
}
Output
Student 1: Aarav, Age: 20, Grade: 88.5
Student 2: Siya, Age: 21, Grade: 92
Student 3: Rohan, Age: 19, Grade: 75
Explanation
- The
Studentstruct is defined withname,age, andgrademembers. - An array of
Studentstructs,students, is created and initialized with data for three students. - A loop iterates through the array, printing the details of each student.
Practical Examples
Example 1: Calculating the Average Grade
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
double grade;
};
double calculateAverageGrade(Student students[], int size) {
double total = 0;
for (int i = 0; i < size; i++) {
total += students[i].grade;
}
return total / size;
}
int main() {
Student students[3] = {
{"Aarav", 20, 88.5},
{"Siya", 21, 92.0},
{"Rohan", 19, 75.0}
};
int size = 3;
double averageGrade = calculateAverageGrade(students, size);
cout << "Average Grade: " << averageGrade << endl;
return 0;
}
Output
Average Grade: 85.1667
Explanation
- The
calculateAverageGradefunction takes an array ofStudentstructs and the array size as arguments. - It calculates and returns the average grade of the students.
- The
mainfunction initializes the array, calls thecalculateAverageGradefunction, and prints the average grade.
Example 2: Finding the Youngest Student
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
double grade;
};
int findYoungestStudent(Student students[], int size) {
int youngestIndex = 0;
for (int i = 1; i < size; i++) {
if (students[i].age < students[youngestIndex].age) {
youngestIndex = i;
}
}
return youngestIndex;
}
int main() {
Student students[3] = {
{"Aarav", 20, 88.5},
{"Siya", 21, 92.0},
{"Rohan", 19, 75.0}
};
int size = 3;
int youngestIndex = findYoungestStudent(students, size);
cout << "Youngest Student: " << students[youngestIndex].name
<< ", Age: " << students[youngestIndex].age << endl;
return 0;
}
Output
Youngest Student: Rohan, Age: 19
Explanation
- The
findYoungestStudentfunction takes an array ofStudentstructs and the array size as arguments. - It finds and returns the index of the youngest student.
- The
mainfunction initializes the array, calls thefindYoungestStudentfunction, and prints the details of the youngest student.
Example 3: Sorting Students by Grade
#include <iostream>
#include <algorithm>
using namespace std;
struct Student {
string name;
int age;
double grade;
};
// Comparator function to sort students by grade
bool compareGrades(Student a, Student b) {
return a.grade > b.grade; // Sort in descending order
}
int main() {
Student students[3] = {
{"Aarav", 20, 88.5},
{"Siya", 21, 92.0},
{"Rohan", 19, 75.0}
};
int size = 3;
// Sort the students by grade
sort(students, students + size, compareGrades);
// Print sorted student data
for (int i = 0; i < size; i++) {
cout << "Student " << i + 1 << ": " << students[i].name
<< ", Age: " << students[i].age
<< ", Grade: " << students[i].grade << endl;
}
return 0;
}
Output
Student 1: Siya, Age: 21, Grade: 92
Student 2: Aarav, Age: 20, Grade: 88.5
Student 3: Rohan, Age: 19, Grade: 75
Explanation
- The
compareGradesfunction is a comparator function used to sort students by their grades in descending order. - The
sortfunction from the<algorithm>header sorts the array of students using thecompareGradesfunction. - The sorted array is printed, showing students ordered by their grades.
Conclusion
An array of structs in C++ provides a structured way to manage and manipulate collections of related data. This chapter covered the basics of defining a struct and creating an array of that struct type, along with practical examples demonstrating how to calculate the average grade, find the youngest student, and sort students by grade. Understanding how to use arrays of structs effectively is essential for organizing and processing complex data in C++ programs.