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

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

The discussion focuses on resizing a vector in C++ to contain integers counting down from a specified size. The user initially struggles with populating the vector after resizing it using the vector::resize method. A solution is provided, which involves using a for-loop to assign values to the vector elements by iterating from 0 to newSize and setting each element to newSize - i. The final output correctly displays the countdown followed by "Go!".

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with the std::vector class
  • Knowledge of for-loop constructs in C++
  • Basic input/output operations using iostream
NEXT STEPS
  • Learn how to implement dynamic memory management in C++
  • Explore advanced features of std::vector such as iterators
  • Study the differences between std::vector and other STL containers like std::list
  • Investigate error handling techniques in C++ for vector operations
USEFUL FOR

C++ developers, programming students, and anyone looking to enhance their skills in vector manipulation and dynamic array handling in C++.

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!
 

Similar threads

Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
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