Reversing integers in an array?

Hi, i need help reversing an integers while using an array.the code is already complete i just need to reverse the output. Please help me thank you. i have some ideas that i'm working on right now, but comment anyway, thank you so much!!

this is what i want:

input is:
1
2
3
4
5,
out put is:
5
4
3
2
1

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>
#include <string>
using namespace std;

int main()
{
    string integers[20];
    int nums;

    cout << "How many numbers?" << endl;
    cin >> nums;

    for (int counter = 0; counter < nums; counter++)
    {
        cout << "Enter number " << counter << endl;
        cin >> integers[counter];
    }

    cout << "You entered:" << endl;

    for (int x = 0; x < 20; x++)
    {
        cout << integers[x] << endl;
    }


    return 0;
}
Last edited on
Decrement instead of increment.
tried it but all i get is a run time error and if i put a ">" instead of a "<", i get nothing. or no output.
maybe i need to add a function?
try doing this for your print for loop do

for(int x = counter - 1; counter >= 0; x--){

cout << intergers[x] << endl;

}
Last edited on
it didnt work dude, on which for loop did you want me to try it on, i tried it on the bottom one
nvm, but yeah it did not work
Last edited on
the code now gives me an error: name lookup of counter changed for ISO for scoping [-fpermissive]
my class just did this but im too tired to think right now so heres my code for reversing arrays

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;

// Character Declarations
	char Y;		// to rerun the program
// Constaints
	const int Capacity = 50;
// Function Declarations
	void fillArray(string[], double[], int&);
	void printArray(string[], double[], int);

int main()
{
	// Output Identification
	system("CLS");
	cout << "In-class #11 by Christopher Adique -\n "
		 << "Reverse Rocket Order\n\n";

	double newMass[Capacity];	// array for mass
	string newName[Capacity];	// array for names
	int numOfElements = 0;		// number of elements

	do{
		fillArray(newName,newMass,numOfElements);
		printArray(newName, newMass, numOfElements);

		cout << "\n\nWould you like to run the program again (Y=Yes) => ";
		cin >> Y;
	}
	while (Y == 'Y' || Y == 'y');

	cout << "\n\nEnd Program.\n";


	return 0;
}


void fillArray(string newName[], double newMass[], int& numOfElements){

	int i = 0;			// inital value for i
	double mass;		// input mass
	std::string name;	// input name
	std::string junk;	// clear out name

	while ((name != "END") && (i < Capacity)){

		cout << "\nEnter a rocket name (END to end list): ";
		std::getline (std::cin,name);
		newName[i] = name;

		if (name != "END"){
		cout << "\nWhat is the mass of " << name << ": ";
		cin >> mass;
		newMass[i] = mass;
		std::getline (std::cin,junk);
		}

		numOfElements++;
		i++;
	}
}

void printArray(string newName[], double newMass[], int numOfElements){

	cout << "\nThe rockets entered in reverse order are: \n";
	for (int i = (numOfElements - 2); i >= 0; i--) {
		cout << left << setw(25) <<  newName[i] << setw(10) << right << newMass[i] << endl;
		}
}
nvm i got it already, thank you, instead of the counter that you said to put in the bit of code you showed me, i had to put the "nums" and vualla!! HAHA thank you so much though!
Topic archived. No new replies allowed.