DOC PREVIEW
CSUN COMP 106 - Review for Final

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Review for final May 18, 20061Review for FinalReview for FinalLarry CarettoComputer Science 106Computing in Engineering and ScienceMay 18, 20062The Final• Thursday, Tuesday, May 23, 12:45 to 2:45 pm in this room• Closed book, except for C++ guide• Problems like the first quiz, midterm and homework assignments– Interpret code and give the results of the code (show your reasoning!)– Write code to accomplish a simple task3Outline• Data types, operators, expressions, assignment, and type conversion• Simple programs with sequential statements and input/output• Use of if statements for choice• Looping commands• Functions• Arrays4Data Types• All variables must be declared as belonging to a certain data type– Good idea to initialize data at the same time that it is declared– Beware of scope rules• Know int, double, char, and string• Types (classes) for file variable names: fstream, ofstream, ifstream5Assignment Operator (=)• <variable> = <expression>• Assignment operator not an equality• Expression is a constant, a variable, or combination of constants, variables and operators• Have arithmetic, relational and logical operators– Arithmetic operators in order of precedence (unary–) (* % /) (+ –)6Operator Precedence• Determines how operators are applied• Can use parentheses to overcome normal rules of precedence– Important for getting correct equations• Precedence order: arithmetic, relational, logical– Relational precedence (<, >, <=, >=) (== !=)– Logical precedence ! && ||Review for final May 18, 200627Mathematical Statements• Must be in correct sequential order– Input data– Do calculations in order: quantities on left sides of = operator must be calculated first– Write output• Can use functions in <cmath> library such as exp, pow, log, sin, cos, atan, …• Use type double for calculations, reserve type int for counting8Types of Statements• Sequential• Choice– Given by if statements• Looping (repetition)– Test before versus test after– for loop for counting loops• Function– Transfer control and data from one function to another and back9Conditions• Choice and loops use conditions, expressions that have (Boolean) values of true or false• Use relational operators <, >, <=, >=, ==, and !=• Combine conditions with Boolean (bool) operators: not(!), and(&&), or(||)– Examples: ( x < 3 ), (hours > 40), (age >= 16 && age < 75)10Simple if Statementif ( <condition>){< true statement block >}<next statement executed if condition is false>11Simple if-else Statementif (<condition>){< true statement block >}else{< false statement block >}<next statement executed after true or false block>12if-else if Statementif ( <condition 1>){< statement block 1 >}else if ( <condition 2>) {< statement block 2 >}··················else if ( <condition n>) {< statement block n >}else{<allConditionsFalse block>}<next statement executed after the selected block is executed>• Only one block executes• Final else is optionalReview for final May 18, 2006313Nested If Statements• Can have one if block inside another• Example: Find number of days in month– If the number of the month is 4, 6, 9, or 11 the answer is 30– If the number of the month is 2• If it is a leap year, the answer is 29• Otherwise the answer is 28– For all other month numbers (1, 3, 5, 7, 8, 10, and 12) the answer is 3114Simple while Statementwhile ( <condition>){< loop body >}< next statement >• Loop body statements are executed repeatedly if condition is true• May never be executed once• Control transfers when condition is false15do-while Statementdo{< loop body >}while ( <condition>);< next statement >• Loop body statements are executed repeatedly if condition is true• Loop body will be executed first time• Control transfers when condition is false16Count-controlled while Loopint count = 0;while ( count < maxCount ){cout << count << “ times”;count = count + 1;}< next statement >Initialize counterTest counter to continue loopUse counter value in loopIncrement counter17Count-controlled for Loopfor( int count = 0; count <maxCount; count++ ){cout << count << “ times”;}< next statement >Initialize counter Test counter to continue loopUse counter value in loopIncrement counter18Nested Loops• Nested loop example of printing a table// print column headersfor ( v1 = a; v1 < b; v1 += c){ cout << “\nv1 = “ << v1;for ( v2 = d; v2 < e; v2 += f )cout << setw(12) << v3(v1, v2)}// watch roundoff in loop controlsReview for final May 18, 2006419Loop Errors• Make sure that you have the correct numerical limits on loops• Is continuation condition < or <=– Can have > or >= conditions where loop index is decremented• Check values of limits in conditions, especially when they have variables– Do a simple mental test to see if your code gives the desired results20How do we write functions?• C++ code is a collection of functions• Each function, including main, has the same level of importance– Close code for each function before starting a new functionint main(){ // body of main}int myFunction( …… ){ // body of myFunction}21Function Examplebool leap( int year ){if ( year % 4 != 0 )return false;else if ( year % 400 == 0 )return true;else if ( year % 100 == 0 )return false;elsereturn true;}HeaderBodyNameTypeArgu-mentListMultiple returnsbool leap( int year );Prototype22Use of bool leap( int year )bool leap ( year ); // prototypeint main() // examples of use{ cout << “Enter a year: “;int y; cin >> y;bool cond = leap( y );if ( leap( y ) )if ( leap( y ) && month == 2 )………………………………23Use of Functions• Data is transmitted to a function based on the order of the arguments in the function header• The order of the arguments in the function call gives the correspondence between the call and the function– Names do not matter, it is only the order of the arguments in the function header and the call statement that count24Function Exampledouble myPow( double n, double p){ return exp( p * log( n ) ); }int main(){ double a = 3, n = 4, p = 2, r = 6;cout << myPow ( a, n );cout << myPow ( p, r );cout << myPow ( p, n );34= 8126= 6424= 16Review for final May 18, 2006525Pass by Value and Reference• Pass by value is the normal operation– The value of the parameter in the calling code is passed to the function– If the corresponding dummy parameter in the function is changed, no change


View Full Document

CSUN COMP 106 - Review for Final

Download Review for Final
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Review for Final and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Review for Final 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?