Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ALEXANDROS AGRAFIOTIS, ECE UTH, 12/01/2021 */
- /* This program sorts students to classes based on their age */
- #include"lab10.h"
- #include <stdio.h>
- #include <stdlib.h>
- /* Returns the count of a specified age found in students' array */
- int print_count_of_age (const student_t students_array[], const int num_students,const int age) {
- int count=0,i;
- for(i=0; i<num_students; i++){
- if(age == students_array[i].age){
- count++;
- }
- }
- return count;
- }
- void print_student_info(const student_t students_array[],const int num_students, const int min_age, const int max_age){
- int i, serial_num,count;
- /* Print student's serial number, name and age */
- for(i=0; i< num_students; i++){
- serial_num = (int)(&students_array[i]-&students_array[0]);
- printf("%d. %s - %d\n",serial_num,students_array[i].name, students_array[i].age);
- }
- /* Print count of every age in students' array */
- for(i=min_age; i<= max_age; i++){
- count = print_count_of_age(students_array, num_students, i);
- printf("Age %d: %d students\n",i,count);
- }
- }
- 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){
- int i,j,pointer_count;
- for(i=1; i<=num_classes; i++){
- classes_array[i-1].level = i;
- }
- /* Instantiate all pointers-to-students to NULL */
- for(i=1; i<= num_classes; i++){
- for(j=0; j<num_students; j++){
- classes_array[i-1].pstudents[j] = NULL;
- }
- }
- /* For each class, check if every student's age corresponds to the age of the class. If it does, set pointer towards student */
- for(i=1; i <= num_classes; i++){
- pointer_count = 0;
- for(j=0; j<num_students; j++){
- if(students_array[j].age == min_age + i - 1){
- classes_array[i-1].pstudents[pointer_count] = &students_array[j];
- pointer_count++;
- }
- }
- }
- }
- void print_sorted_classes(const class_t class, const int max_students){
- int i;
- printf("== %d ==\n", class.level);
- for(i=0; i<max_students; i++){
- if(class.pstudents[i] == NULL){
- break;
- }
- printf("%s\n", class.pstudents[i]->name);
- }
- }
- int main (int argc, char *argv[]) {
- class_t classes_array[NUM_CLASSES];
- student_t students_array[MAX_STUDENTS];
- int num_students,i;
- if(argc == 1 || argc > 2 || atoi(argv[1]) > MAX_STUDENTS ){
- printf("Incorrect arguments!\n");
- return 1;
- }
- num_students = atoi(argv[1]);
- /*Instantiate students array with generate_students function */
- generate_students(students_array, MAX_STUDENTS,num_students, MIN_AGE, MAX_AGE, MAX_NAME_SIZE);
- print_student_info(students_array, num_students, MIN_AGE, MAX_AGE);
- sort_students_to_classes(classes_array, NUM_CLASSES, students_array, num_students, MIN_AGE);
- for(i=0; i<NUM_CLASSES; i++){
- print_sorted_classes(classes_array[i], print_count_of_age(students_array, num_students, MIN_AGE+i));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement