+ 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

24th Dec 2016, 10:00 AM
Aqib Asmat
Aqib Asmat - avatar
2 Antworten
+ 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 ;)
24th Dec 2016, 10:16 AM
visph
visph - avatar
- 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; }
24th Dec 2016, 1:22 PM
Boris Batinkov
Boris Batinkov - avatar