Little help with using basic arrays

Hey guys, I'm learning the ins and outs of arrays now. I understand that the index numbering starts at 0. So in my case index 0 in my code produces a value of 1. My question is, how can I manipulate my code in my for loop for it to display this?

1: Array Location is 1
2: Array Location is 3
3: Array Location is 5
4: Array Location is 7
5: Array Location is 9

I messed around a bit with changing my for loop but I couldn't produce the result I wanted. Thanks for the help. :-)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  int main ()
{
	int ARR [5] = {1, 3, 5, 7, 9};
	int x;

	cout << "Display first value in array: " << ARR [0] << endl;
	cout << "Now Display last value in array: " << ARR [4] << endl;

	for (x = 0; x < 5; x++)
	{
	cout << x << ": Array Location" << " is " << ARR [x] << endl;
	}

system("pause");
return 0;
}
check the first cout statement of line 11 closely

cout << x << ": Array Location" << " is " << ARR [x] << endl;

you can now guess what you have to do :)
for (x = 0; x < 5; x++)
{
cout << x+1 << ": Array Location" << " is " << ARR [x] << endl;
}

This way x will increment by 1 in the cout statement each time. While to for loop runs from 0-4.
Last edited on
Thanks both of you,

But honestly I'm kind of embarrassed! I should have known that! So simple... I was a bit in a hurry and had a brain fart! Haha

Thanks again!
:-)
welcome :)
Topic archived. No new replies allowed.