 Need help with First Three Multiples

Hi guys, I am currently on C++ module First Three Multiples Tut.

I have looked at ranged based for loops a bit to understand the for loop at the bottom, but I am stuck on why my code outputs so many 0’s. Could anyone explain to me why, and what I should do instead?

My aim for this tut is to print out 3 values of multiples of 8. 8 (81). 16 (82). 24 (8*3).

#include <iostream>
#include <vector>

// Define first_three_multiples() here:

std::vector <int> first_three_multiples(int num)
{
std::vector<int> ftm(num);
for (int i = 1; i <= 3; i++)
{
ftm.push_back(num * i);
}
return ftm;
}

int main() {

for (int element : first_three_multiples(8)) {
std::cout << element << "\n";
}
return 0;
}

Cheers.

Han

Hello, welcome to the forum This is the crucial line:

std::vector<int> ftm(num);

take a look at the reference -> http://www.cplusplus.com/reference/vector/vector/vector/, this is the part that relates to this situation:

explicit vector (size_type n, const allocator_type& alloc = allocator_type());
vector (size_type n, const value_type& val, const allocator_type& alloc = allocator_type());
Constructs a container with n elements. Each element is a copy of val (if provided).

This means that in this line of code you have created a vector of type int with num (in case of this snippet num = 8) elements. Vector initialises all the elements and the default value for int is 0. That is why in the output you get eight zeros To fix this problem you can simply omit the size_type parameter:

std::vector<int> ftm;

Small digression that can be ignored.

But this should lead us to a question - why there is a way to specify the number of elements at the creation time of the vector - its length is extendable, right? Right. But when you initialise vector without specifying the size then with every push_back there might be a need to allocate memory (and this takes time). And sometimes this is what we want - sometimes we simply do not know how many elements will be in the vector.

But in case of this function, we are sure that we will push_back only 3 values. But we do not want to start with three zeros, we don’t know the values, we have to calculate them. In this scenario we can use reserve method to allocate memory for three int values:

std::vector<int> ftm;
ftm.reserve(3);
1 Like

Are there any advantages to reserving the capacity verses to initializing the vector with int 0 as elements by default?

You’re very welcome If there is a high probability that many of the values will remain zeros then it might be better to initialize with zeros (or with any other value passed as a second parameter).

But when you don’t have any prior knowledge of what kind of values you will place inside the vector then it’s better to just reserve space for them.

It is pretty intuitive - the fewer operations a computer needs to execute, the faster the code will be.

My code generates the requested output but I get a message from Codecademy “Is first_three_multiples() defined?” I get the feeling I’m missing something. What is it and why does it matter?

Code below

#include <iostream>
#include <vector>

// Define first_three_multiples() here:
// Define first_three_multiples() here:
//don't forget the vector is standard
// in opening line ftm(must include type of variable). not necessary in body.
std::vector <int> first_three_multiples (int num) {

// std::vector <int> first_three_multiples (num); --(num) is for the number of elements.  this line of code will send out 8 0s (because 0 is the default.)
std::vector <int> first_three_multiples;
// use semicolors to separate information inside loop functions.
for (int i=1; i <= 3; i++){
//add on to the end of the list using push_back
first_three_multiples.push_back(num*i);
}
return first_three_multiples;
}

int main() {

for (int element : first_three_multiples(8)) {
std::cout << element << "\n";
}

}