For some reason i get ahuge - number idk whats wrong

#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>

using namespace std;

int main(){
int results [6];
int max = 5;
int holder = 0;

cout << "input the 5 students results" << endl;

for(int i=0; i< max; i++){
cin >> results [i];
cout << endl;
}

for(int i=0; i< max; i++){

for(int i=0; i< max; i++){

if(results[i] > results[i+1]){
holder = results [i+1];
results [i+1] = results [i];
results [i] = holder;

i--;
}
}
}

for(int i=0; i<5; i++){
cout << results [i] << endl;
}


system("pause");
}






i have no idea whats wrong with this, when i run it it re assigns a number to -858993460
Last edited on
see your i--;
1
2
//change to
i++;
The first thing: the inner loop must not have i as the index

second: since i ends at max on the very last iteration i+1 is out of bounds.
i.e. the inner loop must end at max - 1
@coder777


You are wrong. He defined the array as having max + 1 elements.:)

But he did not initialize the max element.:)
i changed the final for loop to:

for(int i=1; i<6; i++){
cout << results [i] << endl;
}

and its working now
Last edited on
and its working now
no, it isn't


You are wrong. He defined the array as having max + 1 elements
not physically, but logically
Topic archived. No new replies allowed.