Kalhnyxtakias

Untitled

Jan 14th, 2021
433
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ALEXANDROS AGRAFIOTIS, ECE UTH, 12/01/2021 */
  2. /* This program sorts students to classes based on their age */
  3.  
  4. #include"lab10.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. /* Returns the count of a specified age found in students' array */
  9. int print_count_of_age (const student_t students_array[], const int num_students,const int age) {
  10.     int count=0,i;
  11.    
  12.     for(i=0; i<num_students; i++){
  13.         if(age == students_array[i].age){
  14.             count++;
  15.         }
  16.     }
  17.     return count;
  18. }
  19.  
  20. void print_student_info(const student_t students_array[],const int num_students, const int min_age, const int max_age){
  21.     int i, serial_num,count;
  22.  
  23.     /* Print student's serial number, name and age */
  24.     for(i=0; i< num_students; i++){
  25.         serial_num = (int)(&students_array[i]-&students_array[0]);
  26.         printf("%d. %s - %d\n",serial_num,students_array[i].name, students_array[i].age);
  27.     }
  28.     /* Print count of every age in students' array */
  29.     for(i=min_age; i<= max_age; i++){
  30.         count = print_count_of_age(students_array, num_students, i);
  31.         printf("Age %d: %d students\n",i,count);
  32.     }        
  33. }
  34.  
  35. void sort_students_to_classes(class_t classes_array[], const int num_classes,student_t students_array[], const int num_students, const int min_age){
  36.     int i,j,pointer_count;
  37.  
  38.     for(i=1; i<=num_classes; i++){
  39.         classes_array[i-1].level = i;
  40.     }
  41.  
  42.     /* Instantiate all pointers-to-students to NULL */
  43.     for(i=1; i<= num_classes; i++){
  44.         for(j=0; j<num_students; j++){
  45.             classes_array[i-1].pstudents[j] = NULL;
  46.         }
  47.     }
  48.  
  49.     /* For each class, check if every student's age corresponds to the age of the class. If it does, set pointer towards student */
  50.     for(i=1; i <= num_classes; i++){
  51.         pointer_count = 0;
  52.         for(j=0; j<num_students; j++){
  53.             if(students_array[j].age == min_age + i - 1){
  54.                 classes_array[i-1].pstudents[pointer_count] = &students_array[j];
  55.                 pointer_count++;
  56.             }
  57.         }
  58.     }
  59. }
  60.  
  61. void print_sorted_classes(const class_t class, const int max_students){
  62.     int i;
  63.    
  64.     printf("== %d ==\n", class.level);
  65.    
  66.     for(i=0; i<max_students; i++){
  67.         if(class.pstudents[i] == NULL){
  68.             break;
  69.         }
  70.         printf("%s\n", class.pstudents[i]->name);
  71.     }
  72. }
  73.  
  74.  
  75. int main (int argc, char *argv[]) {
  76.  
  77.     class_t classes_array[NUM_CLASSES];
  78.     student_t students_array[MAX_STUDENTS];
  79.     int num_students,i;
  80.  
  81.     if(argc == 1 || argc > 2 || atoi(argv[1]) > MAX_STUDENTS ){
  82.         printf("Incorrect arguments!\n");
  83.         return 1;
  84.     }
  85.    
  86.     num_students = atoi(argv[1]);
  87.  
  88.     /*Instantiate students array with generate_students function */
  89.     generate_students(students_array, MAX_STUDENTS,num_students, MIN_AGE, MAX_AGE, MAX_NAME_SIZE);
  90.  
  91.     print_student_info(students_array, num_students, MIN_AGE, MAX_AGE);
  92.  
  93.     sort_students_to_classes(classes_array, NUM_CLASSES, students_array, num_students, MIN_AGE);
  94.  
  95.     for(i=0; i<NUM_CLASSES; i++){
  96.         print_sorted_classes(classes_array[i], print_count_of_age(students_array, num_students, MIN_AGE+i));
  97.     }
  98.    
  99.     return 0;
  100. }
  101.  
RAW Paste Data