frostblooded

Untitled

Dec 10th, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.42 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define STUDENTS_COUNT 3
  5. #define SUBJECTS_COUNT 6
  6.  
  7. struct student {
  8.     int id;
  9.     char name[20];
  10.     float scores[SUBJECTS_COUNT];
  11. };
  12.  
  13. struct student students[STUDENTS_COUNT];
  14.  
  15. int score_of_student(int student_id, int subject_id) {
  16.     int i;
  17.     for(i = 0; i < STUDENTS_COUNT; i++) {
  18.         if(students[i].id == student_id) {
  19.             return students[i].scores[subject_id];
  20.         }
  21.     }
  22.    
  23.     return -1;
  24. }
  25.  
  26. float average_subject_score(int subject_id) {
  27.     int score_sum = 0;
  28.    
  29.     int i;
  30.     for(i = 0; i < STUDENTS_COUNT; i++) {
  31.         score_sum += students[i].scores[subject_id];
  32.     }
  33.    
  34.     return score_sum / STUDENTS_COUNT;
  35. }
  36.  
  37. float average_of_all_subjects() {
  38.     float averages_sum = 0;
  39.    
  40.     int i;
  41.     for(i = 0; i < SUBJECTS_COUNT; i++) {
  42.         averages_sum += average_subject_score(i);
  43.     }
  44.    
  45.     return averages_sum / SUBJECTS_COUNT;
  46. }
  47.  
  48. int main() {
  49.     int i;
  50.     for(i = 0; i < STUDENTS_COUNT; i++) {
  51.         students[i].id = i + 1;
  52.         scanf("%s\n", &students[i].name);
  53.        
  54.         int j;
  55.         for(j = 0; j < 6; j++) {
  56.             students[i].scores[j] = (i + j) % 11;
  57.             // scanf("%d\n", &students[i].scores[j]);
  58.         }
  59.     }
  60.  
  61.     printf("%d\n", score_of_student(2, 3));
  62.     printf("%f\n", average_subject_score(4));
  63.     printf("%f\n", average_of_all_subjects());
  64.  
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment