GiH
GiH

Reputation: 415

Implementing Merge Sort

So, I am trying to implement merge sort, which I conceptually understand but am obviously having great difficulty programming (I'm quite inexperienced). I have looked up previous posts which has helped, but I can't get past this point. I am currently having the following errors :

13:20: error: âSizeâ was not declared in this scope mergesort(array[Size], low, mid);

41:20: error: invalid conversion from âintâ to âint*â [-fpermissive] mergesort(data[size], 0, size-1);

6:6: error: initializing argument 1 of âvoid mergesort(int*, int, int)â [-fpermissive] void mergesort(int array[], int low, int high)

I am also very confused on how to proceed in general. I really don't know how to merge everything back together either. This did not seem like it would be nearly this difficult, but recursion really confuses me :/ Thanks for your help in advance.

#include <iostream>
using namespace std;

void merge(int[], int, int, int);

void mergesort(int array[], int low, int high)
{
    int mid;

    if(low < high)
    {
       mid = low + (high-low)/2;
       mergesort(array[Size], low, mid);
       mergesort(array[Size], mid+1, high);
       merge(array[Size], low, mid, high);
    }
}

void merge(int array, int low, int mid, int high)
{

}

int main()
{
    int size;
    cin >> size;
    int data[size];

    for(int i = 0; i < size; i++)
    {
        cin >> data[i];
    }

    mergesort(data[size], 0, size-1);
}

Upvotes: 0

Views: 1121

Answers (2)

Joseph Mansfield
Joseph Mansfield

Reputation: 110768

data[Size] attempts to get the Sizeth value from the array data. Size doesn't exist, and I don't think this is what you want. If you want to refer to the array itself, just use it's name: data.

You have the same problem later on with data[size], except size does exist in this case. The error message is telling you that it can't convert an int to an int*. When you declare a function parameter like int array[], it is actually just syntactic sugar for int* array. You are passing data[size] to this parameter, which attempts to access an int from data (although size is outside the bounds). Hence, the compiler does not know how to convert an int to a int* - they're different types.

Note that the following code is not standard C++, because variable-length arrays are not supported:

int size;
cin >> size;
int data[size];

That is, the size of an array needs to be known at compile-time, which size is not. You could instead use std::vector<int> data(size);.

Also, your declaration and definition for merge don't match - one takes an int as its first parameter, while the other takes int[].

Upvotes: 1

MichaelCMS
MichaelCMS

Reputation: 4763

It seems you are having difficulties understanding static arrays vs dynamic arrays.

I would suggest using std::vector instead of your data[size] declaration .

Regarding your errors :

Note that inside your mergesort function you are referring to Size which isn't defined . If you want to go with static array, I would suggest the following :

 #define SIZE 200
 ...
 int data[SIZE];

This will allow you to use the same SIZE thorughout the code.

However your array won't be the size of your input .

If you want to allocate an array at runtime , you need to change your code from

 int size;
 cin >> size;
 int data[size]; 

To

 int size;
 cin >> size;
 int* data = new int[size];

Then , in your mergesort function, you will have to pass the size as parameter.

Upvotes: 1

Related Questions