TD1_DEV51_Qualite_Algo/student_rank.c

124 lines
3.3 KiB
C
Raw Permalink Normal View History

// Student rank
// M.Menault 2024
#include <stdio.h>
#include <stdlib.h>
#include "heapsort.h"
2024-09-03 08:26:03 +02:00
#include "bubblesort.h"
2024-09-03 12:18:08 +02:00
void generate_grades(int **students_array, int students_number, int grades_number)
{
int i = 0;
2024-09-03 12:18:08 +02:00
for (i = 0; i < students_number; i++)
{
2024-09-03 12:18:08 +02:00
students_array[i] = (int *)malloc(grades_number * sizeof(int));
generate_array(students_array[i], grades_number);
}
}
2024-09-03 12:18:08 +02:00
void generate_ranks(int **students_array, int students_number, int grades_number)
{
int i = 0;
int j = 0;
2024-09-03 12:18:08 +02:00
for (i = 0; i < students_number; i++)
{
2024-09-03 12:18:08 +02:00
students_array[i] = (int *)malloc(grades_number * sizeof(int));
for (j = 0; j < grades_number; j++)
{
students_array[i][j] = -1;
2024-09-03 12:18:08 +02:00
}
}
}
2024-09-03 12:18:08 +02:00
void free_array(int **students_array, int students_number)
{
int i = 0;
2024-09-03 12:18:08 +02:00
for (i = 0; i < students_number; i++)
{
free(students_array[i]);
}
}
2024-09-03 12:18:08 +02:00
void print_student_array(int **students_array, int students_number, int grades_number)
{
int i = 0;
printf("----------------------\n");
2024-09-03 12:18:08 +02:00
for (i = 0; i < students_number; i++)
{
2024-09-03 12:18:08 +02:00
printf("Student %d : ", i);
print_array(students_array[i], grades_number);
}
printf("----------------------\n");
}
2024-09-03 12:18:08 +02:00
int find_rank_student(int student_grade, int *grades_array, int students_number)
{
int position = -1;
int i = 0;
2024-09-03 12:18:08 +02:00
for (i = students_number - 1; i >= 0; i--)
{
2024-09-03 12:18:08 +02:00
if (grades_array[i] == student_grade)
{
position = students_number - i;
break;
}
}
return position;
}
2024-09-03 12:18:08 +02:00
void sort_students(int **students_rank, int **students_array, int students_number, int grades_number)
{
int i = 0, j = 0;
2024-09-03 12:18:08 +02:00
for (i = 0; i < grades_number; i++)
{
2024-09-03 12:18:08 +02:00
int *grades = (int *)malloc(students_number * sizeof(int));
for (j = 0; j < students_number; j++)
{
grades[j] = students_array[j][i];
}
bubblesort(grades, students_number);
for (j = 0; j < students_number; j++)
{
students_rank[j][i] = find_rank_student(students_array[j][i], grades, students_number);
}
free(grades);
}
}
2024-09-03 12:18:08 +02:00
int main(int argc, char **argv)
{
2024-09-03 12:18:08 +02:00
int **student_grades = NULL;
int **student_ranks = NULL;
int students_length = 0;
int grades_length = 0;
int debug_mode = 0;
2024-09-03 12:18:08 +02:00
if (argc != 4)
{
2024-09-03 12:18:08 +02:00
printf("Usage : %s <number of student> <number of grade per student> <debug mode : 0|1>\n", argv[0]);
return -1;
}
students_length = atoi(argv[1]);
grades_length = atoi(argv[2]);
debug_mode = atoi(argv[3]);
2024-09-03 12:18:08 +02:00
student_grades = (int **)malloc(students_length * sizeof(int *));
student_ranks = (int **)malloc(students_length * sizeof(int *));
generate_grades(student_grades, students_length, grades_length);
generate_ranks(student_ranks, students_length, grades_length);
sort_students(student_ranks, student_grades, students_length, grades_length);
if (debug_mode)
{
2024-09-03 12:18:08 +02:00
print_student_array(student_grades, students_length, grades_length);
print_student_array(student_ranks, students_length, grades_length);
}
2024-09-03 12:18:08 +02:00
free_array(student_grades, students_length);
free_array(student_ranks, students_length);
free(student_grades);
free(student_ranks);
return 0;
}