Initialization

This commit is contained in:
Kozar 2024-04-25 17:56:47 +02:00
parent 525ad77cc3
commit 81ca9d6d44

View File

@ -1,16 +1,9 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#define MAX_STUDENTS 100 #define MAX_STUDENTS 100
#define NAME_SIZE 50 #define NAME_SIZE 50
int compare_names(const void *a, const void *b) {
const char **name_a = a;
const char **name_b = b;
return strcmp(*name_a, *name_b);
}
int main() { int main() {
int max_students, num_accepted = 0, num_students = 0; int max_students, num_accepted = 0, num_students = 0;
char names[MAX_STUDENTS][NAME_SIZE]; char names[MAX_STUDENTS][NAME_SIZE];
@ -25,10 +18,9 @@ int main() {
memset(names, 0, sizeof(names)); memset(names, 0, sizeof(names));
while (fgets(temp_name, sizeof(temp_name), stdin)!= NULL && num_accepted < max_students) { while (fgets(temp_name, sizeof(temp_name), stdin)!= NULL && num_accepted < max_students) {
// Remove the newline character from the temp_name array
temp_name[strcspn(temp_name, "\n")] = '\0';
int i, found = 0; int i, found = 0;
for (i = 0; i < num_accepted; i++) { for (i = 0; i < num_accepted; i++) {
if (strcmp(names[i], temp_name) == 0) { if (strcmp(names[i], temp_name) == 0) {
found = 1; found = 1;
@ -36,26 +28,33 @@ int main() {
} }
} }
if (!found && num_accepted < MAX_STUDENTS) { if (!found) {
strcpy(names[num_accepted], temp_name); strcpy(names[num_accepted], temp_name);
num_accepted++; num_accepted++;
} }
num_students++; num_students++;
// Check if we have reached the maximum number of accepted students
if (num_accepted == max_students) {
break; // Exit the loop
} }
}
qsort(names, num_accepted, sizeof(names[0]), compare_names);
if (num_accepted == 0) { if (num_accepted == 0) {
printf("Ziadne prihlasky\n"); printf("Ziadne prihlasky");
} else { } else {
printf("Prijati studenti:\n"); printf("Prijati studenti:\n");
for (int i = 0; i < num_accepted; i++) { for (int i = 0; i < num_accepted; i++) {
printf("%s\n", names[i]); printf("%s", names[i]);
}
printf("\nNeprijati studenti:\n");
for (int i = 0; i < num_students; i++) {
int found = 0;
for (int j = 0; j < num_accepted; j++) {
if (strcmp(names[j], temp_name) == 0) {
found = 1;
break;
}
}
if (!found) {
printf("%s", temp_name);
}
} }
} }