C/C++ Resize Vector and Populate with Integers for C++ CountDown Problem

  • Thread starter Thread starter osu3124
  • Start date Start date
  • Tags Tags
    C++ Vector
Click For Summary
The discussion centers around resizing a vector in C++ to contain a countdown of integers from a specified size down to one. The initial code snippet successfully resizes the vector but fails to populate it with the desired countdown values. A suggested solution involves using a for-loop to iterate through the vector, assigning values from `newSize` down to 1 by calculating `newSize - i` for each index. The user successfully implements this solution, resolving their issue. The final output correctly displays the countdown followed by "Go!" as intended.
osu3124
Messages
3
Reaction score
0
Problem Statement:
Resize vector countDown to have newSize elements. Populate the vector with integers {newSize, newSize - 1, ..., 1}. Ex: If newSize = 3, then countDown = {3, 2, 1}, and the sample program outputs:
3 2 1 Go!

Code I have so far, it works but I don't understand how to populate the vectors with integers counting down.

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

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

   newSize = 3;
   
   countDown.resize(newSize);

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

   return 0;
}
 
Technology news on Phys.org
osu3124 said:
Problem Statement:
Resize vector countDown to have newSize elements. Populate the vector with integers {newSize, newSize - 1, ..., 1}. Ex: If newSize = 3, then countDown = {3, 2, 1}, and the sample program outputs:
3 2 1 Go!

Code I have so far, it works but I don't understand how to populate the vectors with integers counting down.

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

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

   newSize = 3;
   
   countDown.resize(newSize);

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

   return 0;
}

Hi osu3124! Welcome to MHB! ;)

How about creating a for-loop to populate the vector?
Say, iterate [m]i[/m] from [m]0[/m] to [m]newSize[/m], and initialize the corresponding elements with [m]newSize - i[/m]?
 
I like Serena said:
Hi osu3124! Welcome to MHB! ;)

How about creating a for-loop to populate the vector?
Say, iterate [m]i[/m] from [m]0[/m] to [m]newSize[/m], and initialize the corresponding elements with [m]newSize - i[/m]?

I got it working. Thanks so much!
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

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