Displaying Array numbers in reverse order?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Displaying Array numbers in reverse order?

  1. #1
    Join Date
    Nov 2017
    Posts
    1

    Displaying Array numbers in reverse order?

    Hello, I have an assignment due tomorrow, which requires a basic array code to enter 5 numbers and have those numbers be displayed in reverse order. Here's the specific assignment instructions:

    Create an array of type double that can hold up to 5 elements. Ask the user for 5 numbers. Display those numbers in the reverse order from which they were entered.

    Note: this assignment must use loops. I should be able to change a single variable from 5 to 10 and have it work correctly.


    I have constructed a basic, working array code here:

    Code:
      #include <iostream>
    using namespace std;
    
    int main() 
    {
        const int size = 5;
        int myarr[5], total = 0;
        cout << "Please enter 5 numbers below: " << endl;
        
        for (int i = 0; i < 5; i++) 
        {
            cin >> myarr[i];
            total += myarr[i];
        }
        
        cout << "Total = " << total << endl;  
    
        system("pause");
        return 0;
    }
    I tried to insert a while N loop to make the numbers be displayed in reverse.

    Code:
      #include <iostream>
    using namespace std;
    
    int main() 
    {
        const int size = 5;
        int myarr[5], total = 0;
        int n, reversedNumber = 0, remainder;
        int i;
        cout << "Please enter 5 numbers below: " << endl;
        
        for (int i = 0; i < 5; ++i); 
        {
            cin >> myarr[i];
            total += myarr[i];
        }
        while(n != 0)
    {
            remainder = n%5;
            reversedNumber = reversedNumber*5 + remainder;
            n /= 5;
        }
        
        cout << "Reversed Order = ";
        cout << "Total = " << total << endl;  
        
        return 0;
    }
    However, it only allows one number to be typed and it fails to display the entered numbers in reverse. What am I doing wrong? How do I insert code to display the numbers entered in reverse correctly?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,872

    Re: Displaying Array numbers in reverse order?

    One way is to decrement through the array using a for loop that starts at size - 1 and decrements the index rather than incrementing it. Another way is to use a reverse iterator.

    Note that in the posted code, whilst you have set constant size to 5, you have hard-coded 5 into the program - rather than using the constant size.

    Why are you totalling the entered values when just a reverse display is wanted?

    PS The assignment requires an array of double, but the code has an array of int!
    Last edited by 2kaud; November 20th, 2017 at 11:07 AM. Reason: PS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    19,008

    Re: Displaying Array numbers in reverse order?

    Quote Originally Posted by JustMcCollum View Post
    ...
    I tried to insert a while N loop to make the numbers be displayed in reverse.

    Code:
      #include <iostream>
    using namespace std;
    
    int main() 
    {
        const int size = 5;
        int myarr[5], total = 0;
        int n, reversedNumber = 0, remainder;
        int i;
        cout << "Please enter 5 numbers below: " << endl;
        
        for (int i = 0; i < 5; ++i); 
        {
            cin >> myarr[i];
            total += myarr[i];
        }
        while(n != 0)
    {
            remainder = n%5;
            reversedNumber = reversedNumber*5 + remainder;
            n /= 5;
        }
        
        cout << "Reversed Order = ";
        cout << "Total = " << total << endl;  
        
        return 0;
    }
    However, it only allows one number to be typed and it fails to display the entered numbers in reverse. What am I doing wrong? How do I insert code to display the numbers entered in reverse correctly?
    Your n is initially undefined (uninitialized)...
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)