Advertisement
Griwin

Exercise

Oct 5th, 2023 (edited)
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | Software | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Student {
  5. public:
  6.     string name;
  7.     double grade;
  8.     int age;
  9.     Student(string n, double g, int a) : name(n), grade(g), age(a) {}
  10.  
  11. };
  12.  
  13.  
  14.  
  15. void averageAge(Student arr[], int size) {
  16.     int sum = 0;
  17.    
  18.     for (int i = 0; i < size; i++) {
  19.         sum = sum + arr[i].age;
  20.     }
  21.     int result = sum / size;
  22.     cout << "The avg age is:"<< result <<endl;
  23. }
  24.  
  25. int main() {
  26.     Student st1("Chefo", 5, 17);
  27.     Student st2("Georgi", 3, 17);
  28.     Student st3("Atanas", 6, 16);
  29.     Student st4("Stefan", 4, 17);
  30.     Student st5("Ivan", 5, 16);
  31.  
  32.     Student students[5] = { st1, st2, st3 , st4, st5 };
  33.     int size = sizeof(students) / sizeof(students[0]);
  34.  
  35.     averageAge(students, size);
  36.    
  37.     return 0;
  38. }
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement