0

Kindly explain second cout statement..how n[i]=i+100 and n[j] works

#include <iostream> #include<iomanip> using namespace std; using std::setw; int main() { int i,j; int n[10]; for(i=0;i<10;i++) { n[i]=i+100; } cout<<"element"<<setw(10)<<"value"<<endl; for(j=0;j<10;j++) { cout<<setw(5)<<j<<setw(10)<<n[j]<<endl; } return 0; }

21st Dec 2018, 6:44 AM
Usman Zafar
Usman Zafar - avatar
2 Answers
+ 5
`n[i] = i+100;` gets assigned 10 times to an expression inside a loop like this i = 0, n[0] = 0+100; i = 1, n[1] = 1+100; i = 2, n[2] = 2+100; i = 3, n[3] = 3+100; i = 4, n[4] = 4+100; i = 5, n[5] = 5+100; i = 6, n[6] = 6+100; i = 7, n[7] = 7+100; i = 8, n[8] = 8+100; i = 9, n[9] = 9+100; `cout << n[j] << endl;` prints out the values inside the array using another loop like so j = 0, n[0] == 100, print out 100 j = 1, n[1] == 101, print out 101 j = 2, n[2] == 102, print out 102 j = 3, n[3] == 103, print out 103 j = 4, n[4] == 104, print out 104 j = 5, n[5] == 105, print out 105 j = 6, n[6] == 106, print out 106 j = 7, n[7] == 107, print out 107 j = 8, n[8] == 108, print out 108 j = 9, n[9] == 109, print out 109
21st Dec 2018, 8:16 AM
Babak
Babak - avatar
+ 1
Its realy appreciable effort from c++ soldier... Thanks alot sir....
22nd Dec 2018, 1:03 AM
Usman Zafar
Usman Zafar - avatar