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 , which on a computer does not give exactly ; and it shows where it leads, in the following units and in the course Programming in C++ II.
Learning objectives
- Describe the sequence of contents of the unit and explain what each lesson contributes to the next.
- Recall what is needed from unit 1 (the cycle of compiling and running, formatted output, reading with
std::cinand reading compiler messages) and review it if necessary. - Explain, with an example, why a number in a program is not a number of mathematics, and anticipate why is not exactly on a computer.
- 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.
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.
Question 1From earlier unitsA procedure says: “1. takes the value 17. 2. takes the value . 3. takes the value .” What is the value of at the end?
It assumes that each step starts from the initial value; the second starts from the value the first one left.It reads “takes the value” as a mathematical equality; it is an order that replaces the value of .It confuses a variable with a constant: the value of a variable changes each time another value is assigned to it.Each step starts from the value that has at that moment, not from the initial one. Following the procedure by hand, with the value of each variable after each step, is the trace table of unit 1, and lesson 2.1 turns it into the instrument for reasoning about the state of a program.Question 2From earlier unitsWhat does the statement
std::println("{:.2f}", 2.0 / 3);write?It believes that the format specification truncates; it rounds to the nearest decimal.It ignores the format specification: that is the output without.2f.It believes that the division is integer division; with2.0, which has decimals, the division is real.The specification.2fwrites the number with two decimals, rounding: is written0.67. The stored value does not change; only the way it is written.Review: Input, output and errors
Question 3From earlier unitsThe number is to be written with two decimals, rounding. What is obtained?
It truncates instead of rounding: it discards the third digit without looking at it.It rounds to one decimal, not to two.It increases the second decimal digit by more than it should: lies between and , and rounding chooses one of those two.Rounding to two decimals means choosing the nearest number with two decimals: is closer to than to . Lesson 2.3 will show that the computer rounds in the same way, but in base 2, and that this is why almost no number with decimals is stored exactly.Review: Input, output and errors
Question 4From earlier unitsA program has
const int pages{12}on line 5 andstd::println("{}", pages);on line 6. The compiler writes6:5: error: expected ',' or ';' before 'std'. Where is the defect?Line 6 is correct; the compiler points to it because that is where it noticed the omission.stdis recognized; the message says that it expected a semicolon before it.The message sayserror, notwarning: there is no executable.The compiler discovers the omission when it finds something that cannot follow the declaration, at the beginning of line 6, and points to that place. If the line pointed to is fine, the defect is usually at the end of the previous one.Review: Anatomy of a program
Question 5A program declares
int a{2};andint b{7};, and then executesa = b;andb = 10;. What is the value ofaat the end?It believes that the assignment binds the two variables, as ifawere another name forb; it only copies the value.It readsa = bbackwards, as if it copiedaintob; the value goes from right to left.It adds the two values; assignment replaces, it does not accumulate.The assignmenta = bcopies intoathe value thatbhas at that moment; afterwards,aandbare independent cells, and changingbdoes not changea.Review: Variables and memory
Question 6How is the number 13 written in base 2?
It is an honest answer: lesson 2.2 teaches how to write a number in base 2 and how to read it, and the exit assessment will ask it again.It reads the remainders of the repeated divisions in the order in which they appear; they are read from the last to the first.It adds the weights wrongly: .In base 2 each position weighs twice as much as the one to its right, and the only digits are 0 and 1. Lesson 2.2 teaches it, because every integer in a program is stored this way.Question 7A 4-byte
intholds , its maximum value, and the program adds 1 to it. What can be stated?It ignores the limit of the representation: that number does not fit in a 4-byteint.That is how unsigned integers behave, whose arithmetic is modular; for signed integers, the standard does not guarantee it.The compiler does not know the values the program will have when it runs, unless they are constants; almost always it compiles without saying anything.An integer in a program has a fixed number of bits and, with them, a finite range. Leaving the range of a signed integer violates the precondition of addition; the course’s sanitizer stops the program, and without it the program may write anything. Lesson 2.2 studies it.Question 8Does have a finite decimal expansion?
is an approximation: , not 1.However many are written, a remainder is always left over; the expansion is periodic, not finite.It is a property of the number and of the base, not of the instrument; the calculator only shows an approximation.An irreducible fraction has a finite decimal expansion if and only if its denominator has no prime factors other than 2 and 5, those of base 10. In base 2 the condition is more demanding: the denominator must be a power of 2, and that is why has no finite binary expansion. Lesson 2.3 draws the consequences of this.Review: Floating-point numbers
Question 9What does
std::println("{}", 0.1 + 0.2);write?It assumes that the computer adds exact decimals; it adds binary approximations.It confuses the default output with the format specification{:.2f}, which rounds to two decimals.The sum of twodoublevalues is legitimate; what is surprising is its result, not the way it is written.Neither nor is stored exactly in binary, and the sum of their approximations is not the approximation of .std::printlnwrites the shortest decimal representation that identifies the stored value, and that representation reveals the difference. Lesson 2.3 explains digit by digit where it comes from.Review: Floating-point numbers
Question 10A program declares
const double ratio{7 / 2};and writesratio. What appears?It believes that the type of the variable decides how the division is done; the type of the operands decides it.It believes that integer division rounds; it discards the decimal part.The value 3 is constant and is represented exactly in adouble: there is no loss, and the braces allow it.The division7 / 2is between integers, and gives the integer quotient, 3; only afterwards is that 3 converted todouble. Which division is performed is decided within the operation, by the type of the operands, and not when the result is stored. Lesson 2.4 teaches where to place the conversion to obtain 3.5.
Loading the content…
Completed
Sign in to save your progress