+ 1
From Where does the last 5 come?
#include <iostream> using namespace std; int main() { int arr[]={11,17,15,15,85}; for(int i=0;i<=5;i++) { cout<< arr[i] << endl ; } return 0; } /* output 11 17 15 15 15 85 5 */ /* From Where does the last 5 come? */
4 Answers
+ 7
Your array has 5 elements which have numbers 0 to 4, but in for condition there is i<=5, so it processes elements 0 to 5. Since the 5th element of the array does not exist, the program shows the content of the byte in ram which is five bytes right from the first byte of array. Actually, the program must raise runtime error, but c++ is always unpredictable.
+ 6
Replace <= with < and your program should run fine. Ahtoh (?) has explained the reason behind this occurrence, it's always good to know that arrays may store stray values so take heed.
+ 1
thank you
0
remove 'greater than or equal to' to' greater than'