CSC 107 – Programming For Science
Announcements
George Boole Mathematician from English middle-class Lived from 1815 – 1864 Started work at age 16 as a teaching assistant Held two assistantships to support family Opened own school after many years of work In 1847 wrote Mathematical Analysis of Logic
Mathematical Analysis of Logic Boole’s book proposed new logical system World began with 2 values– though more created Devised rules to add, subtract, & multiply Work ignored during Boole’s lifetime System only had 2 values, so what was the point? What is done with developer of pointless knowledge? Basis for most technology in the modern age All it took was a simple little discovery…
Gate Combines input(s) to generate output signal Like most electronics, uses “on-off” state Input is "off", if line drops below 2 volts From volts, an input is considered on Gate is deep fried silicon if line goes above 5 volts, Like Boole’s logic, electronics have 2 values Simple gates combine to make modern circuitry All initially part of Boolean algebra Basis of programming at the lowest, rawest level
Truth Table Normal way that Boolean functions presented All combinations of inputs shown in this table This is really easy, inputs must be true or false Output shown for each of the possible inputs Given how it sounds, not at all complicated Very simple rules to follow to construct Does requires you count up to 2
NOT Gate Simplest gate: computes opposite of input Output false when input true; Output true when input false; !a Written in C++ as !a a is gate’s input x is gate’s output ax !a!a!a!a true false a x
OR Gate Equivalent to addition in Boolean algebra If either input is true is going to be checked true when either a OR b are true; false otherwise a || b Written in C++ as a || b a & b are inputs; x is output abx a || b false true false true a b x
AND Gate Equivalent to multiplication in Boolean algebra If both inputs are true is going to be checked True when a AND b are true; false otherwise a && b Written in C++ as a && b a & b are inputs; x is output abx a && b false true false true a b x
Boolean Values 3 boolean functions can combine into computer Billions of gates combined by engineers to make CPU (a && b) || (a && !c) Could use, for example: (a && b) || (a && !c) But how to generate inputs for these functions First need boolean values of true and false Easy in hardware (true is any signal > 3.3V) But how could we do get values within our program?
Relational Operators < ( less than) > ( greater than) <= ( less than of equal to) >= ( greater than of equal to) != ( inequality ≠) == ( equality – if two things have same value)
Relational Operators < ( less than) > ( greater than) <= ( less than of equal to) >= ( greater than of equal to) != ( inequality ≠) == ( equality – if two things have same value) NOT the same as assignment (=)
Assignment vs. Comparison
Relational Operators Relational operators compute bool Like any expression, can be used in any statements int nfl = 32; bool team = 0 > 6; bool group = 45 <= nfl; bool gang = nfl == 32; team = sqrt(144) == 12.0; group = (133 == pow(12, 2)); gang = (group == team); cout << "Gang says: " << gang << endl;
Code Structures in Programming Sequence Selection yesno
if (…) statement First evaluates expression in parenthesis If expression is true, executes next statement Skips over the statement, when expression is false int nyc = 32, dc = 1; bool gang = (nyc > 12) && (dc == 1); if (nyc == dc) cout 20); cout << "Huh?"
if (…) statement
I Want More! Add opening brace ( { ) after closing parenthesis Can now write all statements to execute Add closing brace ( } ) to show where if ends If expression false, execution restarts at that point if (sqrt(x) == 3.0) { cout << "root of x = 3" << endl; cout << "So, x = 9" << endl; }
A Modest Proposal ; if (a == b); Hanging semi-colon is a common bug Also easy to miss when writing & debugging No harm is done by adding (unneeded) braces A little extra typing is worst-case scenario Use braces everywhere Prevents hanging semi-colon bug
A Modest Proposal ; if (a == b); Hanging semi-colon is a common bug Also easy to miss when writing & debugging No harm is done by adding (unneeded) braces A little extra typing is worst-case scenario Use braces everywhere Prevents hanging semi-colon bug Name variables stewardesses to balance typing
Spacing in a Program C++ ignores almost all spaces in a program This also means where most newlines placed ignored Symbolic constants & text in quotes are exceptions This can lead to some very… interesting code
What Does This Compute? char _ [3141 ],__3141[3141];_314159[31415],_3141[31415];main(){register char* _3_141,*_3_1415, *_3__1415; register int _314,_31415,__31415,*_31, _3_14159,__3_1415;*_ =__31415=2,_ [0][_ ]=1[__3141]=5;__3_1415=1;do{_3_14159=_314=0,__ ;for( _31415 =0;_31415<(3,14-4)*__31415;_ )_31415[_3141]=_314159[_31415]= - 1;_3141[*_314159=_3_14159]=_314;_3_141=_ __3_1415;_3_1415= __3_1415 +__3141;for(_31415 = __3_1415 ;_31415;_ ,_3_141 ++,_3_1415++){_314 +=_314<<2 ;_314<<=1;_314+= *_3_1415;_31 =_ _314; if(!(*_31+1) )* _31 =_314 / __31415,_314 [_3141]=_314 % __31415 ;* ( _3__1415=_3_141 )+= *_3_1415 = *_31;while(* _3__1415 >= 31415/3141 ) * _3__1415+= - 10,(*--_3__1415 )++;_314=_314 [_3141]; if ( ! _3_14159 && * _3_1415)_3_14159 =1,__3_1415 = 3141-_31415;}if( _314+(__31415 >>1)>=__31415 ) while ( ++ * _3_141==3141/314 )*_3_141--=0 ;}while(_3_14159 ) ; { char * __3_14= "3.1415"; write((3,1) (--*__3_14,__3_14 ),(_3_ ,++_3_14159)) ; } for ( _31415 = 1; _31415< ;_ )write( 31415% 314-( 3,14),_ [ _31415 ] + " ","314" [ 3]+1)-_314; puts((*_ =0,_ )) ;_314= *" ";}
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces)
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is more
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is more annoying
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is more annoying than
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is more annoying than looking
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is more annoying than looking for
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is more annoying than looking for the next
Indentation Traditionally we indent code within braces ( {} ) Use consistent size to indent (I use 2 spaces) Nothing is more annoying than looking for the next line.
Your Turn Get in groups & work on following activity
For Next Lecture Read sections – 7.3 for Tuesday What if we want to have multiple possible options? What do else & else if statements do? Are there any rules about how to use them? Week #3 weekly assignment due Tuesday Problems available on Angel If problem takes more than 10 minutes, TALK TO ME!