This commit is contained in:
Weber 2024-03-21 21:17:41 +00:00
parent 7cc02efe4e
commit 9b16a7ae19

View File

@ -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[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);