Week 1

Escape sequences

Escape sequence Output
\' ' (single quotation mark)
\" " (double quotation mark)
\\ \ (backslash)
\n new line
\t tab

Exercise 1.1

Write a program that produces the output

"Hello"
'world'
Solution

#include <iostream>

using namespace std;

int main() {
    cout << "\"Hello\"\n\'world\'";
    return 0;
}

Alternative solution

#include <iostream>

using namespace std;

int main() {
    cout << "\"Hello\"" << endl << "\'world\'";
    return 0;
}

Variables

Exercise 1.2

Given the following program, fill in the blank with code that swaps the values of x and y (multiple statements may be required). Your code must work for all possible values of x and y.

#include <iostream>

using namespace std;

int main() {
    int x = 10;
    int y = 20;



    return 0;
}
Solution

We can introduce an intermediate variable z to store the old value of x as follows.

    int z = x;
    x = y;
    y = z;

Types

Type Specifier Examples
Boolean bool true, false
Character char 'a', '2', '!'
Integer int 2, 0, -4
Double-precision floating-point number (real number) double 2.0, -1.25, 3.1e2, -1.2e-1

Numerical errors

Integer overflow may lead to unexpected results. For example, on many computers,

int i = 2147483647;
cout << i + 1;

produces the output -2147483648 since the integers ‘wrap around’ from the largest integer to the smallest integer.

Floating-point numbers may also overflow. For example,

double d = 1e308;
cout << 2 * d;

produces the output inf (representing $\infty$) since the largest double is approximately $1.8 \times 10^{308}$. Underflow is also possible with floating-point numbers:

double d = 1e-323;
cout << d / 4;

produces the output 0 since the smallest positive double is approximately $4.9 \times 10^{-324}$. In addition, floating-point numbers are susceptible to rounding errors. The statements

double d = 0.1 + 0.1 + 0.1 - 0.3;
cout << d;

give 5.55112e-17 since $0.1$ and $0.3$ cannot be exactly represented by a double. The size of the relative error in a floating-point arithmetic operation could be up to $\sim 10^{-16}$.

Operators

Operation Operator
Addition +
Subtraction -
Multiplication *
Division /
The type of the result of an arithmetic operation will be the same as the types of the operands. In particular, if both operands are integers, the result will remain an integer (and be truncated if necessary). However, if either operand is a double, the other operand will be ‘promoted’ to a double, so the result will be a double.

Exercise 1.3

What is the output of the following statements?

int x_int = 2;
int y_int = 3;
double x_double = 2.0;
double y_double = 3.0;

cout << x_int / y_int << '\n'; 
cout << x_double / y_int << '\n'; 
cout << x_int / y_double << '\n'; 
cout << x_double / y_double << '\n'; 
Solution

0
0.666667
0.666667
0.666667

Operation Operator Usage Effect
Addition assignment += x += y x = x + y
Subtraction assignment -= x -= y x = x - y
Multiplication assignment *= x *= y x = x * y
Division assignment /= x /= y x = x / y
(Pre-)increment ++ ++x x = x + 1
(Pre-)decrement -- --x x = x - 1