Swapping Array elements using temp variable.

I'm trying to swap the contents of an array to get the array to display in reverse. The array has 12 elements so I need to swap the first with the last etc.
I had this problem solved with a different solution but was told I needed to swap the elements instead.

The array values are read in from a file.

I've been looking online for help and I've attempted this many times using a temp variable to swap the values but I can't get it to work out correctly. Do I need two for loops for this or will it work with one?



I don't understand whether or not I need a second for loop ie. J and i or is one enough? I've also tried with just 1 for loop letting list[i]=list[11];
But I can't figure it out.

some guidance or a similar example would be greatly appreciated.

Thank you.

Here is one of my attempts. It's obviously not correct. It's just giving me random negative numbers where I have swapped the elements.
1
2
3
4
5
6
7
8
9
10
11
12
13
  for (int i = 0; i < 6; i++)
				for (int j = 12; j >6;j--)
			{
				infile >> list[i];
				int temp = list[i];
				list[i] = list[j];
				list[j] = temp;

				cout << list[i] << list[j];

				

			}
one loop is enough

1
2
3
4
5
6
7
int size = 12;

for (int i = 0; i<size; ++i)
{
    temp = list[i];
    list[size-i] = temp;
}
Thanks. Would I read in the contents from the file just before the line temp=list[I]
Just read the file once at the beginning. No need to read it inside the for loop again and again.
Topic archived. No new replies allowed.