-
Switching arrays
I have been trying this for a week and i still dont understand how to do it. I have to reverse the elements of an a array. I turned the problem into the teacher already and she said all i did was print them backwards and not reversed the elements. I know how to exchange values but i just cant figure this problem out.
this is my array.....can u help me reverse the elements..??
ary[MAX]={89,92,56,34,28};
for (i=0;i<MAX;i++)
thanx.
-
Option 1:
Create a second array of the same size. Then perform the following algorithm:
array_new [0] = array_old [N]
array_new [1] = array_old [N-1]
...
array_new [N] = array_old [0]
Then you could copy array_new to array_old so array_old is reversed. Or you could change pointer of something different.
Option 2:
Use a temporary variable to keep old value and use this for switching. Algorithm:
temp_value = array [0]
array [0] = array [N]
array [N] = temp_value
...etc. Take care if the numer of elements is odd.