Hello, I am in the process of creating a program that accepts random numbers, put them in an array and sort them using different
types of sorting algorithms. I then passed the array as an argument to the "BubbleSort" function, however, when I try to call
the array in the main() function, an error appeared saying "argument of type "int" is incompatible with parameter of type "int *"
Why did that appear? Did I overlook something in the program? It's not finished yet, but here it is:

    #include <iostream>
    using namespace std;

    void BubbleSort(int arr[], int n); 
    int main()
    {

        int num, ch, i;
        int arr[999] = {};

        cout << "Enter number of nodes: ";
        cin >> num;

        for(i=0; i<num; i++)
        {
            cout << "Enter node: ";
            cin >> arr[i];
        }

        cout << "\nHere are the unsorted nodes: ";
        for(int i=0; i<num; i++)
        {
            cout <<""<<arr[i]<<" ";
        }

        choice:
        cout << "\n\nChoices are:";
        cout << "\n[1] Bubble Sort"
             << "\n[2] Insertion Sort"
             << "\n[3] Selection Sort"
             << "\n[4] Exit"
             << "\nEnter choice: ";

        cin >> ch;

        switch(ch)
        {
        case 1:
            cout << "\nBubble Sort" <<endl;
            BubbleSort(arr[i], num);
            cout << "\n"<<arr[i]<<" ";
            break;

        case 2:
            cout << "\nInsertion Sort" <<endl;
            break;

        case 3:
            cout << "\nSelection Sort" <<endl;
            break;

        case 4:
            exit(1);
            break;

        default:
            cout << "\nInvalid entry, try again";
            goto choice;
            break;
        }

        system("pause>null");
        return 0;
        }

        void BubbleSort(int arr[], int n) 
        {
          bool swapped = true;
          int j = 0;
          int tmp;
          while (swapped) {
                swapped = false;
                j++;
                for (int i = 0; i < n - j; i++) {
                      if (arr[i] > arr[i + 1]) {
                            tmp = arr[i];
                            arr[i] = arr[i + 1];
                            arr[i + 1] = tmp;
                            swapped = true;
                      }
                }
          }
        }

Any help would be highly appreciated, I'll be glad if someone will enlighten me with this matter. Thanks! :)

Recommended Answers

All 6 Replies

@line 40
pass the array like this:

BubbleSort(arr, num);

@line 41
are you trying to print the whole array? you could use a loop for that

Hey Thank you so much! I used a for loop for printing the whole array after it has been sorted, just like you said. But what if I want to display or print
the passes that happened during the loop, do I need to create a separate function for that or just "cout" after every swap? :)

But what if I want to display or print the passes that happened during the loop

you could print out the variable you need to display before passing it to a function or doing something to it

other than that if you need something else entirely then could you post a sample code of what you need so I can understand it better

The output should be like this:

    Enter number of nodes: 4
    What are the unsorted nodes? 12 6 4 8 

    Choices are: 
    [1] Bubble Sort 
    [2] Insertion Sort 
    [3] Selection Sort 
    [4] Exit 
    Enter your choice: 1 

    Bubble Sort including passes: 
    12 6 4 8
    6 12 4 8
    6 4 12 8
    6 4 8 12
    4 6 8 12

Something like that? but anyway I was just wondering so you know :)

You could simply print out the current array after a swapping is done

Thanks! I'll be working on that :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.