2

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.

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
markdid
  • 55
  • 1
  • 5
  • What is the variable `in`? – Some programmer dude Jan 28 '14 at 17:20
  • Also, you have to remember that strings contain an extra special character (`'\0'`) that tells the system where the strings end. So for a three-character string you actually need *four* characters. – Some programmer dude Jan 28 '14 at 17:21
  • And finally, [don't cast the return of `malloc`](http://stackoverflow.com/a/605858/440558). – Some programmer dude Jan 28 '14 at 17:26
  • in is just a temporary variable to store the whole input string to allocate each character to be either a course or a student. The first 3 characters of in are added to the course struct and the following characters by 3 are added to the student struct – markdid Jan 28 '14 at 17:35

1 Answers1

0

The problem is that you store strings of three characters in arrays with space for three characters. The problem with this is that in C strings don't have a length, and functions like strcmp expects all strings to be terminated with a special character, the so called null character (not to be confused with the NULL pointer). This special character is normally expressed as '\0'.

What you need to do is to increase all string arrays to make space for this terminator character, and the add it to the end of the strings. Like in char studName[4]; and e.g.

if(y < 3) {
    snode->studName[y++] = in[z];
} else if (y == 3) {
    snode->studName[y] = '\0';
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • If you use studName[3], that means you have 4 slots to put a character in. Is that not enough? And even though I've increased the string arrays the output I'm getting is also wrong. I think the problem lies in the organization of the linked list – markdid Jan 28 '14 at 18:18
  • @Marquack No that's three slots, numbered zero to two. – Some programmer dude Jan 28 '14 at 20:29