90 likes | 186 Views
CS 109 C/C++ Programming for Engineers with MATLAB. Administrative HW 3 due Friday, Jan 31 st @ 9pm 2 parts, each part is worth 100 points Lab sections are meeting, attendance mandatory Thursday : 8am and 9am, 2249 SELE Thursday: 10am and 11am, 2263 SELE (Mac lab)
E N D
CS 109 C/C++ Programming for Engineers with MATLAB • Administrative • HW 3 due Friday, Jan 31st @ 9pm • 2 parts, each part is worth 100 points • Lab sections are meeting, attendance mandatory • Thursday: 8am and 9am, 2249 SELE • Thursday: 10am and 11am, 2263 SELE (Mac lab) • Friday: 8am, 9am and 10am, 2249 *F* SELE • Topics for today: • Finish up with selection • Computers are finite machines— implications? CS 109 -- 29 Jan 2014
The many forms of selection… • if-then-else: • if-then: • single-stmt: if (condition) { then-part } else { else-part } if (condition) { then-part } if (condition) single-stmt; else single-stmt; if (condition) single-stmt; exception #2: { } are optional if they contain only 1 statement exception #1: else-part is optional, if condition is false ==> do nothing CS 109 -- 29 Jan 2014
Common uses… nested or "chained" independent . . . if (...) { } else { if (...) { } else { } } . . . . . . if (...) { } if (...) { } if (...) { } . . . CS 109 -- 29 Jan 2014
Computers are finite machines… • So? CS 109 -- 29 Jan 2014
Computers are finite! • RAM (memory) is fixed size • Hard disks are fixed size • Numbers have fixed size • integers are 32 or 64 bits in size • real numbers are 64 or 128 bits in size • the "size" of your computer — 32-bit or64-bit — refers to max memory size andtypical integer size 00001101010101010101010101110001 32 bits ("binary digits") Finite machines have limits… CS 109 -- 29 Jan 2014
Integer number line on a computer: • for 32-bit integers (int): • for 64-bit integers (long long) -231 0 231 - 1 -263 0 263 - 1 CS 109 -- 29 Jan 2014
Implication: • What happens when you exceed endpoints? • For example, what if we add 1 to the maximum integer? #include <iostream> #include <climits> using namespace std; intmain() { inti; i = INT_MAX; cout << i << endl; i = i + 1; cout << i << endl; return 0; } output? output? CS 109 -- 29 Jan 2014
Real numbers on a computer: • Consider the real number line from just 0.0 to 1.0… How many real numbers are there? 0.0 1.0 CS 109 -- 29 Jan 2014
Implication? • No way to represent them all ― some must be approximated… double x, y, z; x = 0.9; y = 0.1; z = 1.0 - x - y; cout << z << endl; double x, y, z; x = 0.8; y = 0.2; z = 1.0 - x - y; cout << z << endl; output? output? CS 109 -- 29 Jan 2014