Main with Merge Sort Function - Help Please
Hi, could someone please help me with my main function that has merge sort in it?
I'm not sure how to call it or how this should be set up. I read in a file that has students and they are supposed to be sorted by their birthday, like JANUARY 5, and we find who has the closest birthday to someone else. The file reads in first name, last name, month, day, and year on the same line, but year is not used in the sorting. The queried students are part of the class, and I'm supposed to find the name of the student not queried with the closest birthday to the queried student.
This is what I have for main:
Code:
#include <stdio.h>
#include <string.h>
#define MAXLENGTH 30
struct student {
char first_name[MAXLENGTH];
char last_name[MAXLENGTH];
int month[12];
int day;
int year; //not used for sorting the birthdays
};
int Is_Sorted(struct student *val[], int length);
void MergeSort(struct student *val[], int low, int high);
void Merge(struct student *val[], int low, int middle, int high);
void Print_Array(struct student *val[], int length);
int main ()
{
int num_classes;
int month[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 394, 334};
FILE* ifp;
ifp = fopen("birthday.txt", "r");
struct student *info;
info = (struct student*)(malloc(sizeof(struct student)*MAXLENGTH));
fscanf(ifp, "%d ", &num_classes);
int i, j;
// Loop through each class
for (i=1; i<=num_classes; i++)
{
//Beginning Output
printf("Class #%d:\n\n", i);
int num_students;
fscanf(ifp, "%d", &num_students);
for(j=0; j<num_students; j++)
{
char first_name[MAXLENGTH];
char last_name[MAXLENGTH];
char month[MAXLENGTH];
int day, year;
fscanf(ifp, "%s ", first_name);
fscanf(ifp, "%s ", last_name);
fscanf(ifp, "%s ", month);
fscanf(ifp, "%d ", &day);
fscanf(ifp, "%d ", &year);
}
int num_queries;
char queried_firstname[MAXLENGTH];
char queried_lastname[MAXLENGTH];
fscanf(ifp, "%d", &num_queries);
for(j=0; j<num_queries; j++)
{
fscanf(ifp, "%s ", queried_firstname);
fscanf(ifp, "%s ", queried_lastname);
}
}
// Sort the Values
MergeSort(&info, 0, MAXLENGTH-1);
//Print_Array(val, SIZE);
// Check if it's sorted.
if (Is_Sorted(&info, MAXLENGTH))
//printf("Sorted correctly.\n");
/*
int dayofyear;
for(i=0; i<12; i++)
{
dayofyear = month[i] + day;
differenceleft = month[i] - month[i-1];
differenceright = month[i+1] - month[i];
if (differenceleft < differenceright)
//middle person closest to person on left
printf("%s %s has closest birthday to %s %s", first_name, last_name,
queried_firstname, queried_lastname);
else if(differenceright < differenceleft)
//middle person closest to person on right
}
*/
free(info);
fclose(ifp);
system ("PAUSE");
return 0;
}