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
AI Thread 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!
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

Similar threads

Replies
1
Views
4K
Replies
22
Views
3K
Replies
5
Views
3K
Replies
23
Views
3K
Replies
1
Views
1K
Replies
1
Views
1K
Back
Top