Test Results
Welcome to the last lesson of the C++ course! In it, you'll put together everything you've learned and build one useful program step by step: the results of a test, which a maths teacher prints after marking it. When you finish, your program will print this:
Max is not in the class.
TEST RESULTS
Adam: 45 points, grade A
Emma: 41 points, grade B
Finn: 50 points, grade A
Jack: 17 points, grade F
Lucy: 38 points, grade B
Average score: 38.2
Best: Finn (50 points)
Failed: 1The program prints each student's score and grade, works out the average, finds the best student, gives everyone bonus points and fixes a mistake in one student's score. You'll use variables, conditions, vectors, loops, functions, references and strings: everything from the course. Each exercise continues where the previous one ended (the editor starts with its finished code) and adds one piece.
The data. The results are at the start of main(), in two vectors. The vector names holds the students' names, and the vector scores holds how many points each of them got (50 at most). Elements with the same index belong together: names[0] is "Adam", and scores[0] is his score, 42. Index 1 belongs to Emma with 38 points, and so on. In a real school, the data would come from the school's online system. The programs here get their data from variables.
The plan. The program is split into functions, each with one job. So far, there's one: printResults(), which prints the results for the vectors it gets as arguments. For now, it prints only the heading. It's above main(), because a function must come earlier in the program than the place where you call it. That's the usual shape of a program: the functions at the top, and main(), which uses them, at the bottom.
Both parameters are const references. Thanks to the &, the function works with the vectors from main() directly, and nothing is copied. Thanks to const, it only reads them and can't change them. That's all printing needs. The parameters have the same names as the vectors in main(): they don't have to, but it's easier to follow.
You need the elements with the same index from both vectors. So go through them with an index loop, not with for (int score : scores), which gives you only the values of one vector. A reminder (inside main()):
std::vector<std::string> subjects = {"Maths", "Physics", "History"};
std::vector<int> lessons = {4, 2, 2};
for (int i = 0; i < subjects.size(); i++) {
std::cout << subjects[i] << ": " << lessons[i] << " lessons a week" << std::endl;
}Maths: 4 lessons a week
Physics: 2 lessons a week
History: 2 lessons a weekThe condition is i < subjects.size(), not <=. The last index is size() - 1, so with <=, the loop would also reach for an element that isn't in the vector.
Instructions
In printResults(), under the line that prints the heading, write a for loop with a variable i that starts at 0 and runs while i is less than names.size(). In the loop, print the name of each student (names[i]) on a line of its own.
Complete the line in the loop so that it also shows the score after the name: after the name, a colon and a space, then scores[i], a space and the word points. The program prints:
TEST RESULTS
Adam: 42 points
Emma: 38 points
Finn: 48 points
Jack: 14 points
Lucy: 27 pointsStart programming for free
1/7