Skip to content
Topos Uranos

Summary

This lesson opens the sixth unit of the course, devoted to compound data. It explains what the unit is for: to teach how to represent collections of data (sequences of fixed and of variable size, tables and records) and how to process them with the basic algorithms, written by hand and taken from the library, estimating how much they cost; it presents its six lessons and the thread that joins them; it recalls what the unit takes for granted, above all loops with an invariant and passing by const reference; it formulates the problem the unit solves, that of a program that must store data whose quantity it does not know and that, written with separate variables, silently ignores the ones left over; it anticipates cost as the second measure of an algorithm, alongside its correctness; and it shows where it leads, in the following units and in the course Programming in C++ II.

Learning objectives

  1. Describe the sequence of contents of the unit and explain what each lesson contributes to the next.
  2. Recall loops, their invariants and parameter passing (by value, by reference and by const reference), and review them if needed.
  3. Anticipate, with an example, the cost of an algorithm as a second measure of its quality, alongside its correctness.
  4. Recognize what one is expected to be able to do at the end of the unit, and distinguish it from what will be learned in the following units.

Entry assessment

Before working through the unit, it is worth measuring the starting point. This assessment is a self-assessment: it is not graded, and its only purpose is to guide your study. The first four questions check what the unit takes for granted: the invariant of the loop that looks for a maximum, the passing mode of a large parameter, the number of iterations of two nested loops and unsigned arithmetic. The next six touch, in the order of the lessons, on the central ideas of the unit: the valid indices of a sequence, the creation of a vector, the number of times a size can be divided in half, searching in sorted data, the double index of a table and the danger of storing the data of a single entity in separate collections. No one is obliged to get them right yet, and in some of them “I don’t know yet” is an honest answer: each answer, right or not, explains the point and names the lesson that deals with it. The same skills are measured again at the end, in the synthesis lesson.

Entry assessment

Before starting the unit, measure your starting point. It is a short self-assessment: it is not graded and its result is stored only in your browser.

  1. Question 1From earlier unitsThe fragment int largest{0}; int t{}; while (std::cin >> t) { if (t > largest) { largest = t; } } must leave in largest the highest of the temperatures read. With what input does it fail?

  2. Question 2From earlier unitsA function computes how many vowels a text of a million characters has, stored in a std::string, and does not modify it. How should its parameter be declared?

  3. Question 3From earlier unitsHow many times is ++count executed in for (int i{0}; i < 6; ++i) { for (int j{0}; j < i; ++j) { ++count; } }?

  4. Question 4From earlier unitsWhat does std::println("{}", 0u - 1); write on a system with a 32-bit unsigned int?

  5. Question 5In a sequence of nn elements numbered as in C++, which indices ii designate an element?

  6. Question 6What does v contain after std::vector<int> v(3, 7);?

  7. Question 7How many iterations does for (int m{1024}; m > 1; m /= 2) { ... } make?

  8. Question 8A phone book has a million names in alphabetical order. To find one, it is opened in the middle, the half where the name cannot be is discarded, and the process is repeated with the other half. At most, how many times must it be opened?

  9. Question 9With std::vector<std::vector<int>> t{{1, 2, 3}, {4, 5, 6}};, what are the values of t[1][0] and t.size()?

  10. Question 10A program stores the names of a group in one vector and their grades in another, so that the grade grades[i] is that of names[i]. If it sorts only the vector of grades, what happens?