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