0

I am trying to implement a merge-sort algorithm with the use of arrays instead of vectors and I am getting some errors in one of my two functions. The code of the two functions is below.

void Merge(int ar[], int ar1[], int ar2[], int n1, int n2) {

    int p1 = 0;
    int p2 = 0;
    int p3 = 0;
    while (p1 < n1 && p2 < n2) {
        if (ar1[p1] < ar2[p2])
        {
            ar[p3]=ar1[p1];
            p3++;
            p1++;
        }
        else
        {
            ar[p3]=ar2[p2];
            p3++;
            p2++;
        }
    }

    while (p1 < n1)
    {
        ar[p3]=ar1[p1];
        p3++;
        p1++;
    }

    while (p2 < n2)
    {
        ar[p3]=ar2[p2];
        p3++;
        p2++;
    }
}

I figured out how to solve the problem I was facing with the code below.

void Sortmerge(int array[],int n) {

    if (n <= 1) return;

    if (n % 2 == 0)
    {
        int arr1[n/2];
        int arr2[n/2];
        int k=0;
        int l=0;

        for (int i=0;i<n;i++)
        {
            if (i<(n/2))
            {
                arr1[k++]=array[i];
            }

            else
            {
                arr2[l++]=array[i];
            }
        }

        Sortmerge(arr1,n/2);
        Sortmerge(arr2,n/2);

        for (int i=0;i<n;i++)
        {
        array[i]=0;
        }

        Merge(array, arr1, arr2,n/2,n/2);
    }

    if (n%2!=0)
    {
        int arr1[(n-1)/2];
        int arr2[(n+1)/2];
        int k=0;
        int l=0;
        for (int i=0;i<n;i++)
        {
            if (i<((n-1)/2))
            {
                arr1[k++]=array[i];
            }

            else
            {
                arr2[l++]=array[i];
            }
        }

        Sortmerge(arr1,(n-1)/2);
        Sortmerge(arr2,(n+1)/2);

        for (int i=0;i<n;i++)
        {
            array[i]=0;
        }

        Merge(array, arr1, arr2,(n-1)/2,(n+1)/2 );
    }
}
2
  • 2
    Be aware that variable-size arrays aren't standard C++ so some compilers won't accept them. Looks like you're using gcc which does. Commented Nov 16, 2011 at 21:28
  • 1
    sizeof(ar1)/sizeof(int) doesn't do what you want, since sizeof(ar1) is the same as sizeof(int*)
    – Neil
    Commented Nov 16, 2011 at 21:55

3 Answers 3

3

You have declared two sets of arrays. Two named arr1 and two named arr2. These have some data added to them, but then they go out of scope and the data is not used. That is the source of the warnings.

You then attempt to use arr1 and arr2 out of their scopes -- outside of the if blocks. This is the source of the error. These arrays must be declared once, likely at the top of your function (Sortmerge), before your n % 2 checks.

1

When you write

if (n%2==0)
{
    int arr1[n/2]; //<-------here
    int arr2[n/2]; //<-------here
    ..........
}

arr1 and arr2 are only visible to code within that scope (between the {}s). So you have two different sets of arr1 and arr2, one for odd n and one for even n, but neither of those is visible to your Sortmerge(...) calls.

Also, you can replace

if (n%2==0){
....
}

if (n%2!=0){
....
}

with

if (n%2==0){
....
}
else{
....
}
1

It's seem normal.

You declare your two array in a if statement. The scope for your variable is just for this statement. Replace :

   if (n%2==0)
    {
        int arr1[n/2]; //<-------here
        int arr2[n/2]; //<-------here
        int k=0;
        int l=0;

by

   int arr1[n/2]; //<-------here
   int arr2[n/2]; //<-------here
   if (n%2==0)
    {
        int k=0;
        int l=0;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.