# Thread: Swapping Elements in an Array

1. ## Swapping Elements in an Array

I thought I'm done doing mg activity, but my professor said that we need to use a Temporary Variable for Swapping and I have no idea where to put it. Mind giving me a hand, please?

Here is his activity:

Activity: Swapping
Create a program that accepts a 10-element array of type int. Where the 1st user-input is stored in the 1st element of the array; the 2nd user-input is stored in the 2nd element of the array; so on so forth until the last user-input stored in the last element of the array.
Your source code should be able to SWAP the values of the 1st and 10th; 2nd and 9th; 3rd and 8th; 4th and 7th; and 5th and 6th elements. It should display the values of the original and the swapped values of the array.
example:
Enter 10 integer values:
array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5
array[5] = 6
array[6] = 7
array[7] = 8
array[8] = 9
array[9] = 10
Swapped:
array[0] = 10
array[1] = 9
array[2] = 8
array[3] = 7
array[4] = 6
array[5] = 5
array[6] = 4
array[7] = 3
array[8] = 2
array[9] = 1
NOTE: SWAPPING is DIFFERENT from REVERSE.The values of each element in the array should be swapped. I'll check your codes next meeting.

MY CODE:
Code:
```#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
int array [10];

for ( int i = 0; i <=9; i++ )
{
cout <<"array [" << i << "] = "; cin >> array [i];
}

cout <<"Swapped: \n";

cout <<"array [0] = " << array [9];
cout << endl;
cout <<"array [1] = " << array [8];
cout << endl;
cout <<"array [2] = " << array [7];
cout << endl;
cout <<"array [3] = " << array [6];
cout << endl;
cout <<"array [4] = " << array [5];
cout << endl;
cout <<"array [5] = " << array [4];
cout << endl;
cout <<"array [6] = " << array [3];
cout << endl;
cout <<"array [7] = " << array [2];
cout << endl;
cout <<"array [8] = " << array [1];
cout << endl;
cout <<"array [9] = " << array [0];
cout << endl;

getch ();
return 0;

}```

Thank you.

2. You should write a loop to do that swapping. The variable that your professor suggested can then be declared just before the loop or inside the loop body.

By the way, when your loop condition should be i < 10 instead of i <= 9. This will come in handy when you use a named constant for the array size instead of magic numbers like 9 or 10. Of course, for the loop that will reverse the elements of the array, you need to think about what the condition will be.