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!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

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