Array one dimension

Hello guy. can you help me
I confuse in line 16,17,18.
Why lastly the output is more than three.

where does the value of 8,3,-1 come from?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
	int temp[5];

	for (int i = 0; i < 5; i++)
		temp[i] = 2 * i - 3;

	for (int i = 0; i < 5; i++)
		cout << temp[i] <<"__";
	cout << endl;

	temp[0] = temp[4];
	temp[4] = temp[1];
	temp[2] = temp[3] + temp[0];

	for (int i = 0; i < 5; i++)
		cout << temp[i] << " "<<endl;
	cout << endl;

}  Put the code you need help with here.



Output
1
2
3
4
5
6
7
8
-3 -1 1 3 5


5
-1
8
3
-1
Here is a hint:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

void status( const char * name, const int arr[], int num ) {
  for ( int i = 0; i < num; ++i ) {
    std::cout << name << '[' << i << "] == " << arr[i] << '\n';
  }
  std::cout << '\n';
}

int main()
{
  int temp[5];

  for (int i = 0; i < 5; i++)
    temp[i] = 2 * i - 3;

  status( "temp", temp, 5 );
  temp[0] = temp[4];
  status( "temp", temp, 5 );
  temp[4] = temp[1];
  temp[2] = temp[3] + temp[0];

  return 0;
}

Do you notice the effect of line 18?
Can you use the same method to figure out the rest?
Topic archived. No new replies allowed.