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

  • C/C++
  • Thread starter osu3124
  • Start date
  • Tags
    C++ Vector
In summary, the problem was to resize a vector and populate it with integers counting down. The solution was to use a for-loop to initialize the vector elements with the appropriate values.
  • #1
osu3124
3
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
  • #2
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]?
 
  • #3
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!
 

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

1. What is a C++ vector?

A C++ vector is a container that can hold a dynamic array of elements. It is part of the standard template library (STL) in C++ and provides functionality for dynamic memory allocation, resizing, and accessing elements.

2. How do I declare a vector in C++?

To declare a vector in C++, use the syntax vector name; where type is the type of elements you want to store and name is the name of your vector. For example, vector numbers; would declare a vector of integers named "numbers".

3. How do I add elements to a vector in C++?

To add elements to a vector, you can use the push_back() function. This function takes in the element you want to add as an argument and adds it to the end of the vector. For example, numbers.push_back(5); would add the integer 5 to the end of the "numbers" vector.

4. How do I access elements in a vector in C++?

You can access elements in a vector using the at() function or the square bracket notation. For example, numbers.at(0) would return the first element in the "numbers" vector, and numbers[2] would return the third element.

5. How do I resize a vector in C++?

To resize a vector in C++, you can use the resize() function. This function takes in the new size you want for the vector as an argument. If the new size is larger than the current size, the vector will be extended with default-initialized elements. If the new size is smaller, elements at the end of the vector will be removed. For example, numbers.resize(10); would resize the "numbers" vector to have a size of 10 and fill any new elements with default values.

Similar threads

Replies
1
Views
4K
Replies
22
Views
2K
Replies
5
Views
2K
Replies
10
Views
2K
Replies
23
Views
2K
Replies
1
Views
1K
Replies
1
Views
1K
Back
Top