C++ Vector Resize: Counting Down to the Test

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Vector
Click For Summary
SUMMARY

The forum discussion focuses on resizing a C++ vector named countDown to contain integers from newSize down to 1. The sample program demonstrates initializing the vector and outputting its contents. The user encountered issues with the resize method, which is confirmed to function correctly, but they must also populate the vector with the appropriate values after resizing. The correct approach involves using a loop to fill the vector with integers in descending order.

PREREQUISITES
  • Understanding of C++ vector operations
  • Familiarity with C++ control structures (loops)
  • Basic knowledge of C++ input/output using iostream
  • Experience with C++ data types (specifically int)
NEXT STEPS
  • Learn how to populate C++ vectors using loops
  • Explore C++ vector methods such as resize and at
  • Study C++ error handling for vector operations
  • Investigate advanced C++ vector functionalities like iterators
USEFUL FOR

C++ students, software developers, and anyone preparing for programming tests that involve vector manipulation in C++.

ineedhelpnow
Messages
649
Reaction score
0
going through problems to study for a test

Resize vector countDown to have newSize elements. Populate the vector with integers newSize down to 1. Ex: If newSize = 3, then countDown = {3, 2, 1}, and the sample program outputs:

3 2 1 Go!
Sample program:

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

int main() {
   vector<int> countDown(0); 
   int newSize = 0;             
   int i = 0;                         

   newSize = 3;
   <STUDENT CODE>

   for (i = 0; i < newSize; ++i) {
      cout << countDown.at(i) << " ";
   }
   cout << "Go!" << endl;

   return 0;
}

i tried countDown.resize(newSize); but it didnt work.
 
Technology news on Phys.org
ineedhelpnow said:
i tried countDown.resize(newSize); but it didnt work.

Hi! ;)

That should work to resize the vector.
But I think you still have to populate the vector in order to get 3, 2, 1. (Thinking)
 

Similar threads

Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 13 ·
Replies
13
Views
2K