Hi everybody, this program I'm writing is supposed to collect comic books from a file, sort them, then print them. I'm having trouble with my mergesort functions. Heres the declaration and initial call followed by the functions themselves:
Struct Comic
Code:
struct comic
{
char title[100];
char issue[100];
int issuenum;
//struct comic *next;
};
Declarations in Main
Code:
struct comic *thecomics;
struct comic *temp;
Initial Call
Code:
tempcnt = 1;
cnt = count +1;
do
{
if(strcmp(thecomics[count].title, thecomics[cnt].title) == 0)
{
temp[0] = thecomics[count];
temp[tempcnt] = thecomics[cnt];
tempcnt++;
}
cnt++;
}while(cnt < numcomics);
system("pause");
mergesort(&temp, tempcnt);
cnt = 0;
printf("%s\n", temp[0].title);
while(cnt <= tempcnt)
{
printf("\t%d. %s\n", temp[cnt].issuenum, temp[cnt].issue);
cnt++;
}
Merge Function
Code:
void merge(struct comic **comicsc, struct comic **leftc, int leftlen, struct comic **rightc, int rightlen)
{
int comicsnum = 0;
int leftnum = 0;
int rightnum = 0;
int righttst, lefttst;
// As long as both arrays have more integers, keep merging
while(leftnum < leftlen && rightnum < rightlen)
{
// If the smallest remaining integer in the left half is smaller than
// the smallest remaining integer in the right half, put the integer
// from the left half into the final array first
//righttst = &rightc[rightnum]->issuenum;
//lefttst = &leftc[leftnum]->issuenum;
if(&leftc[leftnum]->issuenum <= &rightc[rightnum]->issuenum)
{
*comicsc[comicsnum] = *leftc[leftnum];
comicsnum++;
leftnum++;
}
// Otherwise, go with the right half
else
{
*comicsc[comicsnum] = *rightc[rightnum];
comicsnum++;
rightnum++;
}
}
// If there aren't any integers left in the right half, that means there
// must still be some in the left half. Copy the remaining integers from the
// left half of the array.
if(rightnum >= rightlen)
{
while(leftnum < leftlen)
{
*comicsc[comicsnum] = *leftc[leftnum];
comicsnum++;
leftnum++;
}
}
// Otherwise, there are still integers in the right half, so copy those
else
{
while(rightnum < rightlen)
{
*comicsc[comicsnum] = *rightc[rightnum];
comicsnum++;
rightnum++;
}
}
}//End merge
Mergesort Function
Code:
void mergesort(struct comic **comics, int len)
{
struct comic *left;
struct comic *right;
int ct;
if(len <= 1)
{
return;
}
system("pause");
left = (struct comic*)calloc(len/2, sizeof(struct comic));
for(ct = 0; ct < len/2; ct++)
{
left[ct] = *comics[ct];
}
mergesort(&left, len/2);
right = (struct comic*)calloc(len - len/2, sizeof(struct comic));
for(ct = len/2; ct<len; ct++)
{
right[ct - len/2] = *comics[ct];
}
mergesort(&right, len - len/2);
merge(&comics, &left, len/2, &right, len-len/2);
free(left);
free(right);
}//End mergesort
Whats going on is there is an error code poping when my program reaches the mergesort call. The program runs fine before it hits that. Anyone see what's going on?