computer science week 2 · self-assessment •gibt euch rückmeldung über euren fortschritt. •er...

Post on 15-Jan-2020

2 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Computer ScienceWeek 2Moritz Schneider

Moritz Schneider 24. September 2019 1

Outline:

1. Self-Assessment I

2. Integer division and operator

3. Binary Representation

4. (Additional) Binary Representation of Negative Numbers

5. (Additional) Expressions

1

Self-Assessment

• Gibt euch Rückmeldung über euren Fortschritt.

• Er testet nicht, wie gut Ihr bis jetzt in den Vorlesungen aufgepasst habt.

• Die Teilnahme ist komplett freiwillig, aber stark empfohlen.

• Die erzielte Note zählt in keiner Weise mit zur Endnote!

• Er wird unmittelbar im Anschluss zusammen korrigiert.

• Zeit: 15 Minuten

• https://moodle-app2.let.ethz.ch/mod/quiz/view.php?id=372227

2

Repetition: Integer

• What is a variable?

• What is an integer?

• What is an unsigned integer?

• Are there other variable types?

• float / double

• char / string

• bool , etc.

3

Modulo Operator

• Dividing int or unsigned int variables ignores the digits behind the decimalpoint.

5/3 == 1 (integer division)

• The Modulo-Operator % calculates the remainder.5 % 3 == 2

• Attention: = (Assignment) and == (comparison operator) are NOT the same!

4

Module Operator: ExamplesDivision: Modulo:

7 / 3 == 2 7 % 3 == 115 / 4 == 3 15 % 4 == 316 / 4 == 4 16 % 4 == 0

Re-gaining original number: (15 / 4) * 4 + 15 % 4 == 15

2012 % 2 == 0 // even2011 % 2 == 1 // odd

Questions?

5

Exercise: Last Three Digits

Task: Write a program which reads in an integer a larger than 1000 and outputs its lastthree digits. For example, if a=14325, the output should be 3 2 5.

Input: An integer larger than 1000.Output: Last three digits separated by spaces.

Hint: You need to use integer division and modulo operators.

SOLVE ON PEN AND PAPER

6

Exercise: Last Three Digits II

Solve the exercise on [code]expert (www.expert.ethz.ch) pairwise.

7

Exercise: Last Three Digits - A possible solution#include <iostream >#include <cassert >int main() {

unsigned int a; // inputstd::cin >> a;assert(a >= 1000);// computationint digit0 = a % 10;int remainder0 = a / 10;int digit1 = remainder0 % 10;int remainder1 = remainder0 / 10;int digit2 = remainder1 % 10;// outputstd::cout << digit2 << "␣" << digit1 << "␣" << digit0 << std::endl;return 0;

}

8

Binary Representation

Question: How to change the program from the previous exercise to output last threebits?

Possible Solution: Change all 10 into 2.

9

Binary Representation II

Question: What is the meaning of 10 or 2 in this program?

Possible Solution: It is the base of the system into which we are converting.

10

Binary Representation III

Bits: 0 and 1 in the binary system are similar to numeric symbols0,1,2,3,4,5,6,7,8,9 in the decimal system.

1 1 1 1 . 1 1 1 123 22 21 20 . 2−1 2−2 2−3 2−4

8 4 2 1 . 12

14

18

116

11

Binary Representation IV

X(10) X(2)

0 01 12 103 114 1006 1107 1118 100015 111116 10000

Example: 61binary 1 1 1 1 0 1

decimal 32 16 8 4 0 1 = 61

12

Binary Representation: Algorithm

Question: What would be an algorithm to convert from decimal to binary?

Possible Solution: Divide the decimal number by two and keep the rest. Repeat untilyou reach 0:

61 = 2*30 + 130 = 2*15 + 015 = 2* 7 + 17 = 2* 3 + 13 = 2* 1 + 11 = 2* 0 + 1

binary of 61: 1 1 1 1 0 1

13

Number of Bits

Question: What is the largest number that can be stored in an unsigned int in4bits?

Possible Solution: 1 1 1 1 == 15

14

Binary Representation of Negative Numbers I

Motivation: add 1 to -1 and get zero.General: add x to -x and get zero

In 4bit binary 110 is represented as:→ 00012

and 010 is represented as:→ 00002

What do we need to add to 00012 and get 00002?Answer: 00012 + 11112 = 00002

15

Binary Representation of Negative Numbers II

How can we generalise the previous approach to any arbitrary negative number x?

Answer: Two’s complement

1. Convert the absolute value of x to binary.

2. Flip all bits.

3. Add 1.

What is the largest and smallest number that we can represent by using two’scomplement in 4-bits?Smallest: −23 = −8, which is 10002

Largest: 23 − 1 = 7, which is 01112

16

From Lecture: Evaluating ExpressionsAn expression is a sequence of operators and their operands, that specifies acomputation.

5u + 5 * 3u5u + (5 * 3u)5u + 15u20u

(u is an unsigned int)

int a = 5;std :: cout << a << ++a;

→ Avoid modifying a variable if used again in the same expression!

17

From Lecture: Evaluating Expressions II

In groups of two:

1. Which of the following are not C ++ expressions, and why not? a and b here arevariables of type int.

(i) 1*(2*3) (ii) (a=1)(iii) (1 (iv) (a*3)=(b*5)

2. Now decide for every expression that you have identified as correct in ??, whetherit acts as an lvalue or rvalue value. Why?

3. Determine the values of the expressions and explain how to use them. Whichvalues are unspecified and therefore can not be clearly used?

18

From Lecture: Evaluating Solutions

1. (i) 1*(2*3) : valid expression(ii) (a=1) : valid(iii) (1 : Not an expression, since there is no closing parenthesis.(iv) (a*3)=(b*5) : Invalid, since (a*3) is an rvalue, but the left operand of theassignment operator must be an lvalue.

2. (i) An rvalue by definition of the multiplication operator *.(ii) An lvalue by definition of the assignment operator =.

3. (i) The value is 6, obtained by evaluating the two multiplications.(ii) The value is 1, obtained by assigning value 1 to a.

19

Questions?

20

top related