Possible to reverse numbers in an array?

This is a discussion on Possible to reverse numbers in an array? within the C++ Programming forums, part of the General Programming Boards category; I know it's possible to reverse numbers of a list, but is it possible to do it when using an ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    20

    Possible to reverse numbers in an array?

    I know it's possible to reverse numbers of a list, but is it possible to do it when using an array?

    For an example here is a simple program that asks the user to enter the amount of numbers in the array, asks the users to enter the numbers in the array, and then prints them out.

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
        int SIZE;
        
        int counter;
        
        cout << "Enter the size of the array: ";
        cin >> SIZE;
        int number[SIZE];
        for (counter = 0; counter < SIZE; counter++)
        {
            cout << "Enter number " << (counter + 1) << ": " ;
            cin >> number[counter];
        }
        
        
        for (counter = 0; counter < SIZE; counter++)
        {
            cout << number[counter] << " ";
        }
        cout << endl;
        
        return 0;
    }
    If I enter 1 2 3 4 5 in, is it possible with the code I have, to make the numbers come out 5 4 3 2 1?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Sure. Just start counting from the end of the array instead of the beginning and decrement counter.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    Or access number[SIZE - 1 - i] even.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    Quote Originally Posted by Clairvoyant1332 View Post
    Sure. Just start counting from the end of the array instead of the beginning and decrement counter.
    Well that's how I figured you would do it but how do you start at the end of the array since the user gets the enter the number of the array.

    Like if I knew the array was a size ten I could do
    Code:
     for (count = 10; count > number; count--)
    Since I don't know the size of the array, not sure how to do it.

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Try knowing about dynamic array creation or if you still want to use static ones, in the very start, try initializing your array with null and then after getting input from user, search through the array unless you arrives null and get the index. From here continue, decrementing the loop and printing it. Good Luck.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  6. #6
    Registered User inequity's Avatar
    Join Date
    Nov 2010
    Location
    Seattle, Washington
    Posts
    59
    Quote Originally Posted by theCanuck View Post
    I know it's possible to reverse numbers of a list, but is it possible to do it when using an array?
    Heh, in practice this is generally more complex to do with a list.

    There are a few ways you could probably do this though.

    You could write a function to reverse an array of any size. This isn't very hard and is good practice.
    But the idea for reversing an array, or ANY range of numbers, is you would store two indexes, one at the first element in the array (let's call it START), the other at the end of the array (let's call that END).
    Then you would loop through half the array, each time switching array[START + i] and array[END - i], where i represents iterations the loop has completed.

    With this method you could reverse a subset of your array.

    Or you could print them starting from the end and moving backwards. Or you could have the numbers stored in the array starting at the end.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to reverse numbers with sizeof operator
    By JoelearningC in forum C Programming
    Replies: 13
    Last Post: 03-09-2008, 11:53 AM
  2. Help with Reverse Array
    By Crcullen3916 in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2007, 08:47 PM
  3. How do you reverse an array?
    By ducksickii in forum C Programming
    Replies: 25
    Last Post: 04-01-2007, 05:39 AM
  4. reverse an array - help appreciated
    By Vireyda in forum C++ Programming
    Replies: 2
    Last Post: 03-21-2004, 11:52 AM
  5. how do I reverse order of my array
    By jgonzales in forum C++ Programming
    Replies: 6
    Last Post: 09-25-2002, 03:48 PM

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