I am asked to create a dynamic array of n integers and to sort the array using b
ID: 3776264 • Letter: I
Question
I am asked to create a dynamic array of n integers and to sort the array using bubble sort (ascending and descending).
I have my bubble sort below. In bubble sort, my order calls the swap and depending on the choice it will either call ascending
or descending.
void bubble_sort(int **array, int size, bool choice) {
void order(int **, int **);
orderBY_AOD = choice;
int k = 0;
for (int j = 0; j < size - 1; j++) {
for (j + 1; k < size; k++) {
order(array + j, array + k);
}
}
The problem I have is in my main, when I call the bubble sort method.
In my main
// I initialize methods that were created outside main
int arraySize;
// I ask for user input
int *a = new int [arraySize];
// I ask for user to input values for array
//Then I call bubble sort the array
bubble_sort(a, arraySize, true); //Calling bubble sort, but I get an error. It says "type int* is incompatible with parameter of type int**"
I'm not sure how to fix this problem.
Explanation / Answer
Below is the code I had compiled on CodeChef IDE in C++ :
#include <iostream>
using namespace std;
void bubble_sort(int **array, int size, bool choice)
{
cout<<"hello";
}
int main() {
int arraySize=10;
int *a = new int [arraySize];
bubble_sort((int **)a, arraySize, true); // change in the code
return 0;
}
the only change that you need to make is to type cast the single pointer to double pointer.
Also, you can use the signature as : void bubble_sort(int *array[], int size, bool choice)
In C/C++, array parameters are considered as pointers. so a double pointer or an array of pointer is same when they are parameters.
feel free to ask if you have any doubt :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.