# Using A function to reverse the order of an array

• 11-04-2005
jaisch
Using A function to reverse the order of an array
I am supposed to use a void function that reverses two values to rever se the order of an array. I have written the function to reverse the two values which is below, but now I am confused about how to use this to reverse the entire array.

Code:

``` void swap(double &number1, double &number2) {         double temp; //Declaring variables         temp=number1; //Here the numbers are being swaped using a temporary variable         number1=number2;         number2=temp;         return; }```
• 11-04-2005
The Brain
you are on the right track.. all you need to do now.. is pass in your arrays instead of double constants...

pseudo:
Code:

```void function(double  my_array_1[], double my_array_2[]) {       //Use a 'for' loop here based array size           //assign the contents of one array into a 'temp' variable      temp = array1[i];           //assign the back of one array..  into the beginning of the other one  array1[i] = array2[j];           //assign the 'temp' variable to the back of the second array  array2[j] = temp; }```

tip: you don't need to loop through the entire arrays.. you only need to loop 1/2 way through each array ( array_size / 2 )
• 11-05-2005
7stud
Let's say you have this array:
Code:

`int nums[] = {10, 20, 30, 40};`
If you swap the first element of the array with the second element, and then you swap the second element with the third element, and then the third element with the forth element, where will the 10 be? Ok, so that's a start.

Next, you need to swap the new first element(which used to be 2nd) across the length of the array, however when you get to the end of the array, do you want to swap the 3rd and 4th element?