Bubble sort problem.

Hello, the program works but the problem is the sorted numbers doesnt appear. All it brings up is 1 and that's all. Where is the error?
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
#include <iostream>

using namespace std;

int const N = 20;

void pirmaisMasivs(int N);

int main (){

 cout << "Numbers being sorted - 5,4,2,6,1,3,8,9,10,7 > " << pirmaisMasivs;

}


void pirmaisMasivs(int N){
 int temp;
 int masivs[10] = {5,4,2,6,1,3,8,9,10,7};
 for( int i = 0; i < N - 1; i++ ){
    for( int j = 0; j < N - 1; j++ ){
        if( masivs[ j ] > masivs[ j + 1 ]){
        temp = masivs[ j ]; 
        masivs[ j ] = masivs[ j + 1];
        masivs[ j + 1 ] = temp;
 }
 }
 }


}
the program works but the problem is the sorted numbers doesn't appear

Then it's not working, is it?

Line 11: You're printing the value of a function pointer.

Line 30: The sorted array (masivs) goes out of scope when pirmaisMasivs exits.

So, what should I do exactly?
1) Move line 18 to main (line 10).
2) Pass masivs as an argument to pirmaisMasivs.
3) Remove pirmaisMasivs from line 11.
4) Add a call to pirmaisMasivs().
4) Add a loop at line 12 to print the contents of masivs

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
#include <iostream>
using namespace std;

int const N = 10;   //  This should be 10, not 20

void pirmaisMasivs(int masivs[], int N);

int main ()
{   int masivs[N] = {5,4,2,6,1,3,8,9,10,7};  // Declare in main
    
    cout << "Numbers being sorted - 5,4,2,6,1,3,8,9,10,7 > " << endl; 
    pirmaisMasivs (masivs, N);
    for (int i=0; i<N; i++)
        cout << masivs[i] << endl;    
    system ("pause");        
}


void pirmaisMasivs(int masivs[], int N)
{   int temp;
 
    for( int i = 0; i < N - 1; i++ )
    {   for( int j = 0; j < N - 1; j++ )
        {   if( masivs[ j ] > masivs[ j + 1 ])
            {   temp = masivs[ j ]; 
                masivs[ j ] = masivs[ j + 1];
                masivs[ j + 1 ] = temp;
            }
        }
    }
}


Thanks a million!!! One more question, how do I cout numbers that are in line 9 - using function?
Last edited on
Ok, nevermind, got it myself ;D
Topic archived. No new replies allowed.