Prerequisites
Before going over the practice problems, please review these sections
* Note that 2. Program structure/Functions now has a tl;dr section that summarizes the content
Group Problems
Problem 1: Weekly Rainfall Analyzer
- Write a program that has a vector that stores the rainfall (in millimeters) for each day of a week and another vector for the names of the days.
- Write a function that finds all days when the rainfall is higher than the week’s average rainfall. Return a vector of the names of those days.
Objectives:
-
You have rainfall measurements for each day of the week in a vector.
-
Your goal is to figure out which days had rainfall higher than the average of the whole week.
-
For those special days, report the name of each day.
Problem 2: Alarm Frequency Finder
Suppose you want a vector to represent hourly temperature readings for a 24-hour period. The building is equipped with an alarm that sounds whenever the temperature drops at least 10 degrees compared to the previous hour.
- Write a function to return how many alarms would sound in one 24-hour dataset.
Objectives:
-
You have a vector of temperature readings, one for each hour over 24 hours.
-
The building’s alarm sounds if the temperature drops by 10 degrees or more between any two consecutive hours.
-
Your job is to count how many alarms would go off during the day.
General Problems
- Identify the logical error and explain the mistake.
vector<int> vals = {1, 2, 3, 4, 5, 6..};
for (size_t i = 0; i > vals.size(); i++) {
cout << vals[i] << ", ";
}- Identify the error and explain the mistake. If there are multiple, repeat the process.
The program is expected to print out 14;
vector<int> v(10);
for (size_t i = 0; i < 10; i++) {
v[i] = i * 2;
}
cout << v[6] << endl;- Modify the line to fix the error
The program is expected to find the value
19and print outFound!Challenge: Create a line of code where it breaks after finding the value;
vector<int> numbers = {1, 5, 6, 12, 16, 21, 26, 23};
for (size_t i = 0; i < numbers.size(); i++) {
if (numbers[i] == 19) {
cout << "Found!";
}
}- Identify the error and fix the issue.
The program doesn’t run smoothly because of this error.
int printVector(vector<int> y) {
cout << "[";
for (size_t i = 0; i < y.size(); i++) {
if (i < y.size()-1)
cout << ", ";
cout << y[i];
};
cout << "]";
}- Identify the error and fix the issue.
#include <cmath>
...
bool isPrime(int num) {
if (num < 0) return isPrime(abs(num));
if (num == 1) return false;
for (int i = 2; i <= sqrt(floor(num)); i++) {
if (num % i == 0)
return false;
}
}
void printPrimeNumbers(vector<int> nums) {
for (size_t i = 0; i < nums.size(); i++) {
if isPrime(nums[i]) {
cout << nums[i];
if (i != nums.size() - 1)
cout << ", ";
}
}
}
int main() {
vector<int> vec;
printPrimeNumbers(vec);
return 0;
}- Making the function modify the variables
void idkWhatThisIs(int a, int b, int c) {
c = a + b;
a = 6;
b = 9;
}
int main() {
int a = 5, b = 4;
int sum;
idkWhatThisIs(a, b, sum);
cout << sum;
return 0;
}- Explain what this code does.
void doesWhatever(vector<string>& words) {
if (!words.empty()) {
string temp = words[0];
words[0] = words[words.size()-1];
words[words.size()-1] = temp;
}
}
vector<string> list = {"apples", "bananas", "pears", "watermelons"};
doesWhatever(list);- Explain what this code does.
int countMultiples(const vector<int>& arr, int n) {
int count = 0;
for (size_t i = 0; i < arr.size(); i++) {
if (arr[i] % n == 0) count++;
}
return count;
}
cout << countMultiples({1, 2, 3, 4, 5}, 1); " multiples";- Make the function return a certain value
15without changing the arguments of the calling function
#include <iostream>
using namespace std;
int multiply (int a, int b) {
return a * b;
}
cout << multiply(5);