Nov 20, 2013 at 7:47am UTC
Please, why I get numbers which are not array values?
Many thanks!
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
#include<iostream>
using namespace std;
int main(){
int c=40;
int array_f[5]={17, 19, 23, 29, 31};
cout<<array_f[0]<<" " <<array_f[1]<<" " <<array_f[2]<<" " <<array_f[3]<<" " <<array_f[4]<<endl<<endl;
if (c<array_f[0]){
cout<<array_f[0]<<endl;
}
if (c>array_f[4]){
cout<<array_f[4]<<endl;
}
for (int j=0;j<5;j++) {
if (c>=array_f[j]) {
if (c-array_f[j]<=array_f[j+1]-c){
cout<<array_f[j]<<" " <<"@" <<endl;
}
else {
cout<<array_f[j+1];
}
break ;
}
}
return 0;
}
Last edited on Nov 20, 2013 at 7:58am UTC
Nov 20, 2013 at 9:41am UTC
What is your expected output?
Nov 20, 2013 at 11:17am UTC
Loop on line 22 iterates the case j==4.
=>
Condition on line 26 will and statement on line 32 can dereference element array_f[5], which does not exist.