Initialization

This commit is contained in:
Kozar 2024-04-22 19:19:03 +02:00
parent 37b488a493
commit 06701bbaee

View File

@ -5,8 +5,8 @@
#define MAX_STUDENTS 100 #define MAX_STUDENTS 100
#define NAME_SIZE 50 #define NAME_SIZE 50
int compare(const void *a, const void *b) { int compareStrings(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b); return strcmp((const char *)a, (const char *)b);
} }
int main() { int main() {
@ -23,9 +23,11 @@ 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) {
int i, found = 0; int i, found = 0;
// // Remove newline character if present
// temp_name[strcspn(temp_name, "\n")] = '\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;
@ -40,17 +42,14 @@ int main() {
num_students++; num_students++;
} }
// Add a null character at the end of the names array
names[num_accepted][0] = '\0';
qsort(names, num_accepted, sizeof(names[0]), compare);
if (num_accepted == 0) { if (num_accepted == 0) {
printf("Ziadne prihlasky"); printf("Ziadne prihlasky");
} else { } else {
qsort(names, num_accepted, sizeof(names[0]), compareStrings);
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", names[i]); printf("%s\n", names[i]);
} }
} }