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

Slides:



Advertisements
Similar presentations
Introduction to Programming (in C++) Subprograms: procedures and functions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science,
Advertisements

CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
If Statements & Relational Operators Programming.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The If/Else Statement, Boolean Flags, and Menus Page 180
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Introduction to Programming (in C++) Data types and visibility Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. Computer Science, UPC.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
Binary Numbers.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
UniMAP Sem2-08/09 DKT121: Fundamental of Computer Programming1 Introduction to C – Part 2.
Prime numbers Jordi Cortadella Department of Computer Science.
A little bit of geometry Jordi Cortadella Department of Computer Science.
Recursion Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
Approximate computations Jordi Cortadella Department of Computer Science.
Sequences Jordi Cortadella Department of Computer Science.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Sudoku Jordi Cortadella Department of Computer Science.
CPS120: Introduction to Computer Science Operations Lecture 9.
First steps Jordi Cortadella Department of Computer Science.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CONDITIONALS. Boolean values Boolean value is either true or false It is name after the British mathemetician, George Boole who first formulated Boolean.
Laws of Boolean Algebra Commutative Law Associative Law Distributive Law Identity Law De Morgan's Theorem.
Control statements Mostafa Abdallah
Vectors Jordi Cortadella Department of Computer Science.
Parameter passing Jordi Cortadella Department of Computer Science.
Functions Jordi Cortadella Department of Computer Science.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
Combinatorial problems Jordi Cortadella Department of Computer Science.
A Sample Program #include using namespace std; int main(void) { cout
CS 1428 Exam I Review. Exam Format 130 Total Points – 40 Points Writing Programs – 30 Points Tracing Algorithms and determining results – 20 Points Short.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Constants, Data Types and Variables
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
Basic concepts of C++ Presented by Prof. Satyajit De
Jordi Cortadella, Ricard Gavaldà, Fernando Orejas
Computer Science 210 Computer Organization
Approximate computations
Chapter 7: Expressions and Assignment Statements
Reasoning with invariants
More important details More fun Part 3
For loops, nested loops and scopes
Chapter 7: Expressions and Assignment Statements
Approximate computations
Variable Declarations, Data types, Expressions
Jordi Cortadella Department of Computer Science
Bits and Bytes Boolean algebra Expressing in C
Computer Science 210 Computer Organization
Jordi Cortadella Department of Computer Science
Understanding Conditions
Arithmetic Operations
CS150 Introduction to Computer Science 1
Presentation transcript:

Data types and their representation Jordi Cortadella Department of Computer Science

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

The memory Address byte int 19 0 Hell owo rld string (“Hello world”) Introduction to Programming© Dept. CS, UPC3

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

Number representation base base base base 2 CCXVII Roman Introduction to Programming© Dept. CS, UPC5

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); Write: n%2 n/2 Introduction to Programming© Dept. CS, UPC6

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 Introduction to Programming© Dept. CS, UPC7

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

Boolean expressions Introduction to Programming© Dept. CS, UPC9

Boolean Algebra George Boole, Introduction to Programming© Dept. CS, UPC10

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

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

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

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

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

… Boolean expressions x > 2 and x < … …  x >= 3 and x <= 6 x … x <= 2 not (x <= 2)  x > ? x == 5 x Introduction to Programming© Dept. CS, UPC16

… Boolean expressions x > 2 and x < … …  x >= 3 and x <= 6 x … x <= 2 not (x <= 2)  x > not (x == 5)  x != 5 … … Introduction to Programming© Dept. CS, UPC17

Exercise x == 0 or x == 6 Introduction to Programming© Dept. CS, UPC18

Exercise x == 0 or x > 5 … Introduction to Programming© Dept. CS, UPC19

Exercise x%2 == 0 … … Introduction to Programming© Dept. CS, UPC20

Exercise (x > -1 and x = 6 and x < 11) Introduction to Programming© Dept. CS, UPC21

Complement of and/or … … (x > 2 and x < 7) not (x > 2 and x < 7) x = 7 not De Morgan’s law Introduction to Programming© Dept. CS, UPC22

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

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

Real numbers Introduction to Programming© Dept. CS, UPC25

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: no yes Introduction to Programming© Dept. CS, UPC26

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

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

// 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

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

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

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 = ; // j = 3 Introduction to Programming© Dept. CS, UPC32

// 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

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