Thread: Using A function to reverse the order of an array

  1. #1
    Registered User
    Join Date
    Oct 2005

    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.

    void swap(double &number1, double &number2)
    	double temp; //Declaring variables
    	temp=number1; //Here the numbers are being swaped using a temporary variable

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Manassas, VA USA
    you are on the right track.. all you need to do now.. is pass in your arrays instead of double constants...

    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 )
    Last edited by The Brain; 11-04-2005 at 06:02 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Apr 2003
    Let's say you have this array:
    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?
    Last edited by 7stud; 11-05-2005 at 03:41 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM