Advertisement
Guest User

Exercise 1 OOP 1

a guest
Feb 20th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int NUMBER_OF_STUDENTS = 2;
  4. const int NUMBER_OF_GRADES = 5;
  5.  
  6. struct Student {
  7. char name[30];
  8. char surname[30];
  9. char egn[11];
  10. int fn;
  11. double grades[NUMBER_OF_GRADES];
  12. };
  13.  
  14. Student getNew()
  15. {
  16. Student newStudent;
  17. cout << "Enter name of new student:";
  18. cin >> newStudent.name;
  19. cout << "Enter surname of new student:";
  20. cin >> newStudent.surname;
  21. cout << "Enter egn of new student:";
  22. cin >> newStudent.egn;
  23. cout << "Enter faculty number of new student:";
  24. cin >> newStudent.fn;
  25. cout << "Enter grades of new student:";
  26. for (int i = 0; i < 5; i++)
  27. {
  28. cin >> newStudent.grades[i];
  29. }
  30. return newStudent;
  31. }
  32.  
  33. Student getStudentWithHighestGrades(Student students[NUMBER_OF_STUDENTS])
  34. {
  35. Student highestAchievingStudent;
  36. double highestGrade = 0;
  37. for (int i = 0; i < NUMBER_OF_STUDENTS; i++)
  38. {
  39. double averageGrade = 0;
  40. for (int j = 0; j < NUMBER_OF_GRADES; j++)
  41. {
  42. averageGrade += students[i].grades[j];
  43. }
  44. averageGrade /= NUMBER_OF_GRADES;
  45. if (highestGrade < averageGrade)
  46. {
  47. highestGrade = averageGrade;
  48. highestAchievingStudent = students[i];
  49. }
  50. }
  51. return highestAchievingStudent;
  52. }
  53.  
  54. void printStudent(Student student)
  55. {
  56. cout << student.name << endl;
  57. cout << student.surname << endl;
  58. cout << student.egn << endl;
  59. cout << student.fn << endl;
  60. for (int j = 0; j < NUMBER_OF_GRADES; j++)
  61. {
  62. cout << student.grades[j] << endl;
  63. }
  64. }
  65.  
  66.  
  67. void printStudentsBornInMarch(Student students[NUMBER_OF_STUDENTS])
  68. {
  69. for (int i = 0; i < NUMBER_OF_STUDENTS; i++)
  70. {
  71. if (students[i].egn[3] == '3')
  72. {
  73. printStudent(students[i]);
  74. }
  75. }
  76.  
  77. }
  78.  
  79.  
  80. int main()
  81. {
  82. Student students[NUMBER_OF_STUDENTS];
  83. for (int i = 0; i < NUMBER_OF_STUDENTS; i++)
  84. {
  85. students[i] = getNew();
  86. }
  87. char input[30];
  88. cout << "Do you want the student with the highest grade or the students born in march?" << endl;
  89. cin >> input;
  90. if (strcmp(input, "highest grade") == 0)
  91. {
  92. Student myBestStudent = getStudentWithHighestGrades(students);
  93. printStudent(myBestStudent);
  94. }
  95. else if(strcmp(input, "born march") == 0){
  96. printStudentsBornInMarch(students);
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement