DM560, Introduction to Programming in C++

Sheet 2

Main concepts from the lectures in weeks 37 amd 38:

Types and operations:

  • built-in
  • user defined
  • standard library

Type safety:

  • Static type check
  • Dynamic type check

Type conversions

  • safe vs unsafe
  • implicit vs explicit

Terminology:

  • type defines a set of possible value
  • object is some memory that hold
  • variable is a named object
  • value is a set of bits in memory interpreted according to a type
  • literal is a value conforming a type
  • declaration, definition, assignment, initialization

Computations

Statements: Declaration or Expression or Control structure

Expressions and operator precedences

Control structures

  • Selection:
    if (condition) {} else if (condition) {} else {}
    switch (variable) { case value1: {break;} case vale 2: {break;} default: {}}
    
  • Iteration
    while (condition) {}
    for ( initialize ; condition ; increment ) {}
    

Functions

  Return_type function_name ( Parameter list ) 
  {
    return some_value;  // of Return_type
  }

vector, range-for-loops, algorithm

Task 1 (in class)

Check that for any two given numbers $a$ and $b$ (integer) it is $a=q\cdot b + r$ where $q$ is the quotient, that is, the result of the integer division $a/b$ and $r$ is the remainder of the integer division. (“Try this” on page 68.)

Which operation is performed by the C++ operand / between integer numbers and which one between real numbers?

Task 2 (in class)

Write a small program that takes as input a number, and translates it in character. (“Try this” on page 81.)

Write in this way the whole ASCII code table.

Task 3

Try the Uninitialized variables example from the slides.

Task 4

Write a small program that outputs a personalized letter on the basis of the information you provide from the prompt. The letter must look as follows:

“Dear [first-name],

How are you? I am fine.

Have you seen [name-of-a-friend]? How is [she/he]?

I heard that you just got your [age] birthday. Congratulations!

Yours sincerely, [your full name]”

Task 5 (in class)

Determine which parentheses can be removed from this expression:

(3>=(j-3))==((323-(j*-7))!=(43))

Task 6 (in class)

What is type of the result of this expression:

int a=3;
double d=2.19;
auto f = 4-d+a/2

Task 7 (in class)

What is the outcome of the following expression:

int a=0;
int b=2;
int c=3;
a<b<c;

Task 8

Explain the effect of the following lines of code:

int i;
double d;
int i3 {d/i};
int i2=d/i;

Task 9

Which is the out put of:

cout << 'a'+1;
cout << (int{'a'}+1);

Task 10 (in class)

[use of vector]

Write a program that gets a sequence of integer numbers from the standard input stopping the sequence when a character, rather than a number, is encountered.

Output the sequence of numbers in sorted order and indicate the number of elements in the sequence and the largest number.

Output the sequence removing repeated entries.

Modify the program to achieve the same or similar result with words rather than with numbers.

Task 11

[use of switch structure]

Write a program that reads from standard input a number and a currency among yen, dollars and puonds and converts it to Danish krone. For Example, 1 $ should give 6,50 DKK. Use the switch structure to decide from which currency to translate.

Task 12

[use of functions]

Modify the program from task 10 with integers such that it always outputs the median of the sequence. See Wikipedia for a definition of median. Implement the calculation of the median in a function that takes as input the vector and outputs the median.

Task 13

This is exercise 5 from Chapter 4 of [BS].

Write a program to play a numbers guessing game. The user thinks of a number between 1 and 100 and your program asks questions to figure out what the number is (e.g., “Is the number you are thinking of less than 50?”). Your program should be able to identify the number after asking no more than seven questions. Hint: Use the < and <= operators and the if-else construct.

Task 14

This is exercise 8 from Chapter 4 of [BS].

There is an old story that the emperor wanted to thank the inventor of the game of chess and asked the inventor to name his reward. The inven- tor asked for one grain of rice for the first square, 2 for the second, 4 for the third, and so on, doubling for each of the 64 squares. That may sound modest, but there wasn’t that much rice in the empire! Write a program to calculate how many squares are required to give the inventor at least 1000 grains of rice, at least 1,000,000 grains, and at least 1,000,000,000 grains. You’ll need a loop, of course, and probably an int to keep track of which square you are at, an int to keep the number of grains on the current square, and an int to keep track of the grains on all previous squares. We suggest that you write out the value of all your variables for each iteration of the loop so that you can see what’s going on.

Task 15

This is exercise 9 from Chapter 4 of [BS].

Try to calculate the number of rice grains that the inventor asked for in exercise 8 above. You’ll find that the number is so large that it won’t fit in an int or a double. Observe what happens when the number gets too large to represent exactly as an int and as a double. What is the largest number of squares for which you can calculate the exact number of grains (using an int)? What is the largest number of squares for which you can calculate the approximate number of grains (using a double)?

Task 16

This is exercise 13 from Chapter 4 of [BS].

Create a program to find all the prime numbers between 1 and 100. There is a classic method for doing this, called the “Sieve of Eratosthenes.” If you don’t know that method, get on the web and look it up. Write your program using this method.