Here is my code but it seems that the storage of the string into the variable in the linked list is not working. May I ask what the problem is and how to fix it. Thank you.
typedef struct studList {
char studName[3];
struct studList *next;
} stud;
typedef struct courseList {
char courseName[3];
stud *toStud;
struct courseList *next;
} course;
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 (void)
{
course *chead = NULL, *cnode, *hptr = NULL, *hptr2 = NULL;
stud *shead = NULL, *snode = NULL;
int noc,i,x,y,row,col,matrix[50][50],z,a = 0;
printf("Enter the number of course(s): ");
scanf("%d", &noc);
printf("Enter 3 letter course name followed by 3 letter student names separated by spaces.\n");
for(i = 0; i < noc; i++){
x = 0;
y = 0;
z = 0;
scanf(" %100[^\n]%*c",in);
while(in[z] != NULL){
if(x == 0){
cnode = (course *) malloc(sizeof(course));
}
if(in[z] != ' ' && x < 3){
cnode->courseName[x] = in[z];
x++;
}
else if(in[z] != ' '){
if(y == 0){
snode = (stud *) malloc(sizeof(stud));
}
if(y < 3){
snode->studName[y] = in[z];
y++;
}
}
if(y == 3){
cnode->toStud = snode;
shead = snode->next;
snode = shead;
y = 0;
}
z++;
}
chead = cnode->next;
cnode = chead;
}
hptr = chead;
for(i = 0; i < noc; i++)
matrix[i][i] = 0;
for(row = 0; hptr != NULL; row++){
hptr2 = hptr->next;
col = row + 1;
for(;hptr2 != NULL; col++){
matrix[row][col] = compareStuds(hptr->toStud, hptr2->toStud);
matrix[col][row] = compareStuds(hptr->toStud, hptr2->toStud);
hptr2 = hptr2->next;
}
hptr = hptr->next;
}
for(row = 0; row < noc; row++){
for(col = 0; col < noc; col++){
printf("%d ", matrix[row][col]);
}
printf("\n");
}
return 0;
}
The code basically outputs a matrix if there is a similar student in a course. If same students exist it is flagged by a 1. I can't seem to make the storage of the course name and students in the linked list since you need a student linked list for each course. and there is a lot of course linked list depending on the user. Any ideas?
Input would be like:
Enter number of course: 5
c01 qwe wer ert rty tyu
c02 asd sdf dfg fgh ghj hjk
c03 aaa zzz xxx sss www
c04 qwe aaa dfg poi lll
c05 asd ppp lll mmm
0 0 0 1 0
0 0 0 1 1
0 0 0 1 0
1 1 1 0 1
0 1 0 1 0
Note: The matrix is represented by the course from 1 to n num of courses.
like below,
c01 c02 c03 c04 c05
c01 0 0 0 1 0
c02 0 0 0 1 1
c03 0 0 0 1 0
c04 1 1 1 0 1
c05 0 1 0 1 0
If there is a student in the same class it is marked with a 1, if not then 0. since c01 and c04 both have 'qwe' in them then row c04 and col c01 is marked with 1 the same as row c01 and col c04 is marked also with 1.