Functions and Vectors
An argument can be any value, a vector too. The parameter then has a vector type, such as std::vector<int> numbers. A function that gets a vector can go through it with a loop and return a result. This function counts how many numbers in the vector are greater than a given limit:
#include <iostream>
#include <vector>
int countAbove(std::vector<int> numbers, int limit) {
int count = 0;
for (int number : numbers) {
if (number > limit) {
count++;
}
}
return count;
}
int main() {
std::vector<int> temperatures = {18, 25, 31, 22, 28};
std::vector<int> points = {5, 10, 15};
std::cout << countAbove(temperatures, 24) << std::endl;
std::cout << countAbove(points, 7) << std::endl;
return 0;
}This program prints:
3
2The parameter numbers holds the vector that the call passes: first temperatures, then points. You know counting from the lesson on loops, but now it's inside a function, so you can do it for any vector with a single call. And because count is a local variable, every call starts again from 0.
Notice where the return is: after the } that ends the loop, when all the numbers have been checked. A common mistake is to put it one line higher, inside the loop:
for (int number : numbers) {
if (number > limit) {
count++;
}
return count;
}Now the return is in the body of the loop. It ends the whole function, and with it the loop, on the very first pass. The function checks only the first number, 18, and returns 0 for temperatures.
A function can return a vector too. The return type is then, for example, std::vector<int>. Create an empty vector before the loop, add elements to it with push_back() in the loop, and return it after the loop.
Watch out for one thing: the parameter gets a copy of the vector, just like with numbers in the previous exercise. When the function changes the vector in its parameter, it changes only its copy, and the vector in main stays the same:
#include <iostream>
#include <vector>
void addBonus(std::vector<int> points) {
points.push_back(10);
std::cout << "In the function: " << points.size() << " elements" << std::endl;
}
int main() {
std::vector<int> points = {5, 8, 7};
addBonus(points);
std::cout << "In main: " << points.size() << " elements" << std::endl;
return 0;
}This program prints:
In the function: 4 elements
In main: 3 elementsThe function added an element to its copy, not to the vector points in main. So when you want a function to change a vector, for now, return the vector from the function and store it in main. Besides, copying a big vector takes a while. In the next lesson, References and Pointers, you will learn how to pass a variable from main to a function directly, without copying, so that the function can change it.
Instructions
The program doesn't compile, because the function average doesn't exist yet. Write it in place of the comment: it has a parameter numbers of type std::vector<int> and returns the average of the numbers in the vector as a double, that is, their sum divided by how many there are. Watch out for integer division: keep the sum in a variable of type double. The program then prints the average temperatures of two weeks, 20.8 and 12.75.
Below the function average, write a function warmDays with two parameters: temperatures of type std::vector<int> and limit of type int. It returns a new vector of the temperatures that are higher than limit. At the end of main, before return 0;, store warmDays(week1, 20) in a variable warm, and with a loop, print each of its temperatures on its own line. The program prints 21, 24 and 22.
Start programming for free
9/10