Good day everyone!Code:/* Initialize libraries */ #include <stdio.h> #include <stdlib.h> #include <string.h> /* Create new data types */ typedef struct studList { char studName[3]; struct studList *next; } stud; typedef struct courseList { char courseName[3]; stud *toStud; struct courseList *next; } course; typedef struct scheduling { char courseName2[3]; int color; struct scheduling *next; } sched; int compareStuds(stud *toStud1, stud *toStud2) { int count = 0; stud *tempPtr; while(toStud1 != NULL) { tempPtr = toStud2; while(tempPtr != NULL) { if(strcmp(toStud1->studName, tempPtr->studName) == 0) { count = 1; } tempPtr = tempPtr->next; } toStud1 = toStud1->next; } return count; } int main() { char filename[50]; int roomNum, courseSize = 0; scanf("%s%d", filename, &roomNum); //Input filename, number of rooms /* Initialize main pointers */ course *head = NULL, *hover = NULL, *tail = NULL; stud *studHead, *studHover, *compPtr1, *compPtr2; /* Open file containing courses */ FILE *courseNames = fopen(filename, "r"), *studentNames; /* -------------------------------------------------------- */ /* 1 - SAVING COURSE LIST AND STUDENT NAMES TO LINKED LISTS */ /* -------------------------------------------------------- */ char courseList2[500], studentName[3]; printf("\n"); while (!feof(courseNames)) { fscanf(courseNames, "%s", courseList2); courseList2[3] = '\0'; course *node = (course *) malloc(sizeof(course)); strcpy(node->courseName, courseList2); printf("%s: ", courseList2); studHead = NULL, studHover = NULL; studentNames = fopen(courseList2, "r"); while (!feof(studentNames)) { fscanf(studentNames, "%s", studentName); studentName[3] = '\0'; stud *studNode = (stud *) malloc(sizeof(stud)); strcpy(studNode->studName, studentName); if(node->toStud == NULL) { node->toStud = studNode; } studNode->next = NULL; if(studHead == NULL) { studHead = studNode; studHover = studNode; } else { studHover->next = studNode; studHover = studNode; } printf("%s ", studentName); } fclose(studentNames); node->toStud = studHead; node->next = NULL; if(head == NULL) { head = node; hover = node; } else { hover->next = node; hover = node; } courseSize++; printf("\n"); } fclose(courseNames); printf("\n"); /* -------------------- */ /* 2 - ADJACENCY MATRIX */ /* -------------------- */ char matrix[courseSize][courseSize]; int row = 0, col = 0, i2; course *headPtr = NULL, *headPtr2 = NULL; headPtr = head; for(i2 = 0; i2 < courseSize; i2++) { matrix[i2][i2] = 0; } for(row = 0; headPtr != NULL; row++) { headPtr2 = headPtr->next; col = row + 1; for(; headPtr2 != NULL; col++) { /* Traverse linked list for conflicts */ matrix[row][col] = compareStuds(headPtr->toStud, headPtr2->toStud); matrix[col][row] = compareStuds(headPtr->toStud, headPtr2->toStud); headPtr2 = headPtr2->next; } headPtr = headPtr->next; } for(row = 0; row < courseSize; row++) { for(col = 0; col < courseSize; col++) { printf("%d ", matrix[row][col]); } printf("\n"); } printf("\n"); /* ------------- */ /* 3- SCHEDULING */ /* ------------- */ int schedule[courseSize]; int limit = 0, schedColor = 1, i = 0; row = 0; /* Uncolor everthing */ int ind = 0; for(ind = 0; ind < courseSize; ind++) { schedule[ind] = 0; } while(1){ int init = 0; while(schedule[init] != 0){ init++; if(init == courseSize) { break; } } if(init == courseSize) { break; } sched *schedNode = (sched *) malloc(sizeof(sched)); schedNode->color = init; schedule[init] = schedColor; int i = 0, num = 1; for(i = 0; i < courseSize; i++) { if(num == roomNum) { break; } if(schedule[i] != 0) { continue; } int flag = 1; sched *trav = schedNode; while(trav != NULL){ if(matrix[trav->color][i] == 1) { flag = 0; } trav = trav->next; } if(flag){ schedule[i] = schedColor; num++; trav = schedNode; while(trav->next != NULL) { trav = trav->next; } sched *temp2 = (sched *) malloc(sizeof(sched)); temp2->color = i; trav->next = temp2; } } schedColor++; } for(i = 0; i < courseSize; i++) { printf("%d ", schedule[i]); } printf("\n"); char finalSched[courseSize][3]; hover = head; for(row = 0; row < courseSize; row++) { strcpy(finalSched[row], hover->courseName); hover = hover->next; } int o; for(o = 1; o < schedColor; o++) { printf("TS%d: ", o); for(row = 0; row < courseSize; row++) { if(schedule[row] == o) { printf("%s ", finalSched[row]); } } printf("\n"); } printf("\n"); return 0; /* Program ended successfully */ }
I have an error with the code I posted above. It seems that the 3 - SCHEDULING part has the problem. Can someone help me here? Thanks.