#include <iostream>
#include <string>
using namespace std;
// Nested Structure Definitions
struct Course {
string courseName;
int marks;
};
struct Student {
int id;
string name;
Course courses[3]; // Array of courses
};
// Function Prototypes
void inputStudentDetails(Student &student);
void displayStudentDetails(const Student &student);
float calculateAverageMarks(const Student &student);
int main() {
const int numStudents = 2; // Number of students
Student students[numStudents];
// Input details for each student
for (int i = 0; i < numStudents; i++) {
cout << "\nEntering details for Student " << i + 1 << ":\n";
inputStudentDetails(students[i]);
}
// Display details for each student
for (int i = 0; i < numStudents; i++) {
cout << "\nDetails for Student " << i + 1 << ":\n";
displayStudentDetails(students[i]);
}
return 0;
}
// Function to input details for a single student
void inputStudentDetails(Student &student) {
cout << "Enter Student ID: ";
cin >> student.id;
cin.ignore(); // Ignore trailing newline
cout << "Enter Student Name: ";
getline(cin, student.name);
for (int i = 0; i < 3; i++) {
cout << "\nEnter details for Course " << i + 1 << ":\n";
cout << "Course Name: ";
getline(cin, student.courses[i].courseName);
cout << "Marks: ";
cin >> student.courses[i].marks;
cin.ignore(); // Ignore trailing newline
}
}
// Function to display details for a single student
void displayStudentDetails(const Student &student) {
cout << "Student ID: " << student.id << endl;
cout << "Student Name: " << student.name << endl;
cout << "Courses:\n";
for (int i = 0; i < 3; i++) {
cout << " " << student.courses[i].courseName << " - Marks: " << student.courses[i].marks << endl;
}
float average = calculateAverageMarks(student);
cout << "Average Marks: " << average << endl;
}
// Function to calculate the average marks of a student
float calculateAverageMarks(const Student &student) {
int totalMarks = 0;
for (int i = 0; i < 3; i++) {
totalMarks += student.courses[i].marks;
}
return static_cast<float>(totalMarks) / 3; // Return average marks
}