Hi philip,
First thing to understand is that
= is the assignment operator, and
== tests for equality.
At both line 8 and 12, you are assigning
i a value, instead of checking for equality.
Second thing to understand is that the following line of code does
absolutely nothing.
sortArray[i]=sortArray[i];
What's happening here, is that the current value of sortArray is being assigned to sortArray, which is itself.
I think you're over-complicating this in one way, but forgetting an important detail.
If you're only swapping the first and last elements of the array, you don't need a for loop. No matter how huge your array is, you're only going to be working with two elements: array[0] and array[Size-1].
In order to successfully swap two elements, you need to
save the first element before you overwrite its value.
Here is an example of swapping two variables:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
int main()
{
int a = 42;
int b = 19973;
int temp = a; // we need to store a copy of a before we re-assign a to be a new number
a = b; // a becomes b
b = temp; // b becomes the original value of a
// (if we had done "b = a;" here, that would have just made b become its original value,
// because a was already assigned the value of b.)
std::cout << " a = " << a << ", b = " << b << "\n";
}
|
Can you apply your array to this?