fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // Nested Structure Definitions
  6. struct Course {
  7. string courseName;
  8. int marks;
  9. };
  10.  
  11. struct Student {
  12. int id;
  13. string name;
  14. Course courses[3]; // Array of courses
  15. };
  16.  
  17. // Function Prototypes
  18. void inputStudentDetails(Student &student);
  19. void displayStudentDetails(const Student &student);
  20. float calculateAverageMarks(const Student &student);
  21.  
  22. int main() {
  23. const int numStudents = 2; // Number of students
  24. Student students[numStudents];
  25.  
  26. // Input details for each student
  27. for (int i = 0; i < numStudents; i++) {
  28. cout << "\nEntering details for Student " << i + 1 << ":\n";
  29. inputStudentDetails(students[i]);
  30. }
  31.  
  32. // Display details for each student
  33. for (int i = 0; i < numStudents; i++) {
  34. cout << "\nDetails for Student " << i + 1 << ":\n";
  35. displayStudentDetails(students[i]);
  36. }
  37.  
  38. return 0;
  39. }
  40.  
  41. // Function to input details for a single student
  42. void inputStudentDetails(Student &student) {
  43. cout << "Enter Student ID: ";
  44. cin >> student.id;
  45. cin.ignore(); // Ignore trailing newline
  46.  
  47. cout << "Enter Student Name: ";
  48. getline(cin, student.name);
  49.  
  50. for (int i = 0; i < 3; i++) {
  51. cout << "\nEnter details for Course " << i + 1 << ":\n";
  52. cout << "Course Name: ";
  53. getline(cin, student.courses[i].courseName);
  54. cout << "Marks: ";
  55. cin >> student.courses[i].marks;
  56. cin.ignore(); // Ignore trailing newline
  57. }
  58. }
  59.  
  60. // Function to display details for a single student
  61. void displayStudentDetails(const Student &student) {
  62. cout << "Student ID: " << student.id << endl;
  63. cout << "Student Name: " << student.name << endl;
  64.  
  65. cout << "Courses:\n";
  66. for (int i = 0; i < 3; i++) {
  67. cout << " " << student.courses[i].courseName << " - Marks: " << student.courses[i].marks << endl;
  68. }
  69.  
  70. float average = calculateAverageMarks(student);
  71. cout << "Average Marks: " << average << endl;
  72. }
  73.  
  74. // Function to calculate the average marks of a student
  75. float calculateAverageMarks(const Student &student) {
  76. int totalMarks = 0;
  77. for (int i = 0; i < 3; i++) {
  78. totalMarks += student.courses[i].marks;
  79. }
  80. return static_cast<float>(totalMarks) / 3; // Return average marks
  81. }
  82.  
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
Entering details for Student 1:
Enter Student ID: Enter Student Name: 
Enter details for Course 1:
Course Name: Marks: 
Enter details for Course 2:
Course Name: Marks: 
Enter details for Course 3:
Course Name: Marks: 
Entering details for Student 2:
Enter Student ID: Enter Student Name: 
Enter details for Course 1:
Course Name: Marks: 
Enter details for Course 2:
Course Name: Marks: 
Enter details for Course 3:
Course Name: Marks: 
Details for Student 1:
Student ID: 0
Student Name: 
Courses:
   - Marks: -1156709568
   - Marks: 0
   - Marks: 187955147
Average Marks: -3.22918e+08

Details for Student 2:
Student ID: 188579048
Student Name: 
Courses:
   - Marks: 187368952
   - Marks: -1195687928
   - Marks: -1195697339
Average Marks: 6.96984e+08