I have some homework where I have to use Bubblesort to arrange 500 randomly generated numbers. (In code only 10 until it works). But on line 55 I am getting an error that says:
no match for 'operator<<'in 'std::cout << Bubblesort[[[int*][&array]], n]'
Any help on this error would be much appreciated :D
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int i;
int j = 0;
int tmp;
int count;
int array[10];
int n;
void Bubblesort(int array[], int n)
{ //start Bubblesort
int tmp;
int w;
int q;
int l;
for (q=0; q < n-l; q++)
{ //start for
for (w=0; w < n-l; w++)
{ //start for
if (array[w] > array[w+l])
{ //start if
tmp = array[w];
array[w] = array[w+l];
array[w+l] = tmp;
} //end if
} //end for
} //end for
} //end Bubblesort
int main()
{ //start main
cout << "The numbers in the array are: ";
srand (time(0));
for (int j = 0; j < 10; j++)
{ //start for
i = rand() % 101; //creates and adds numbers to the array
array[j] = i;
} //end for
for (int k = 0; k < 10; k++)
{ //start for
cout << array[k] << " "; //displays unordered array
} //end for
cout << endl;
cout << "The Array after being Bubblesorted is: " << endl;
cout << Bubblesort(array, n) << " ";
cout << endl;
system("pause");
return 0;
} //end main
You are getting an error because "operator <<" does not know how to handle Bubblesort().
Remove " cout << " in front of BubbleSort.
You can either print the elements in the BubbleSort function itself. or
in the main function using loop .
cout << "The Array after being Bubblesorted is: " << endl;
Bubblesort(arr, n);
for (int k = 0; k < 10; k++)
{
cout << arr[k] << " ";
}
I changed int array[10] to int arr[10]
I used that and the program worked.. but it's not sorting
what's the value of n when the Bubblesort function is about to be called..
There's no value at n
@ Raman009
Thanks, I tried to print the result within the Bubblesort function only to get "44x0303" to display. So I made it print in a for loop and it worked fine.
@iantac
Just the way I have been taught, I normally declare all the variables I use then get rid of redundant code at the end.
And I was running into the same problem of it not sorting, but my problem was when I learned Bubblesort today I brain farted and saw the "1" as an "L". So I changed all the l's to 1's and set int n=10; and I got it to work.
Thanks for all the help! Here is the finished product.
#include <iostream>
#include <cstdlib>
usingnamespace std;
int i;
int tmp;
int array[500];
int n = 500;
void Bubblesort(int array[], int n)
{ //start Bubblesort
int tmp;
int j;
int q;
for (q=0; q < n-1; q++)
{ //start for
for (j=0; j < n-1; j++)
{ //start for
if (array[j] > array[j+1])
{ //start if
tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
} //end if
} //end for
} //end for
} //end Bubblesort
int main()
{ //start main
cout << "The numbers in the array are: ";
for (int j = 0; j < n; j++)
{ //start for
i = rand() % 101; //creates and adds numbers to the array
array[j] = i;
} //end for
for (int k = 0; k < n; k++)
{ //start for
cout << array[k] << " "; //displays unordered array
} //end for
cout << endl << endl;
cout << "The Array after being Bubblesorted is: " << endl;
Bubblesort(array, n);
for (int i=0; i < n; i++)
{
cout << array[i] << " " ;
}
cout << endl;
system("pause");
return 0;
} //end main