array problem

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int x [5] ={2,2,1,3,1,};


int y;
y=x[1]+1;

cout<<y<<endl;

x[4]=1;
cout <<x[x[4]]<<endl;// Explain how this printed 2 ?
y--;

cout<<y<< " "<<x[y]<<endl;


return 0;

}

/*
the answer is

3
2 // why is it 2 here?
2 1



i looked at the x[4] which hold 1
*/
Last edited on
So the line
 
cout << x[x[4]] << endl;  // Explain how this printed 2 ? 

It prints 2 since you're accessing the value of the x at index x[4].

This basically is x[1] in this case since the value at index x[4] is 1.
Topic archived. No new replies allowed.