-2

i am newbie, i don't know how to fix it? i don't know how to call function void bubblesort

 #include<iostream>

using namespace std;

void bubbleSort(int a[], int n)
{
    for (int i = 0; i < n - 1; i++)
        for (int j = n - 1; j > i; j--)
            if (a[j] < a[j - 1])
                swap(a[j], a[j - 1]);
}

int main() {
    int a[]={1,4,7,2,6,5,3,9,8,10};
    bubbleSort(a[], sizeof(a));
    for (size_t i=0;i<10;i++)
    {
        cout<< a[i];
    }
}
  • 2
    Could you tell more about your problem ? Is nothing shown ? Did the bubble sort don't do anything ? Did your program crash ? – Marius ROBERT May 13 '22 at 11:34
  • 2
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please take the [tour] and read about [ask] good questions. Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude May 13 '22 at 11:34
  • 1
    the second parameter to `bubblesort()` shouldn't be `sizeof(a)`. It should number of elements – kuro May 13 '22 at 11:35
  • 1
    @Caleth Not `sizeof(a)`, but `sizeof a / sizeof a[0]`. – Some programmer dude May 13 '22 at 11:35
  • 1
    Change `bubbleSort(a[], sizeof(a));` to `bubbleSort(a, sizeof(a)/sizeof(a[0]));` – Jason May 13 '22 at 11:35
  • 1
    What makes you think that the code needs fixing? Please report the symptoms and other observations. – Yunnosch May 13 '22 at 11:40

3 Answers3

2

The correct syntax to call the function passing a and its size would be as shown below. There is no need to have the square brackets [] when passing a to the function.

Additionally, for passing the size of the array as the second argument, you can either use std::size which is available with C++17 or use the expression sizeof a / sizeof a[0] also shown below.

//----------v---------------->no need for the square brackets [] 
bubbleSort(a, std::size(a)); //use std::size(a) with C++17

Or

bubbleSort(a, sizeof a / sizeof a[0]); //works with all C++ versions

Working demo

Jason
  • 36,170
  • 5
  • 26
  • 60
0

When you pass an array as an argument into a function, you simply pass it as if it were a variable. In this case, simply just a would do. This is because arrays "decay" into pointers so a is a "pointer" to your array.

In addition, I recommend dividing by the sizeof(a[0]) to get the full length as the sizeof function returns the size in bytes

bubbleSort(a, sizeof(a)/sizeof(a[0]));
jloh
  • 291
  • 2
  • 8
0

Rather than pass a pointer and a length, you could pass a reference to the array.

You also don't need [] to name a;

template<size_t n>
void bubbleSort(int (&a)[n])
{
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            if (a[j] < a[j - 1])
                std::swap(a[j], a[j - 1]);
}

int main()
{
    int a[]={1,4,7,2,6,5,3,9,8,10};
    bubbleSort(a);
    for (size_t i=0;i<10;i++)
    {
        std::cout << a[i];
    }
}
Caleth
  • 52,200
  • 2
  • 44
  • 75