Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
//student
typedef struct student {
char sname[4];
struct student *next;
} student_t;
//course
typedef struct course {
char name[4];
student_t *student;
struct course *next;
} course_t;
int Compare2(int elements[40], int num, int count)
{
int y,check=0;
for(y=0;y<count;y++)
{
if(elements[y]==num)
check=1;
}
return check;
}
int Compare3(int elements[40][40], int final[40][40], int row, int count, int start, int stop, int x)
{
int y,check=0;
for(y=0;y<count;y++)
{
if(elements[row][y]==final[x][start])
check=1;
}
start++;
return check;
}
//printing
void printlist(course_t *traverse)
{
while (traverse != NULL)
{
student_t *hello = traverse->student;
printf("%s", traverse->name);
while (hello->sname != NULL)
{
printf(" %s", hello->sname);
hello = hello->next;
}
printf("\n");
traverse = traverse->next;
}
}
void printcourse(course_t *go)
{
int x=1;
while(go != NULL)
{
printf("TS %d: %s",x, go->name);
x++;
go = go->next;
printf("\n");
}
}
int main()
{
char filename[100];
int roomlimit;
scanf("%s", filename);
FILE *f = fopen(filename, "r");
course_t *head = NULL, *hover = NULL;
student_t *head2 = NULL, *hover2 = NULL;
char input[5] = {'\0','\0','\0','\0','\0'};
char studentsname[5] = {'\0','\0','\0','\0','\0'};
int x=0,storage[100],y=0;
while (fgets(input, 5, f) != NULL)
{
input[3] = '\0'; //removes newaline
course_t *newa = (course_t *) malloc(sizeof(course_t));
strcpy(newa->name, input);
//where we access the file named C01, etc.
newa->student = NULL;
newa->next = NULL;
//work in progress
FILE *g = fopen(input, "r");
while (fgets(studentsname, 5, g) != NULL)
{
//removes new line
student_t *node = (student_t*) malloc(sizeof(student_t));
studentsname[3] = '\0';
strcpy(node->sname, studentsname);
node->next = NULL;
if(newa->student == NULL)
{
newa->student = node;
hover2 = node;
}
else
{
hover2->next = node;
hover2 = node;
}
}
fclose(g);
//end of accessing
if(head == NULL)
{
head = newa;
hover = newa;
}
else
{
hover->next = newa;
hover = newa;
}
x++;
}
fclose(f);
scanf("%d", &roomlimit);
printlist(head);
//give 0 to all the matrix values
int matrix[x+1][x+1];
int loop=0,loop2=0,decepticons[x+1][x+1],transform[x+1][x+1];
for(loop = 0; loop < x; loop++)
{
for(loop2 = 0; loop2 < x; loop2++)
{
matrix[loop][loop2] = 0;
decepticons[loop][loop2] = 0;
transform[loop][loop2] = 0;
}
}
//code for matrix
course_t *first = head;
course_t *second;
int class1=0;
int class2=class1+1;
//while the list is not yet donedonedonedon
while(first != NULL)
{
//while the first class is not yet NULL
student_t *hold = first->student;
while(hold != NULL)
{
class2=class1+1; //for the 2d array
second = first->next; //the second list is equal to the next of the first(course
//while the second class is not yet equal to NULL
while(second != NULL)
{
student_t *keep = second->student;
//temporary course_t yeah :D
//while the the students in the second class are not yet NULL
while(keep != NULL)
{
if(strcmp(hold->sname,keep->sname) == 0)
{
matrix[class1][class2] = 1;
matrix[class2][class1] = 1;
break;
}
else
{
keep = keep->next;
//traverses the students in the second array
}
}
class2++;
second = second->next; //traverses the second class
}
//goes to the other student;
hold = hold->next;
}
first = first->next;
class1++;
}
//end of matrix
//matrix printer 2d array
int r,c;
printf("\n");
for(c=0; c < x; c++)
{
for(r=0; r < x; r++)
{
printf("%d ",matrix[c][r]);
}
printf("\n");
}
//copies the names of the courses to an array type for easy access in scheduling.
course_t goat[100];
course_t *copy = head;
int iter;
for(iter = 0; iter<x; iter++)
{
strcpy(goat[iter].name, copy->name);
copy = copy->next;
}
//i put what you want inside
int col,row,stor;
col=0;
int autobots[x+1];
autobots[x] = '\0';
while(col<x)
{
decepticons[col][0] = col;
stor = 1;
for(row=0; row<x; row++)
{
if(col!=row && matrix[col][row] ==0)
{
decepticons[col][stor] = row+1;
stor++;
}
}
autobots[col] = stor; //counts the number of zeroes;
col++;
}
if(roomlimit == 1)
{
printf("\n");
printcourse(head);
}
if(roomlimit != 1)
{
int elem=0,w;
int z=x;
for(x=0;x<z&&elem<z;x++)
{
if(Compare2(autobots,x+1,elem)==0)
{
transform[x][0]=x+1;
autobots[elem]=x+1;
elem++;
w=1;
//for the rooms
for(y=w;y<z&&w<roomlimit;y++) //y is less than z and w is less than the number of rooms
{
if(Compare2(autobots,decepticons[x][y],elem)==0) //autobots equal to the current room, decepticons equal to rooms, elem is for number of rooms
{
if(Compare3(decepticons, transform, decepticons[x][y]-1,z,0,w,x) == 1)
{
autobots[elem]=decepticons[x][y]; //will be equal to the elem in that decepticons pile
transform[x][w]=decepticons[x][y]; //will be equal to the elem in that decepticons pile
elem++; //moves the element up
w++; //moves the elemt up
}
}
}
//for the rooms
}
}
int ts=1;
printf("\n");
for(x=0;x<z;x++)
{
if(transform[x][0]!=0)
{
printf("TS %d: ", ts);
ts++;
}
for(y=0;y<z;y++)
{
if(transform[x][y]!=0)
printf("%s ", goat[transform[x][y]-1].name);
// printf("%d earawere\n", transform[x][y]-1);
}
if(transform[x][0]!=0)
printf("\n");
}
}
getch();
return 0;
}
the only thing i need is to separate the courses to groups that it has no conflict with what is wrong with my code?