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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #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.

My professor only wants us to use loops, not reverse iterators. What am I doing wrong? How do I insert code to display the numbers entered in reverse correctly?
Last edited on
Create an array of type double that can hold up to 5 elements
Why do you create an array of int instead?

Shouldn't you reverse the array of the numbers ?
Google will tell you how to reverse an array.

Why do you calculate the total if it isn't required?

Also what value do you think n has on line 17 ?

I know, I was way off because I was using previous examples in class, and those were totaling.

But now, I think I got my code to work correctly as the instructions state, after doing a bit of research:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    const int SIZE = 5;
    int myArr[SIZE];

    cout << "Please enter 5 numbers: "<< endl;
    for(int i = 0; i < SIZE; i++)
    {
        cin >> myArr[i];
    }
    reverse(myArr, myArr+SIZE);
    cout << "Your 5 numbers are: ";
    for(int i = 0; i < SIZE; i++)
    {
        cout << myArr[i] << " ";
    }
    
   system("pause");
   return 0;
}


I am concerned with the part of the instructions that say "I should be able to change a single variable from 5 to 10 and have it work correctly." As this new code I constructed is functional, I want to be sure it's doing what he asks. Is it?
Last edited on
I am concerned with the part of the instructions that say "I should be able to change a single variable from 5 to 10 and have it work correctly." As this new code I constructed is functional, I want to be sure it's doing what he asks. Is it?

On inspection of the code, it can be seen that it will require to be changed in three different places. A clue: lines 10, 13 and 19.
Thank you! And it does! Problem solved!
Topic archived. No new replies allowed.