Vectors-- reversing an array

Hello,
I am prompted to write a program that take an array of user inputted integers and then prints them in reverse order. I have written the code below thus far, which is a combination of two set of code I had written earlier that worked just, although I do understand their connection may be faulty. I am quite lost on how to move forward from this problem. The exact instructions are below:

"Write a program that prompts the user for 10 integers and stores them in a vector.

Print out these integers but in reverse order.

EX: Enter 10 integers: 1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 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
#include <iostream>
#include <vector>
using namespace std;

int main() {
	const int INTEGERS = 10;
	vector<int> userArray(INTEGERS);
	int i;

	cout << "Enter " << INTEGERS << " integers: ";
	for (i = 0; i < INTEGERS; ++i) {
		cin >> userArray[i];
		for (i = INTEGERS - 1; i >= 0; i--) {
			cout << userArray[i] << " ";
		}
		cout << endl;
	}


	//system("pause");
	return 0;
}
closed account (E0p9LyTq)
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
#include <iostream>
#include <vector>

int main()
{
   const int INTEGERS = 10;
   std::vector<int> userArray;
   userArray.reserve(INTEGERS);

   std::cout << "Enter " << INTEGERS << " integers:\n";

   for (int loop = 0; loop < INTEGERS; loop++)
   {
      std::cin >> userArray[loop];  // wrong, wrong, wrong, wrong, wrong, wrong!
      int temp;
      std::cin >> temp;
      userArray.push_back(temp);
   }
   std::cout << '\n';

   for (int loop = INTEGERS - 1; loop >= 0; loop--)
   {
      std::cout << userArray[loop] << " ";
   }
   std::cout << '\n';
}

My original entry loop code worked for VS2017, but it was wrong.
Last edited on
Thanks so much for your help! Unfortunately, I seem to be running into an issue I had with my previous code; debugging is aborted and the program forces a breakpoint at the end of line 14, with a pop up that says "Unhandled exception at 0x0F53CAB6 (ucrtbased.dll) in Lab19.exe: An invalid parameter was passed to a function that considers invalid parameters fatal." How do I remedy this?
The vector.reserve() only allocates memory, it doesn't actually create any elements. You would need to use push_back() to actually add the element to the vector. The other options are to create the elements when you declare the vector or resize() the vector to the proper size.

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
#include <iostream>
#include <vector>

int main()
{
   int num_elements = 10;  // This doesn't need to be a constant value.

   // Create a vector with num_elements elements.
   std::vector<int> userArray;(num_elements);

   std::cout << "Enter " << num_elements << " integers:\n";

    // Populate the vector.
   for (auto& itr : userArray)
   {
      std::cin >> itr;
   }
   std::cout << '\n';

   for (int loop = num_elements - 1; loop >= 0; loop--)
   {
      std::cout << userArray[loop] << " ";
   }
   std::cout << '\n';
}


Ended up with the same forced break point.. putzed around with the code a bit more and found a solution that worked! Thank you both so much for your input. The final code is as follows:

#include <iostream>
#include <vector>
using namespace std;

int main() {

const int INTEGERS = 10;
vector<int> userArray(INTEGERS);
int i;

cout << "Enter " << INTEGERS << " integers: ";

for (int i = 0; i < INTEGERS; i++) {
cin >> userArray[i];
}

for (int i = INTEGERS - 1; i >= 0; i--) {
cout << userArray[i] << " ";
}

//system("pause");
return 0;
}
Topic archived. No new replies allowed.