0

every time I ran my code which is a simple merge sort in c++, it throw an error that Merge func is not found. can any one help on this issue. the implementation is some thing like this:

int A[9];
void mergeSort(int low,int high) {
     if (low==high)
          return ;
     int mid=(high-low)/2;
     mergeSort(low, mid);
     mergeSort(mid+1,high);
     Merge(low,mid,high); 
}

void Merge(int l,int m ,int h) {
    int i=l;int k= l;int j=mid+1;
    int b[9];
    while (l<=m &&j<=high) {
         if (a[i]<=a[j])
              b[k]=a[i];
              k++;
         b[k]=a[j];
         j++;
         i++;
    } 
}
MikeMB
  • 20,029
  • 9
  • 57
  • 102
mokarab
  • 13
  • 1
  • 2
  • 4
  • maybe define `Merge` before `mergeSort` – Saeid Jun 26 '15 at 20:28
  • Have you created a function prototype for your Merge function? If not, create one, or move your Merge function above your mergeSort function. – ChrisD Jun 26 '15 at 20:28
  • Fyi, your mid point calculation is also incorrect. Ex: consider what happens when `low = 4` and `high = 8`. By your calculation `mid = (8-4)/2`, ie. `mid = 2`. Using your recursion, you then enter into `mergeSort(4,2)` and `mergeSort(2, 8)`. Ouch. – WhozCraig Jun 26 '15 at 20:29
  • As @WhozCraig said, your mid calculation should be `(high + low) / 2`. Plus, not minus. – Mr. Llama Jun 26 '15 at 20:32
  • ....or use `mid = low + (high-low)/2;` And your quick-exit is also wrong. it should exit when `(high-low) < 2`, not just when they're equal. – WhozCraig Jun 26 '15 at 20:33
  • As stated in the answers, the reason for your erro is that you have to define or at least declare Merge before mergsort, but I'd like to point out, that your merge function doesn't make any sense what-so-ever, mostly due to some typos and missing braces. – MikeMB Jun 26 '15 at 20:49
  • @mokarab: C++ is "declare before you use" language. You are attempting to call a function that you have not yet declared. – AnT stands with Russia Jun 26 '15 at 20:59

3 Answers3

0

Declare function Merge before function mergeSort

void Merge(int l,int m ,int h);

Take into account that you forgot to specify braces around functions bodies.

Also substitute this statement

int mid=(high-low)/2;

for

int mid = ( high + low ) / 2;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • thx every one, for your valuable comments. I think the declaration of Merge before mergeSort is need it. – mokarab Jun 27 '15 at 04:15
0

You either need to define Merge before mergeSort or supply a function definition beforehand. Each function only knows about those that have been defined or mentioned before it.

See this other question for additional info: Function declaration order matters in c language or am I doing something wrong?

Community
  • 1
  • 1
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
0

Either write Merge function before mergesort or add a function declaration of Merge before mergesort.

A function declaration is required before calling that function in c++, so that compiler can understand what are the arguments required (in order) and what is the return type.

A simple function declaration for Merge would be:

void Merge(int,int,int);

Declare it above the mergesort function.

Compiler only needs to know the return value type and what are the arguments required and in what order before calling the function.

Rakholiya Jenish
  • 3,165
  • 18
  • 28
  • To be pedantic, in C++ it is called *function declaration*. In C++ there's no need for C term "prototype" since in C++ (as opposed to C) any function declaration in C++ is a "prototype". There are no non-prototype function declarations in C++. However, you will see the language spec to use the term *prototype* here and there at times. – AnT stands with Russia Jun 26 '15 at 20:57