Advertisement
tsounakis

C Assignments

Jun 24th, 2022 (edited)
980
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.40 KB | None
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define STUDENTS 20
  4. #define ASSIGNMENTS 5
  5.  
  6. void fillArrayRand(int grades[STUDENTS][ASSIGNMENTS])
  7. {
  8.     for(int i=0;i<STUDENTS;i++){
  9.         for(int j=0;j<ASSIGNMENTS;j++){
  10.             grades[i][j]=rand()%10-1;
  11.         }
  12.     }
  13. }
  14.  
  15. void fillArray(int grades[STUDENTS][ASSIGNMENTS])
  16. {
  17.     int grade = 0;
  18.     int j;
  19.     for(int i=0;i<STUDENTS;i++){
  20.         j = 0;
  21.         printf("\nStudent %d: Please fill the grades. (type number and press Enter.)\n", i+1);
  22.         while(j != ASSIGNMENTS){
  23.             printf("Please, type:\t");
  24.             scanf("%d", &grade);
  25.             if (grade >= -1 && grade <= 10){
  26.                 grades[i][j] = grade;
  27.                 printf("\tSuccess! Go on.\n");
  28.                 j++;
  29.             }
  30.             else {
  31.                 printf("\tGrade not valid! Repeat.\n");
  32.             }
  33.            
  34.         }
  35.     }
  36. }
  37.  
  38. void printArray(int grades[STUDENTS][ASSIGNMENTS])
  39. {
  40.     printf("Printing the formatted array ->\n");
  41.     printf("Students\t"); for(int i = 0;i<ASSIGNMENTS;i++) printf("Assignment #%d\t", i+1);
  42.     printf("\n");
  43.     for(int i=0;i<STUDENTS;i++){
  44.         printf("\t%d\t\t", i+1);
  45.         for(int j=0;j<ASSIGNMENTS;j++){
  46.             printf("%d\t\t", grades[i][j]);
  47.         }
  48.         printf("\n");
  49.     }
  50.     printf("===============================\n");
  51. }
  52.  
  53. void findMax(int grades[STUDENTS][ASSIGNMENTS])
  54. {
  55.     printf("Best grade of each student ->\n");
  56.     int max, pos;
  57.     printf("Students\tBest Grade\tAssignment (*)\n");
  58.     for(int i=0;i<STUDENTS;i++){
  59.         pos = 0;
  60.         printf("\t%d\t", i+1);
  61.         max = -2;
  62.         for(int j=0;j<ASSIGNMENTS;j++){
  63.             if (grades[i][j] > max)
  64.             {
  65.                 max = grades[i][j];
  66.                 pos = j + 1;
  67.             }
  68.         }
  69.         printf("\t%d\t\t%d\n", max, pos);
  70.     }
  71.     printf("(*The best grade was achieved at this assignment.)\n");
  72.     printf("===============================\n");
  73. }
  74.  
  75. void findMin(int grades[STUDENTS][ASSIGNMENTS])
  76. {
  77.     printf("Worst grade per assignment ->\n");
  78.     int min, pos;
  79.     printf("Assignments\tWorst Grade\tStudent (*)\n");
  80.     for(int j=0;j<ASSIGNMENTS;j++){
  81.         min = 10;
  82.         pos = 0;
  83.         printf("\t%d\t", j+1);
  84.         for(int i=0;i<STUDENTS;i++){
  85.             if (grades[i][j] <= min && grades[i][j] != -1)
  86.             {
  87.                 min = grades[i][j];
  88.                 pos = i + 1;
  89.             }
  90.         }
  91.         printf("\t%d\t\t%d\n", min, pos);
  92.     }
  93.     printf("(*The student whose grade was the worst at the specific assignment.)\n");
  94.     printf("===============================\n");
  95. }
  96.  
  97. void findMean(int grades[STUDENTS][ASSIGNMENTS])
  98. {
  99.     printf("Mean grade of each student ->\n");
  100.     double sum = 0, counter = 0;
  101.     double mean;
  102.     printf("Students\tMean Grade\n");
  103.     for(int i=0;i<STUDENTS;i++){
  104.         printf("\t%d\t", i+1);
  105.         for(int j=0;j<ASSIGNMENTS;j++){
  106.             if (grades[i][j] != -1)
  107.             {
  108.                 sum += grades[i][j];
  109.                 counter++;
  110.             }
  111.         }
  112.         mean = sum/counter;
  113.         printf("\t%f\n", mean);
  114.     }
  115.     printf("===============================\n");
  116. }
  117.  
  118. void checkIfEligible(int grades[STUDENTS][ASSIGNMENTS])
  119. {
  120.     int sum, counter;
  121.     printf("Eligibility for the exam ->\n");
  122.     printf("Students\tEligible\tNotes\n");
  123.     for(int i=0;i<STUDENTS;i++){
  124.         sum = 0;
  125.         counter = 0;
  126.         printf("\t%d\t", i+1);
  127.         for(int j=0;j<ASSIGNMENTS;j++){
  128.             if (grades[i][j] != -1)
  129.             {
  130.                 sum += grades[i][j];
  131.                 counter++;
  132.             }
  133.         }
  134.         (sum >= 25 && counter >=3) ? printf("Yes") : printf("No");
  135.         printf("\t\t(sum of grades = %d, submitted assignments = %d)\n", sum, counter);
  136.         /*if (sum >= 25 && counter >= 3)
  137.         {
  138.             printf("Yes")
  139.         }
  140.         else
  141.         {
  142.         printf("\n");
  143.         }*/
  144.     }
  145.     printf("===============================\n");
  146. }
  147.  
  148. int main()
  149. {
  150.     int grades[STUDENTS][ASSIGNMENTS];
  151.     fillArrayRand(grades); // γεμίζει τον 2Δ πίνακα με τυχαίους βαθμούς (-1 έως 10)
  152.     printArray(grades); // τυπώνει όμορφα τον πίνακα των βαθμών
  153.     findMax(grades);
  154.     findMin(grades);
  155.     findMean(grades);
  156.     checkIfEligible(grades);
  157.     return 0;
  158. }
Advertisement
RAW Paste Data Copied
Advertisement