Thread: Exam Scheduling Problem

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    Angry Exam Scheduling Problem

    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 */
    }
    Good day everyone!
    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.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by itsacezon View Post
    I have an error with the code I posted above.
    Then you should post the error and/or explain what the problem is.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Quote Originally Posted by MK27 View Post
    Then you should post the error and/or explain what the problem is.
    When I tried to compile it, the courselist and the adjacency matrix was printed and then my compiler crashed.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by itsacezon View Post
    When I tried to compile it, the courselist and the adjacency matrix was printed and then my compiler crashed.
    "Compiling" refers to the process of creating an executable from source code, not running the executable.

    So if the courselist, etc, printed out, that means the program successfully compiled, and you were running it. It is not the compiler which crashed, it is the program executable itself. The usual cause of that is a "segmentation fault". Segmentation faults occur when programs try to access memory that does not belong to them, and the operating system prevents it.

    A possible cause of that is going out of bounds with an array. The best way to find that is to learn to use a debugger, which you will have to do at some point. However, for right now, you could try inserting stuff like this:

    Code:
        /* ------------- */
        /* 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++) {
    fprintf(stderr, "Uncolor loop: ind = %d\n", ind);
            schedule[ind] = 0;
        }
    I usually leave those unindented so they are easier to find and remove later. It is very important to use fprintf(stderr ...) and not printf(), because printf() uses stdout. Stdout is buffered, meaning the data may not be output before the crash even if the printf() has already occurred (and the point here is to pinpoint when the crash happens).

    HOWEVER, it certainly isn't in the loop above, because ind will obviously always be less than courseSize.

    I would probably start looking around line 250. You are walking a list there, and hover->next may be either some uninitialized value at some point, or NULL, and then you try hover = hover->next again, but a NULL pointer does not have a member "next". That problem will only show up at runtime (as a segmentation fault, since you are depending on information that does not exist/cannot be accessed).
    Last edited by MK27; 10-19-2011 at 09:26 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Oh my! Thank you so much!
    After scanning and debugging with your help, I managed to execute my code.
    The problem was in my variables and linked lists.

    Thank you again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exam scheduling Program trouble
    By Khalamuha in forum C Programming
    Replies: 1
    Last Post: 10-19-2011, 12:22 AM
  2. problem with to long solution to exam question
    By guest in forum C Programming
    Replies: 11
    Last Post: 07-02-2011, 01:50 AM
  3. Scheduling Policy
    By edesign in forum Linux Programming
    Replies: 9
    Last Post: 05-12-2009, 08:47 PM
  4. Scheduling
    By Jules in forum Tech Board
    Replies: 4
    Last Post: 01-18-2004, 01:47 PM
  5. sort problem for an exam
    By rjeff1804 in forum C Programming
    Replies: 10
    Last Post: 02-12-2003, 10:33 PM