Amumu
Amumu

Reputation: 18552

Invalid array passing?

I made a merge sort function:

void mergeSort(int emotionCount[], int low, int high){
    int i=0,k=0;
    //I did this to see the value inside the array, and I always got a garbage value 
    //when i=0, and the first correct value when i=1. I made a for loop here to 
    //see the values in the array in debugging mode in netbean.
    for (i=0;i<=high;i++){            
    }
    if (low == high){
        emotionCount[low]=emotionCount[low];          
    }else{
        int mid = (low+high)/2;
        mergeSort(emotionCount,low,mid);        
        mergeSort(emotionCount,mid+1,high);
        merge(emotionCount, low,mid, high);        }
}
void merge(int emotionCount[], int low,int mid, int high)
{
    int temp[high-low+1];
    int i=low,k=mid+1, j=high, n=0;
    int comparing=emotionCount[k];
    while (i<=mid || k<=high){
        while (emotionCount[i]<comparing)
        {
            temp[n]=emotionCount[i];
            i++;
            n++;

        }
        comparing=emotionCount[i];
        temp[n]=emotionCount[k];
        k++;
        i++;
        n++;
        while (emotionCount[k]<comparing){
            temp[n]=emotionCount[k];
            k++;
            n++;
        }
        comparing=emotionCount[k];
        temp[n]=emotionCount[i];
        k++;
        i++;
        n++;
   }
    while (i<=mid)
    {
        temp[n]=emotionCount[i];
        i++;
        n++;
    }
    while (k<=high)
    {
        temp[n]=emotionCount[k];
        k++;
        n++;
    }
    while (low<=high)
    {
        emotionCount[low]=temp[i];
        low++;
    }
}

And in main, i pass an array:

int array[10] = {0,5,8,2,4,6,8,2,20,25};
//number 9 because the highest position is 9. After this, array[10] is supposed to be sorted.
mergeSort(array ,0, 9);

Well, the method was kinda long, but I want to implement it myself. Basically, I pass an array to the mergeSort function, and if it is not at its minimum size (which is 1), it will continue to pass that array. The thing which bugging me is, when I pass an array to it, the first value is always a garbage value (like an address value or something). Only after i=1, it will give the first value of the array. I don't get it. Also, everytime exiting a lower mergeSort function to continue on the higer ones, all the sorted values in the array become 0.

Edit: The k variable is just used to hold the array value, to see the array value when I'm in debugging mode. I removed the k variable in the mergeSort function to clear up the confusion.

Upvotes: 0

Views: 176

Answers (4)

Amumu
Amumu

Reputation: 18552

I solved the problem. Corrected code:

void mergeSort(int emotionCount[], int low, int high){
    int i=0,k=0;

    if (low == high){
        emotionCount[low]=emotionCount[low];
        k=emotionCount[low];
    }else{
        int mid = (low+high)/2;
        mergeSort(emotionCount,low,mid);
        /*
        int i=0;            
        printf("Lower half:");
        for (i=low;i<=mid;i++){
            k=emotionCount[i];            
            printf("%d ",k);            
        }             
        printf("\n");*/

        mergeSort(emotionCount,mid+1,high);

        /*
        printf("Upper half:");
        for (i=mid+1;i<=high;i++){
            k=emotionCount[i];            
            printf("%d ",k);
        }
        printf("\n");*/

        merge(emotionCount, low,mid, high);
        //this is how I view the sorting process, along with other 2 above loops
       /* printf("Merged:");
        for (i=low;i<=high;i++){            
            k=emotionCount[i];
            printf("%d ",k);
        }
        printf("\n\n");*/
    }
}
void merge(int emotionCount[], int low,int mid, int high)
{
    int temp[high-low+1],index;
    for (index=0;index<high-low+1;index++)
    {
        temp[index]=0;
    }
    int i=low,k=mid+1, j=high, n=0;
    int comparing=emotionCount[k],temp1;
    while (i<=mid && k<=high){
        while (emotionCount[i]<comparing && i<=mid)
        {
            temp[n]=emotionCount[i];
            i++;
            n++;
        }
        if (i<=mid){
            comparing=emotionCount[i];
            if (k<=high)
                temp[n]=emotionCount[k];
            else
                temp[n]=comparing;
            k++;
            n++;
        }

        while (emotionCount[k]<comparing && k<=high){
            temp[n]=emotionCount[k];
            k++;
            n++;
        }
        if (k<=high){
            comparing=emotionCount[k];
            if (i<=mid)
                temp[n]=emotionCount[i];
            else
                temp[n]=comparing;
            i++;
            n++;
        }

   }
    while (i<=mid)
    {
        temp[n]=emotionCount[i];        
        i++;
        n++;
    }
    while (k<=high)
    {
        temp[n]=emotionCount[k];        
        k++;
        n++;
    }
    i=0;
    while (low<=high)
    {
        emotionCount[low]=temp[i];
        low++;
        i++;
    }
}

The initial high value must be exactly array size - 1.

Upvotes: 0

BlueDog
BlueDog

Reputation: 976

for (i=0;i<=high;i++){
    int k=*(emotionCount+i);
}
if (low == high){
    emotionCount[low]=emotionCount[low];
    int k=emotionCount[low];
}else{
    int mid = (low+high)/2;
    mergeSort(emotionCount,low,mid);
    int i=0;
    for (i=low;i<=mid;i++){
        int k=emotionCount[i];
    }

    mergeSort(emotionCount,mid+1,high);
    for (i=mid+1;i<=high;i++){
        int k=emotionCount[i];
    }
    merge(emotionCount, low,mid, high);
    for (i=low;i<=high;i++){
        int k=emotionCount[i];
    }
}

You redeclare int k in all those blocks. I assume you want to assign a value to k instead so remove the leading "int". Just as in Java which you claim to be familiar with.

What do you want to do anyway? Even if you would use your globally declared k assigning all array elements in a loop doesn't make sense at all. Basically you would end up with k being equal to emotionCount[high].

Do you know that you can dereference array elements using the [] syntax?

k = emotionCount[i]

Is easier to read.

You use Netbeans which has a nice GDB frontend. I suggest that you set a few breakpoints, step through your code and try to understand what you implemented there.

Upvotes: 0

Shawn Chin
Shawn Chin

Reputation: 86854

Yet to read through your function, but at first glance the problem that stands out is the way you are calling the function.

It should be :

mergeSort(array, 0, 9); 

If you use array[10], it means you're trying to pass in the 11th element of the array which is unfortunately out of bounds!

Upvotes: 1

lepoetemaudit
lepoetemaudit

Reputation: 17628

It looks like you're passing a value from inside the array instead of the array itself: shouldn't

mergeSort(array[10] ,0, 9);

be something like

mergeSort(array, 0, 0);

Upvotes: 0

Related Questions