Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data types and their representation Jordi Cortadella Department of Computer Science.

Similar presentations


Presentation on theme: "Data types and their representation Jordi Cortadella Department of Computer Science."— Presentation transcript:

1 Data types and their representation Jordi Cortadella Department of Computer Science

2 Outline Data representation Boolean expressions Real numbers Introduction to Programming© Dept. CS, UPC2

3 The memory 00100100111000101010010010100010000000000000000000000000000100110010010001110000011010010101000010010011000010101000000000000000010010101000010000101000000001000010010111100000010000100100000111011010000100100000100010010010010101010100011111010000000010010010100010010110001001000101011001110011010010001000101010000000000000000000000000000000000000000010110100000100001111100100010000100010010000011001000000100001 Address 1036 1040 1044 1048 1052 1056 1060 1064 1068 1072 1076 1080 1084 byte int 19 0 Hell owo rld string (“Hello world”) Introduction to Programming© Dept. CS, UPC3

4 How much memory do we have? Our laptop has few Gbytes of memory: Introduction to Programming© Dept. CS, UPC4

5 Number representation 2 100101 17 base 10 3 2551 321 125 base 5 0 931 012 27 2 81 base 3 0 421 011 8 1 16 011 3264128 base 2 CCXVII Roman Introduction to Programming© Dept. CS, UPC5

6 1 Write the binary representation Design a procedure that, given a number n, writes its binary representation (in reverse order). // Pre: n  0 // Writes the binary representation of n // in reverse order. void base2(int n); 0 421 011 8 1 16 011 3264128 Write: 10011011 0011011 n%2 n/2 Introduction to Programming© Dept. CS, UPC6

7 Write the binary representation // Pre: n  0 // Writes the binary representation of n // in reverse order. void base2(int n) { while (n > 1) { cout << n%2; n = n/2; } cout << n << endl; } ncout 2171 1080 540 271 131 60 31 11 Introduction to Programming© Dept. CS, UPC7

8 Exercise Design a procedure that, given a number n and a base b, writes the representation of n in base b (in reverse order). // Pre: n  0, 2  b  9. // Writes the binary representation of n // in base b (in reverse order). void base(int n, int b); Introduction to Programming© Dept. CS, UPC8

9 Boolean expressions Introduction to Programming© Dept. CS, UPC9

10 Boolean Algebra George Boole, 1815-1864 Introduction to Programming© Dept. CS, UPC10

11 Maximum of three numbers // Returns max(a, b, c) int max3(int a, int b, int c); a > b and a > c a a b b b > c c c else a,b,c else b,c a >= b and a >= c a a b b c c else b >= celse a,b,c b,c Introduction to Programming© Dept. CS, UPC11

12 Maximum of three numbers // Returns max(a, b, c) int max3(int a, int b, int c) { if (a > b and a > c) { return a; } else { if (b > c) { return b; } else { return c; } } } Choose between b and c a > b and a > c a a b b b > c c c else a,b,c else b,c Introduction to Programming© Dept. CS, UPC12

13 Maximum of three numbers // Returns max(a, b, c) int max3(int a, int b, int c) { if (a > b and a > c) return a; if (b > c) return b; return c; } Introduction to Programming© Dept. CS, UPC13

14 Boolean operators if (a > b and a > c) … while (i >= 10 or c == 0) … if (not (a < b and a < c)) … Introduction to Programming© Dept. CS, UPC14

15 Truth tables aba and b false truefalse truefalse true aba or b false true falsetrue anot a falsetrue false Introduction to Programming© Dept. CS, UPC15

16 … Boolean expressions -3-2 0 1 2 3 4 5 6 7 8 910111213 x > 2 and x < 7 -3-2 0 1 2 3 4 5 6 7 8 910111213 … …  x >= 3 and x <= 6 x 7 -3-2 0 1 2 3 4 5 6 7 8 910111213 … x <= 2 not (x <= 2)  x > 2 -3-2 0 1 2 3 4 5 6 7 8 910111213 ? x == 5 x Introduction to Programming© Dept. CS, UPC16

17 … Boolean expressions -3-2 0 1 2 3 4 5 6 7 8 910111213 x > 2 and x < 7 -3-2 0 1 2 3 4 5 6 7 8 910111213 … …  x >= 3 and x <= 6 x 7 -3-2 0 1 2 3 4 5 6 7 8 910111213 … x <= 2 not (x <= 2)  x > 2 -3-2 0 1 2 3 4 5 6 7 8 910111213 not (x == 5)  x != 5 … … Introduction to Programming© Dept. CS, UPC17

18 Exercise -3-2 0 1 2 3 4 5 6 7 8 910111213 x == 0 or x == 6 Introduction to Programming© Dept. CS, UPC18

19 Exercise -3-2 0 1 2 3 4 5 6 7 8 910111213 x == 0 or x > 5 … Introduction to Programming© Dept. CS, UPC19

20 Exercise -3-2 0 1 2 3 4 5 6 7 8 910111213 x%2 == 0 … … Introduction to Programming© Dept. CS, UPC20

21 Exercise -3-2 0 1 2 3 4 5 6 7 8 910111213 (x > -1 and x = 6 and x < 11) Introduction to Programming© Dept. CS, UPC21

22 Complement of and/or -3-2 0 1 2 3 4 5 6 7 8 910111213 … … (x > 2 and x < 7) not (x > 2 and x < 7) x = 7 not De Morgan’s law Introduction to Programming© Dept. CS, UPC22

23 De Morgan’s law not (e 1 and e 2 )  not e 1 or not e 2 not (e 1 or e 2 )  not e 1 and not e 2 Exercise Simplify: not (x >= y or y%2 == 0) x < y and y%2 != 0 Introduction to Programming© Dept. CS, UPC23

24 Operator precedence a + bc (a + b)c a or b and c (a or b) and c Introduction to Programming© Dept. CS, UPC24

25 Real numbers Introduction to Programming© Dept. CS, UPC25

26 Intersection of circles x1, y1, r1 x2, y2, r2 Write a program that reads the center and the radius of two circles and prints “yes” or “no” depending on whether they intersect or not. Example: 2 5.3 1.34 0 0 2 no 1.5 2.5 10 0.5 3.6 4.3 yes Introduction to Programming© Dept. CS, UPC26

27 Intersection of circles Circles intersect if and only if the distance between centers is smaller than or equal to the sum of radii. Introduction to Programming© Dept. CS, UPC27

28 Intersection of circles Introduction to Programming© Dept. CS, UPC28

29 // Reads the center and radius of two circles // and prints whether they intersect or not. int main() { double x1, y1, r1, x2, y2, r2; // Real numbers cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2; double dx = x1 – x2; dx = dxdx; // (x1 – x2)^2 double dy = y1 – y2; dy = dydy; // (y1 – y2)^2 double r = r1 + r2; r = rr; // (r1 + r2)^2 bool intersect = dx + dy <= r; // true or false if (intersect) cout << "yes" << endl; else cout << "no" << endl; } Intersection of circles Introduction to Programming© Dept. CS, UPC29

30 Boolean variables bool b = false; b = i n b = x + y > z; b = (i = 20); b = (large and not found) or (x > y); b = true; if (b) { … } // if (b == true) { … } while (not b) {…} // while (b == false) { … } Introduction to Programming© Dept. CS, UPC30

31 Real numbers Two types: – float (single precision, 32 bits) – double (double precision, 64 bits) Arithmetic operations: + -  / (no remainder) Real constants: 2 -5.003 3.1416 1.4e9 0.6E-15 Introduction to Programming© Dept. CS, UPC31

32 Type conversion Arithmetic operations between integer and real values usually imply an implicit type conversion. Be careful: int i=3, j=2; double x; x = i/j; // x = 1.0 x = i/double(j); // x = 1.5 x = double(i)/j; // x = 1.5 x = double(i/j); // x = 1.0 x = i/2; // x = 1.0 x = i/2.0; // x = 1.5 i = x; // i = 1 j = 3.14159265; // j = 3 Introduction to Programming© Dept. CS, UPC32

33 // Returns x 2 double sq(double x) { return xx; } // Reads the center and radius of two circles // and prints whether they intersect or not. int main() { double x1, y1, r1, x2, y2, r2; cin >> x1 >> y1 >> r1 >> x2 >> y2 >> r2; if (sq(x1 – x2) + sq(y1 – y2) <= sq(r1 + r2)) cout << "yes"; else cout << "no"; cout << endl; } Revisiting intersection of circles Introduction to Programming© Dept. CS, UPC33

34 Summary Variables are stored in memory locations and internally represented as bit vectors. Boolean expressions and variables: – Can take two values: true or false. – Use negative thinking when simpler than positive thinking (apply De Morgan’s law). Real values: – Can be represented with a limited precision. – Be careful with int  double conversions. Introduction to Programming© Dept. CS, UPC34


Download ppt "Data types and their representation Jordi Cortadella Department of Computer Science."

Similar presentations


Ads by Google