how do I reverse order of my array

This is a discussion on how do I reverse order of my array within the C++ Programming forums, part of the General Programming Boards category; How do I reverse the order of this array? Jose Code: #include <iostream.h> #include <stdlib.h> const int myarraysize = 5; ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    21

    how do I reverse order of my array

    How do I reverse the order of this array?

    Jose

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    const int myarraysize = 5;
    
    void Fill_array(double myarray[], int j);
    void Show_array(double myarray[], int j);
    void Reverse_array(double myarray[], int j);
    
    int main()
    {
         cout << "starting in main" << endl;
    
         double uservalues[myarraysize] = {};
    
          Fill_array(uservalues, myarraysize);
          Show_array(uservalues, myarraysize);
          Reverse_array(uservalues, myarraysize);
          Show_array(uservalues, myarraysize);
    
          cout << "back in main" << endl;
    
          system("PAUSE");
          return 0;
    }
    
    void Fill_array(double myarray[], int j)
    {
        cout << "I'm in Fill array" << endl;
        int numberofentries = 0;
        int counter = 0;
    
        for (int i = 0; i < j; i++)
            {
            cout << "Enter a value to be stored in array" << endl;
            cin >>  myarray[i];
            counter = counter + 1;
            }
    
        numberofentries = counter;
        cout << "Values you entered: " << numberofentries << endl;
    
    }
    
    void Show_array(double myarray[], int j)
    {
    
        cout << "Now I'm in Show array" << endl;
        cout << "This is what you entered into the array" << endl;
        for (int i = 0; i < j; i++)
            {
            cout << myarray[i] << endl;
            }
    
    }
    
    void Reverse_array(double myarray[], int j)
    {
        cout << "Now I'm in Reverse array" << endl;
        myarray[0] = myarray[4];
        myarray[1] = myarray[3];
        myarray[2] = myarray[2];
        //myarray[3] = myarray[4];
        //myarray[4] = myarray[3];
    
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Traverse the array from the beginning until you reach the middle. Swap the first element with the last, second element with the second last etc...
    Code:
    void SwapValues(double* Value1, double* value2)
    {
       double Temp = *Value1;
       *Value1 = *Value2;
       *Value2 = Temp;
    }
    
    void ReverseArray(double MyArray[], int SizeOfArray)
    {
       for(int i = 0; i < (int)(SizeOfArray / 2); i++)
       {
          SwapValues(&MyArray[i], &MyArray[SizeOfArray - i - 1]);
       }
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    use a temporary variable to swap values:

    Code:
    void reverseArray(int * array, int num)
    {
       int temp;
       int i;
       int number = num - 1;//assumes num is number of ints in array
       for(i = 0; i < num/2; i++, number--)
       {   
           temp = array[number]; 
           array[number] = array[i];
           arrary[i] = temp;
        }
    }

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Allow me to introduce something wonderful:

    S T L !!!

    Code:
    #include <algorithm>
    
    void ReverseArray(double* array, int size)
    {
     std::reverse(array,array+size);
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if you have access to STL, go for it! If you want to know what STL is doing, study the other two posts.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    I believe this would work too (incomplete but you can fill in whats needed, which isn't much)-

    Code:
    for(y = (j-1); y >= 0; y--)
        {
             temparray[y] = myarray[x];
             x++;
        }
    	for(x=0; x < j; x++)
        {
    	   myarray[x] = temparray[x];
        }

    There, that works. Fairly simple to understand, too, IMHO.
    Last edited by Captain Penguin; 09-25-2002 at 07:12 PM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    Thanks all that helped, I am newbie to C++ and am taking a college course this semester. This is my final working program.

    God bless,
    Jose

    Code:
    //Jose Gonzales, #c++04, 95
    
    #include <iostream.h>
    #include <stdlib.h>
    const int myarraysize = 5;
    int baddata = 0;
    
    void Fill_array(double myarray[], int j);
    void Show_array(double myarray[], int j);
    void Reverse_array(double myarray[], double myrevarray[], int j);
    void Swap_array(double myarray[], double myrevarray[]);
    
    int main()
    {
    
         double uservalues[myarraysize] = {};
         double uservaluesreverse[myarraysize] = {};
    
    
          cout << "\n";
          cout << "Enter " << myarraysize <<" double values to be stored in array" << flush << endl;
          Fill_array(uservalues, myarraysize);
          cout << "\n";
    
          if (baddata == 0)
          {
          cout << "This is what you entered into the array" << endl;
          Show_array(uservalues, myarraysize);
          cout << "\n";
          Reverse_array(uservalues, uservaluesreverse, myarraysize);
          cout << "Now here are your values in reverse order" << endl;
          Show_array(uservaluesreverse, myarraysize);
          cout << "\n";
          Swap_array(uservalues, uservaluesreverse);
          cout << "your values in reverse order all but 1st and last elements of array" << endl;
          Show_array(uservalues, myarraysize);
          }
          cout << "\n";
          cout << "\n";
          cout << "\n";
          cout << "\n";
          cout << "\n";
    
          system("PAUSE");
          return 0;
    }
    
    void Fill_array(double myarray[], int j)
    {
        int numberofentries = 0;
        int counter = 0;
    
        for (int i = 0; i < j; i++)
            {
                 if (!cin)
                 {
                 cin.clear();
                 cout << "bad input, goodbye." << endl;
                 cout << "\n";
                 baddata = 1;
                 break;
                 }
                 else
                 {
                 cin >>  myarray[i];
                 counter = counter + 1;
                 }
            }
    
        numberofentries = counter;
        cout << "total number of entries: " << numberofentries << endl;
    }
    
    void Show_array(double myarray[], int j)
    {
        for (int i = 0; i < j; i++)
            {
            cout << myarray[i] << endl;
            }
    }
    
    void Reverse_array(double myarray[], double myrevarray[], int j)
    {
        for (int i=0; i<j; i++)
            {
                myrevarray[i] = myarray[j - i -1];
            }
    }
    
    void Swap_array(double myarray[], double myrevarray[])
    {
        myarray[1] = myrevarray[1];
        myarray[2] = myrevarray[2];
        myarray[3] = myrevarray[3];
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. printing array elements in ascending order
    By galmca in forum C Programming
    Replies: 29
    Last Post: 10-24-2004, 11:24 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. read into array and spit out in reverse order
    By steven in forum C Programming
    Replies: 4
    Last Post: 09-07-2001, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21