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:
#include <iostream>
usingnamespace std;
int main()
{
constint 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?
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
usingnamespace std;
int main()
{
constint 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?
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.