Skip to content
Topos Uranos

Summary

This lesson opens the second unit of the course, devoted to data and their types. It explains what the unit is for: to teach that every piece of data in a program lives in memory with a type that fixes which values it can take, how much space it occupies and which operations it admits, and that those limits have observable consequences; it presents its four lessons and the thread that joins them, from the variable as a cell with a name and a value to the conversions between types and the constants; it recalls what the unit takes for granted from the first, namely writing, compiling and running a program that reads and writes; it formulates the problem the unit solves, that of a program that looks correct but whose calculations do not agree with those of mathematics, beginning with the sum 0.1+0.20.1 + 0.2, which on a computer does not give exactly 0.30.3; 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 what is needed from unit 1 (the cycle of compiling and running, formatted output, reading with std::cin and reading compiler messages) and review it if necessary.
  3. Explain, with an example, why a number in a program is not a number of mathematics, and anticipate why 0.1+0.20.1 + 0.2 is not exactly 0.30.3 on a computer.
  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 from the first: following a procedure step by step, writing a number with two decimals, rounding, and reading an error message. The next six touch, in the order of the lessons, on the central ideas of the unit. No one is obliged to get them right yet, and in one 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 unitsA procedure says: “1. rr takes the value 17. 2. rr takes the value r−5r - 5. 3. rr takes the value r−5r - 5.” What is the value of rr at the end?

  2. Question 2From earlier unitsWhat does the statement std::println("{:.2f}", 2.0 / 3); write?

  3. Question 3From earlier unitsThe number 12.34612.346 is to be written with two decimals, rounding. What is obtained?

  4. Question 4From earlier unitsA program has const int pages{12} on line 5 and std::println("{}", pages); on line 6. The compiler writes 6:5: error: expected ',' or ';' before 'std'. Where is the defect?

  5. Question 5A program declares int a{2}; and int b{7};, and then executes a = b; and b = 10;. What is the value of a at the end?

  6. Question 6How is the number 13 written in base 2?

  7. Question 7A 4-byte int holds 2 147 483 6472\,147\,483\,647, its maximum value, and the program adds 1 to it. What can be stated?

  8. Question 8Does 1/31/3 have a finite decimal expansion?

  9. Question 9What does std::println("{}", 0.1 + 0.2); write?

  10. Question 10A program declares const double ratio{7 / 2}; and writes ratio. What appears?