-3

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?

#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;
 }
 }
 }


}
Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160
user6200539
  • 3
  • 1
  • 2

1 Answers1

1

Your code has several problems.

First your declare your overall count of numbers to be sorted as int const N = 20;, but later you use 10 as a fixed literal count. The N = 20 is apparently wrong. The loops will cause an overrun of the array bounds.

Another problem is, that you do not output your sorted array in any means. You just try call the sort method. Furthermore, you declare your array of integers to be sorted within your sort method, so you have no chance to access the sorted array outside of this method to print it.

Here is a completely reworked version of your program:

#include <iostream>

using namespace std;

int const N = 10;
int masivs[N] = {5,4,2,6,1,3,8,9,10,7};

void pirmaisMasivs() {   
    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;
            }
        }
    }
}

void printMasivs() {
    for( int i = 0; i < N; i++ ) {
        if ( i == 0 ) {
            cout << masivs[ i ];
        }
        else {
            cout << ", ";
            cout << masivs[ i ];            
        }
    }
}

int main () {
    cout << "Numbers being sorted:\n";
    printMasivs();
    cout << "\n";
    pirmaisMasivs();
    cout << "\n";
    printMasivs();
}
cwschmidt
  • 1,194
  • 11
  • 17