Bubblesort error

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace 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

why you use reserved words as variable names?
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 .

1
2
3
for(int i=0 ; i<sizeOfArray ; i++){
std::cout << array[i] << " " ;
}
Last edited on
1
2
3
4
5
6
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
Last edited on
¿which `reserved words' are you referring to?

@OP: void Bubblesort(int array[], int n); ¿how do you expect to print void?
@ 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <cstdlib>

using namespace 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

You should take a look at comb sort, lots of fun to be had with comb sort.
Topic archived. No new replies allowed.