+ 1
i wrote this prog but it giving me different result,where am i wrong at?
int a[4]={1,2,34,56}; for (int b=0 ; b<4 ; b++){ a[b]=100; cout<<b << ":" <<a[b] <<endl; } result; 1:100 2:100 and instead of 34=100 its giving me 3=100
2 Réponses
+ 3
It's totally normal: an array is indexed by successively interger numbers starting from 0...
First element is always 0, then 1, 2, 3... and so on...
So your result, in reality, is:
0:100
1:100
2:100
3:100
If you want arbitrary indexes, you'll need to use a map like ( array of associative (key, value) pair ) instead of a basic array ;)
- 1
That's because you assign a new value before you printed it. Try this:
int a[4]={1,2,34,56};
for (int b=0 ; b<4 ; b++){
cout<<a[b] << ":";
a[b]=100;
cout<<a[b]<<endl;
}