- 1

What are the contents of this array?

int x [4]; for(int i=0; i <4; i++) { x [i]=i+1; if (i>=2) x [i-1]=×[i]+2; } //Display contents of x. for (i=0; i <4; i++) cout << x [i] << " ": How would you solve this?

26th May 2017, 2:24 AM
Sami
4 odpowiedzi
+ 8
1 5 6 4 First of all there are a lot of syntax errors, which I am will assume to be correct. x [i] = i+1 gives 1,2,3,4 as the numbers. But, if i>=2, x [i-1]=x [i]+2 //x [1]=x [2]+2=3+2=5 //x [2]=x [3]+2=4+2=6 Giving the array, 1 5 6 4
26th May 2017, 2:26 AM
Pixie
Pixie - avatar
+ 3
Edited
26th May 2017, 2:30 AM
Pixie
Pixie - avatar
- 1
Please show how you got that.
26th May 2017, 2:27 AM
Sami
- 1
Makes sense, thank you!!
26th May 2017, 2:37 AM
Sami