Idanref

Advanced C - 5.c

Mar 7th, 2021 (edited)
698
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. // Student Average - Student average of all tests
  6. // TOTAL AVERAGE - Average of all student averages
  7.  
  8. // Two groups:
  9. // 1. Average >= TOTAL AVERAGE
  10. // 2. Average < TOTAL AVERAGE
  11.  
  12. #define SIZE 3
  13. #define scanf scanf_s
  14.  
  15.  
  16. typedef struct student_init_data
  17. {
  18.     int student_id;
  19.     double* grades;
  20.     int num_of_tests; // total tests
  21. } Student_Data;
  22.  
  23.  
  24. typedef struct student_processed_data // each student data
  25. {
  26.     int id;
  27.     double student_average;
  28. } Processed_data;
  29.  
  30.  
  31. typedef struct stataistics
  32. {
  33.     Processed_data* data_arr_1;
  34.     Processed_data* data_arr_2;
  35.  
  36.     int size_data_arr_1;
  37.     int size_data_arr_2;
  38.  
  39.     double total_average;
  40. } Statistics;
  41.  
  42. // End of structs
  43.  
  44. // Functions
  45.  
  46. int* input_data(Student_Data s1)
  47. {
  48.     double current_grade = 0;
  49.     double* student_grades = (double*)malloc((s1.num_of_tests + 1) * sizeof(double));
  50.  
  51.     for (int i = 0; i < s1.num_of_tests; i++)
  52.     {
  53.         printf("\n(Student %d)\nTest %d grade: ", s1.student_id, i + 1);
  54.         scanf("%lf", &current_grade);
  55.  
  56.         student_grades[i] = current_grade;
  57.         current_grade = 0;
  58.     }
  59.  
  60.     return student_grades;
  61. } // V
  62.  
  63. double student_average(double* grades, int size)
  64. {
  65.     double sum = 0;
  66.  
  67.     for (int i = 0; i < size; i++)
  68.     {
  69.         sum += grades[i];
  70.     }
  71.  
  72.     return sum / size;
  73. }
  74.  
  75.  
  76. double total_average(double* grades, int size)
  77. {
  78.     double sum = 0;
  79.  
  80.     for (int i = 0; i < size; i++)
  81.     {
  82.         sum += grades[i];
  83.     }
  84.  
  85.     return sum / size;
  86. }
  87.  
  88.  
  89.  
  90. void print_processed_data(Processed_data p1)
  91. {
  92.     printf("\nStudent ID: %d\n", p1.id);
  93.     printf("\nStudent Average: %lf\n", p1.student_average);
  94. }
  95.  
  96. void classification(Student_Data* data_arr, int size, Statistics* stats_pass)
  97. {
  98.     Statistics stats;
  99.  
  100.     double s_average = 0;
  101.  
  102.     double t_sum = 0;
  103.     double t_average = 0;
  104.  
  105.     // Can be more efficient
  106.     // Instead of 3 function calls and 3 loops can be made with 1 utility array that holds the averages of each student.
  107.    
  108.     int arr_1_size = 0; // Above average arr
  109.     int arr_2_size = 0; // Below average arr
  110.  
  111.     for (int j = 0; j < size; j++) // Sum of total tests
  112.     {
  113.         t_sum += student_average(data_arr[j].grades, data_arr[j].num_of_tests);
  114.     }
  115.  
  116.     t_average = t_sum / size;
  117.  
  118.  
  119.     for (int i = 0; i < size; i++)
  120.     {
  121.         s_average = student_average(data_arr[i].grades, data_arr[i].num_of_tests);
  122.  
  123.         if (s_average >= t_average)
  124.             arr_1_size++;
  125.  
  126.         else
  127.             arr_2_size++;
  128.  
  129.         s_average = 0;
  130.     }
  131.  
  132.     Processed_data* p_data1 = (Processed_data*)malloc((arr_1_size) * sizeof(Processed_data));
  133.     Processed_data* p_data2 = (Processed_data*)malloc((arr_2_size) * sizeof(Processed_data));
  134.  
  135.     int index_1 = 0;
  136.     int index_2 = 0;
  137.  
  138.     for (int k = 0; k < size; k++)
  139.     {
  140.         s_average = student_average(data_arr[k].grades, data_arr[k].num_of_tests);
  141.  
  142.         if (s_average >= t_average)
  143.         {
  144.             p_data1[index_1].id = data_arr[k].student_id; // Above average
  145.             p_data1[index_1++].student_average = s_average;
  146.         }
  147.  
  148.         else
  149.         {
  150.             p_data2[index_2].id = data_arr[k].student_id; // Below average
  151.             p_data2[index_2++].student_average = s_average;
  152.         }
  153.  
  154.         s_average = 0;
  155.     }
  156.  
  157.     printf("\n\n\n\n");
  158.  
  159.     stats.data_arr_1 = p_data1;
  160.     stats.data_arr_2 = p_data2;
  161.  
  162.     stats.size_data_arr_1 = arr_1_size;
  163.     stats.size_data_arr_2 = arr_2_size;
  164.  
  165.     stats.total_average = t_average;
  166.  
  167.     *stats_pass = stats;
  168.  
  169.     //free(p_data1);
  170.     //free(p_data2);
  171. }
  172.  
  173. void print_student(Student_Data s1)
  174. {
  175.     printf("\nStudent ID: %d\n", s1.student_id);
  176.     printf("Number of tests: %d\n", s1.num_of_tests);
  177.     printf("Grades: [");
  178.  
  179.     for (int i = 0; i < s1.num_of_tests; i++)
  180.     {
  181.         printf("%lf, ", s1.grades[i]);
  182.     }
  183.  
  184.     printf("]\n");
  185. }
  186.  
  187.  
  188. void print_tab(Statistics stats)
  189. {
  190.     printf("\n--Statistics--\n\n");
  191.     printf("%d Students above average:\n", stats.size_data_arr_1);
  192.  
  193.     for (int i = 0; i < stats.size_data_arr_1; i++)
  194.     {
  195.         print_processed_data(stats.data_arr_1[i]);
  196.     }
  197.  
  198.  
  199.     printf("\n%d Students below average:\n", stats.size_data_arr_2);
  200.  
  201.     for (int j = 0; j < stats.size_data_arr_2; j++)
  202.     {
  203.         print_processed_data(stats.data_arr_2[j]);
  204.     }
  205.  
  206.     printf("\n\nTotal average: %lf\n", stats.total_average);
  207. }
  208.  
  209. void free_memory(Student_Data *a1, double *a2)
  210. {
  211.     free(a1);
  212.     free(a2);
  213. }
  214.  
  215.  
  216. void main()
  217. {
  218.     Student_Data* student_arr = (Student_Data*)malloc(SIZE * sizeof(Student_Data)); // 3 students
  219.     int tests_number = 0; // default 0
  220.     double* averages_arr = (double*)malloc(SIZE * sizeof(double));
  221.     Statistics stats;
  222.  
  223.     for (int i = 0; i < SIZE; i++) // Initialize Students array
  224.     {
  225.         student_arr[i].student_id = i + 1; // student_id
  226.  
  227.         printf("Enter number of tests for student %d: ", i + 1);
  228.         scanf("%d", &tests_number);
  229.  
  230.         student_arr[i].num_of_tests = tests_number; // *num_of_tests
  231.         student_arr[i].grades = input_data(student_arr[i]); // *grades
  232.  
  233.         averages_arr[i] = student_average(student_arr[i].grades, student_arr[i].num_of_tests);
  234.     }
  235.  
  236.     double t_average = total_average(averages_arr, SIZE);
  237.  
  238.     classification(student_arr, SIZE, &stats);
  239.  
  240.     print_tab(stats);
  241.  
  242.     free_memory(student_arr, averages_arr);
  243. }
  244.  
RAW Paste Data