First, If you could re-post your code, so that it is formatted as code, that will help others to read it more easily. To do so, you can type 3 back tics on the line above your code, and on the line below your code like so:
Ok. The function needs to return a vector, so to define your function, the first thing you have to do is declare the data type that it will return which in this case is a vector containing integers. Like so:
std::vector<int> first_three_multiples(int num) {
//function code here
}
The body of your current function wonât accomplish the task. The pre-written code provided will test your function by passing in the value: 8. You wonât need to assign 8 to a new variable, you just use the num parameter included in your function declaration. What you need to do is multiply num by 1 then 2 then 3 to get the first 3 multiples: 8, 16, 24. Those values should be stored in a vector of type int. Then return the vector. To declare an empty vector of type int you use the following syntax: std::vector<int> myVector;
You can then add values to the vector with the .push_back() method or you can assign the values when you declare the vector like so: std::vector<int> myVector = {value1, value2, value3}; Give it a try. Good luck!
i get this , what i dont understand where go : std::vector myVector;
#include <iostream>
#include <vector>
// Define first_three_multiples() here:
std::vector<int> first_three_multiples(int num) {
//std::vector<int> multiples{num, num * 2, num * 3};
// este es el vector declarado
int vector [] ={num, num * 2, num * 3};
return vector;
}
int main() {
for (int element : first_three_multiples(8)) {
std::cout << element << "\n";
}
}
int main() {
for (int element : first_three_multiples(8)) {
std::cout << element << "\n";
}
Could you explain me how the âfor loopâ (a small part of the question) works? (As far as I remember this is not taught till this part of the course.)
for loops in general in all programming languages are most often used to iterate over an enumerable data structure like an array or in the case of this lesson a vector. The syntax used in this lesson is a little more advanced than usual. In the example, int element is a variable that will contain the values returned by the range on the right side of the :. The range in this case is a function called first_three_multiples. The function returns a vector, and the for loop assigns each element or value included in the vector to the variable named element one at a time. It takes the first value from the vector, performs whatever actions are inside the for loopâs code block, then repeats those actions for the second value, third value, etc. until it reaches the end of the range. In this example there are only 3 values in the vector, and the only action inside the code block is to print the value to the console using std::cout. Hopefully this makes sense. Hereâs a link regarding the use of the : in the loop if youâd like to investigate further: https://www.cprogramming.com/c++11/c++11-ranged-for-loop.html
They definitely go through for loops early on in the lessons but I to was confused by the single â:â which they do not cover. So I appreciate this thread.