Thread: Issue with mergesort

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    26

    Issue with mergesort

    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("&#37;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?
    Last edited by Bizmark; 03-27-2008 at 07:41 PM. Reason: More elaboration on the problem....

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Use (*comics)[ct] instead of *comics[ct] -- the compiler treats this as an array of pointers, not a pointer to an array. (Of course, you'll have to do that everywhere.)

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're comparing the addresses of the issuenum variables:
    Code:
    		if(&leftc[leftnum]->issuenum <= &rightc[rightnum]->issuenum)
    That's probably not what you intended to do.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. re-entrancy pattern issue setbacks
    By George2 in forum Windows Programming
    Replies: 0
    Last Post: 04-12-2008, 02:23 AM
  3. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  4. Natural Mergesort
    By wuzzo87 in forum C Programming
    Replies: 31
    Last Post: 04-14-2007, 09:41 PM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM