From 9b16a7ae19fee3d42ac42311941c281992658c96 Mon Sep 17 00:00:00 2001 From: Weber Date: Thu, 21 Mar 2024 21:17:41 +0000 Subject: [PATCH] skuska --- cv5/program.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/cv5/program.c b/cv5/program.c index c5805e3..417fc0e 100644 --- a/cv5/program.c +++ b/cv5/program.c @@ -28,20 +28,20 @@ int main() { char line[1024]; while (fgets(line, sizeof(line), stdin)) { int vote_count; - char name[1024]; // Use array instead of dynamically allocated memory - sscanf(line, "%d %[^\n]", &vote_count, name); // Read name until newline character + char name[1024]; + sscanf(line, "%d %[^\n]", &vote_count, name); if (student_count >= students_capacity) { students_capacity = (students_capacity + 1) * 2; student_t *new_students = realloc(students, students_capacity * sizeof(student_t)); if (new_students == NULL) { - fprintf(stderr, "Error: Memory reallocation failed\n"); + fprintf(stderr, "chyba"); return 1; } students = new_students; } - students[student_count].name = strdup(name); // Duplicate the name + students[student_count].name = strdup(name); students[student_count].vote_count = vote_count; student_count++; } @@ -49,8 +49,11 @@ int main() { qsort(students, student_count, sizeof(student_t), compare_students); printf("Vysledky:\n"); - for (int i = 0; i < student_count; i++) { - printf("%d %s\n", students[i].vote_count, students[i].name); + printf("%d %s\n", students[0].vote_count, students[0].name); + for (int i = 1; i < student_count; i++) { + if (strcmp(students[i].name, students[i - 1].name) != 0) { + printf("%d %s\n", students[i].vote_count, students[i].name); + } free(students[i].name); } free(students);