-4

I am using sublime text 2 and I am trying to program bubble sort, and every time I run the code below it gives me an error on bubbleSort(num[5], terms); the error is

ERROR: no matching function for call to 'bubbleSort'.

Can anyone tell me why this is happening.

The code is:

#include <iostream>

using namespace std;

void bubbleSort(int arr[], int term) {
    for(int i = 0; i < term; ++i) {
        for(int index = 0; index < term-i-1; ++index) {
            if(arr[index] < arr[index + 1]) {
                int swap;
                swap = arr[index];
                arr[index] = arr[index + 1];
                arr[index + 1] = swap;
            }
        }
    }
    for(int counter = 0; counter < term; counter++) {
        cout << arr[counter] << endl;
    }
}

int main() {
    cout << "Hi in this program I will do bubble sort" << endl;
    cout << "The numbers are 2, 9, 5, 10, 6"<< endl;
    int num[5] = {2, 9, 5, 10, 6};
    int terms = sizeof (num) / sizeof (num[0]);
    bubbleSort(num[5], terms);

    //answer = [2, 5, 6, 9, 2, 10]
}
Ian Zhang
  • 11
  • 8

2 Answers2

1

Although a good question should be accompanied by a complete example of the compiler error, this answer should be fine.

void bubbleSort(int arr[], int term) {
  // ...
}

It's a function which accepts an array of integers as first parameter and an integer as second one.

When you try to invoke it with:

int num[5] = {2, 9, 5, 10, 6};
// ...
bubbleSort(num[5], terms);

You're passing num[5] which is not an array of integer, but it should be an element of the array num, then an integer itself.

In short you're calling the function passing

 bubbleSort(INT, INT);

and not, as requested by the function

 bubbleSort(ARRAY_INT, INT);

That' why the compiler doesn't find a function which names bubbleSort and accept two integers.

Additional note

It's a little be out of context, but I want to suggest you to improve your C++ base skills, because the expression:

num[5]

It's perfectly wrong in your code, because it tries to access to the 6-th element in the array (which is composed by only 5 elements), that'll produce an out-of-bound behaviour.

BiagioF
  • 9,368
  • 2
  • 26
  • 50
0

Thanks for helping me now I know that I am supposed to use

bubbleSort(num, terms);
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Ian Zhang
  • 11
  • 8