Lecture+09 - Engineering 101 Engineering 101 Lecture 9...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Engineering 101 Lecture 9 10/2/07 Iteration and Conditionals Quote of the Day Quote of the Day It is not hard to learn more. What is hard is to unlearn when you discover yourself wrong. ­ Martin H. Fischer Project 3 Project 3 From last From last lecture int main( ){ int a, b, c, d, e, f; ofstream outfile(“outfile.txt”); permute(outfile, 1, 2, 3); outfile.close(); ifstream infile(“outfile.txt”); infile >> a >> b >> c >> d >> e >> f; infile.close(); cout << f << a << endl; cout << e << b << endl; cout << d << c << endl; return 0; void permute(ofstream & fout, int a, int b, int c) { fout << a << b << c << endl; fout << a << c << b << endl; fout << b << a << c << endl; fout << b << c << a << endl; fout << c << a << b << endl; fout << c << b << a << endl; return; } } From last lecture int main( ){ int main( ){ int a, b, c, d, e, f; ofstream outfile(“outfile.txt”); permute(outfile, 1, 2, 3); outfile.close(); ifstream infile(“outfile.txt”); infile >> a >> b >> c >> d >> e >> f; infile.close(); cout << f << a << endl; cout << e << b << endl; cout << d << c << endl; return 0; void permute(ofstream & fout, int a, int b, int c) { fout << a << b << c << endl; fout << a << c << b << endl; fout << b << a << c << endl; fout << b << c << a << endl; fout << c << a << b << endl; fout << c << b << a << endl; return; } } 123 132 213 231 312 321 From last lecture int main( ){ int main( ){ int a, b, c, d, e, f; ofstream outfile(“outfile.txt”); permute(outfile, 1, 2, 3); outfile.close(); ifstream infile(“outfile.txt”); infile >> a >> b >> c >> d >> e >> f; infile.close(); cout << f << a << endl; cout << e << b << endl; cout << d << c << endl; return 0; 123 132 213 231 312 321 a 123 c 213 e 312 b 132 d 231 f 321 } From last lecture int main( ){ int main( ){ int a, b, c, d, e, f; ofstream outfile(“outfile.txt”); permute(outfile, 1, 2, 3); outfile.close(); ifstream infile(“outfile.txt”); infile >> a >> b >> c >> d >> e >> f; infile.close(); cout << f << a << endl; cout << e << b << endl; cout << d << c << endl; return 0; 123 132 213 231 312 321 a 123 c 213 e 312 b 132 d 231 f 321 321123 312132 231213 } Iteration Iteration So far we have only used sequence. Sequence allows for the computation of some complicated formulas, but not the creation of dynamic programs. Iteration allows the computer program to employ repetition to achieve a goal. Iteration often allows us to devise more general solutions to problems. Iteration can be accomplished with a while statement in the form while (condition) loop_body while statement while The condition evaluates to True or False. The loop body is an expression or compound statement in {braces}. If the condition is true the loop body executes, then the condition is checked again. The loop body is repeatedly executed as long as the condition holds. Example: Combinatorics Example: Combinatorics How many ways can you choose 5 balls in groups of 3? 123 124 125 134 135 145 234 235 245 345 10 ways! Example: Combinatorics Example: Combinatorics In general if you are choosing from m things n at a time: m m! C = = n n!( m − n )! So choosing from 5 things 3 at a time 5! 5 ⋅ 4 ⋅ 3 ⋅ 2 ⋅1 120 C= = = = 10 3!2! ( 3 ⋅ 2 ⋅1) ( 2 ⋅1) 6 ⋅ 2 Example: Combinatorics Example: Combinatorics To compute this m m! C = = n n!( m − n )! We need to compute the factorial function. Write a function to compute N! Factorial Function Factorial Function // factorial, assumes N is positive int fact( int N){ int count=1, f=1; while (count <= N){ f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ int count=1, f=1; while (count <= N){ f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 1 int count=1, f=1; while (count <= N){ f 1 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 1 int count=1, f=1; while (count <= N){ f 1 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 1 int count=1, f=1; while (1 <= 5){ f 1 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 1 int count=1, f=1; while (True){ f 1 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 1 int count=1, f=1; while (count <= N){ f 1 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 2 int count=1, f=1; while (count <= N){ f 1 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 2 int count=1, f=1; while (2 <= 5){ f 1 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 2 int count=1, f=1; while (count <= N){ f 2 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 3 int count=1, f=1; while (count <= N){ f 2 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 3 int count=1, f=1; while (3 <= 5){ f 2 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 3 int count=1, f=1; while (count <= N){ f 6 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 4 int count=1, f=1; while (count <= N){ f 6 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 4 int count=1, f=1; while (4 <= 5){ f 6 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 4 int count=1, f=1; while (count <= N){ f 24 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 5 int count=1, f=1; while (count <= N){ f 24 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 5 int count=1, f=1; while (5 <= 5){ f 24 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 5 int count=1, f=1; while (count <= N){ f 120 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 6 int count=1, f=1; while (count <= N){ f 120 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 6 int count=1, f=1; while (6 <= 5){ f 120 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 6 int count=1, f=1; while (6 <= 5){ f 120 f = f * count; count = count + 1; int main( ){ } cout << fact(5); return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 6 int count=1, f=1; while (6 <= 5){ f 120 f = f * count; count = count + 1; int main( ){ } cout << 120; return f; return 0; } } Factorial Function Factorial Function // factorial, assumes N is positive N 5 int fact( int N){ count 6 int count=1, f=1; while (6 <= 5){ f 120 f = f * count; count = count + 1; int main( ){ } cout << 120; return f; return 0; } } The Heart of fact() The Heart of fact() // factorial, assumes N is positive int fact( int N){ int count=1, f=1; while (count <= N){ f = f * count; count = count + 1; } return f; } The Heart of fact() The Heart of fact() // factorial, assumes N is positive int fact( int N){ int count=1; while (count <= N){ do something count = count + 1; } return f; } The Heart of fact() The Heart of fact() // factorial, assumes N is positive int fact( int N){ int count=1; intialization while (count <= N){ do something count = count + 1; } return f; } The Heart of fact() The Heart of fact() // factorial, assumes N is positive int fact( int N){ int count=1; intialization while (count <= N){ do something count = count + 1; increment } return f; } The Heart of fact() The Heart of fact() // factorial, assumes N is positive int fact( int N){ int count=1; intialization while (count <= N){ continuation condition do something count = count + 1; increment } return f; } Using fact() Using fact() // factorial, assumes N is positive int fact( int N){ int main( ){ int count=1, f=1; int m, n, ans; cout <<“Input m & n”; while (count <= N){ cin >> m >> n; f = f * count; ans = fact(m) / fact(n); count = count + 1; ans = ans / fact(m-n); } cout << ans; return 0; return f; } } m m! C = = n n!( m − n )! Combinatorics Combinatorics Our program is inefficient. Why? Combinatorics Combinatorics Our program is inefficient. Why? 6!/3! = 6 x 5 x 4 : two multiplications as opposed to 7 multiplications and 1 division. So, which of the following functions computes m!/n! ? int fact2( int M, int N); Which function computes m!/n! ? Which function computes Version B // Computes M!/N! // assumes M and N are non­negative int fact2( int M, int N){ int main( ){ int count=N+1, f=1; int m, n, ans; while (count <= M){ cout <<“Input m & n”; f = f * count; cin >> m >> n; ans = fact2(m,n); count = count + 1; ans = ans / fact2(m-n,1); } cout << ans; return f; return 0; } } Factorial Function Factorial Function A Little Fun A Little Fun Dr: I’m afraid your son has…the Knack. Mom: The Knack? Dr: The Knack ­ It’s a rare condition characterized by an extreme intuition about all things mechanical and electrical, and…utter social ineptitude. Mom: Can he lead a normal life?!? Dr: No…he’ll be an engineer. And then a real engineer And then a real engineer http://www.youtube.com/watch?v=xs6zBN_GEJ8 Relational Operators Relational Operators Recall that a while loop always includes a condition. As long as the condition is true the loop body continues to be executed. Relational Operators Relational Operators A condition is an expression that will evaluate to either true or false. The computer recognizes the literal expressions “true” and “false”. They are Boolean values and there is a simple type bool that can only adopt these two values true evaluates to 1 and false evaluates to 0. A condition typically involves a relational operator as part of an expression. Is A greater than B? Is C equal to 5? Relational Operators Relational Operators Operator Meaning less than greater than less than or equal to greater than or equal to is equal to is not equal to Arity binary binary binary binary binary binary Placement infix infix infix infix infix infix Precedence 10 10 10 10 9 9 < > <= >= == != all associate left-to-right Relational Operators Relational Operators The relational operators have lower precedence than the mathematical operators (+ ­ * /), but higher precedence than assignment (=) Note that “=” means assignment and “==” means “is equal to?” These are often confused. 5 + 4 == 9 3 * 6 <= 2 * 2 true false Relational Operators Relational Operators You can assign the results of conditional expressions to identifiers. bool a; a = 5 <= 4; a = a == 0; cout << a; Relational Operators Relational Operators You can assign the results of conditional expressions to identifiers. bool a; a = false; a = a == 0; cout << a; Relational Operators Relational Operators You can assign the results of conditional expressions to identifiers. bool a; a = false; a = 0 == 0; cout << a; Relational Operators Relational Operators You can assign the results of conditional expressions to identifiers. bool a; a = false; a = true; cout << a; Relational Operators Relational Operators You can assign the results of conditional expressions to identifiers. bool a; a = false; a = true; cout << true; Relational Operators Relational Operators You can assign the results of conditional expressions to identifiers. bool a; a = 5 <= 4; a = a == 0; cout << a; output: 1 True or False? True or False? Evaluate the expressions 6 + 5 == 5 * 2 11 == 10 4 * 3 + 2 >= 6 * 2 14 >= 12 5 + 5 + 5 != pow(4.0, 2.0) 15.0 != 16.0 10 / 3 == 3 3 == 3 10.0 / 3.0 == 3.0 3.33… == 3 1 == 3 10 % 3 == 3 1>2 4 > 3 > 2 False True True True False False False Relational Operators Relational Operators You cannot string relational operators together in the usual way 4 > 3 > 2 the expression Relational Operators Relational Operators You cannot string relational operators together in the usual way 4 > 3 > 2 true > 2 the expression evaluates to Relational Operators Relational Operators You cannot string relational operators together in the usual way 4 > 3 > 2 true > 2 1 > 2 the expression evaluates to which is equivalent to Relational Operators Relational Operators You cannot string relational operators together in the usual way 4 > 3 > 2 true > 2 1 > 2 false the expression evaluates to which is equivalent to which evaluates to Logical Operators Logical Operators Instead you must use a second kind of operator known as a logical operator. 4 > 3 > 2 is really a shorthand for 4>3 and 3>2 Logical Operators Logical Operators Operator Alternate Arity unary binary binary Placement prefix infix infix Precedence 15 5 4 not and or ! && || and and or associate left-to-right not associates right-to-left Operator Precedence Operator Precedence Operators ! ­ * / % + ­ << >> < > <= >= == != and or = Meaning not, sign change multiplication, division addition subtraction insertion extraction less than, greater than is equal / is not equal and or assignment Precedence 15 13 12 11 10 9 5 4 2 Evaluate the Expression Evaluate the Expression 5 <= 6 and 5 > 2 == 1 or 6 + 5 > 10 1­ True 2­ False Evaluate the Expression Evaluate the Expression 5 <= 6 and 5 > 2 == 1 or 6 + 5 > 10 1­ True 2­ False Evaluate the Expression Evaluate the Expression 5 <= 6 and 5 > 2 == 1 or 11 > 10 Evaluate the Expression Evaluate the Expression 1 and 1 == 1 or 1 Evaluate the Expression Evaluate the Expression 1 and 1 or 1 Evaluate the Expression Evaluate the Expression 1 or 1 Evaluate the Expression Evaluate the Expression 1 (true) Evaluate the Expression Evaluate the Expression ! ! ! ! ! ( 5 == 4 or ! ( 3 > 2 ) ) 1­ True 2­ False Evaluate the Expression Evaluate the Expression ! ! ! ! ! ( 5 == 4 or ! ( 3 > 2 ) ) 1­ True 2­ False Evaluate the Expression Evaluate the Expression ! ! ! ! ! ( 5 == 4 or ! ( 3 > 2 ) ) Evaluate the Expression Evaluate the Expression ! ! ! ! ! ( 5 == 4 or ! ( 1 ) ) Evaluate the Expression Evaluate the Expression ! ! ! ! ! ( 5 == 4 or 0 ) Evaluate the Expression Evaluate the Expression ! ! ! ! ! ( 0 or 0 ) Evaluate the Expression Evaluate the Expression ! ! ! ! ! 0 Evaluate the Expression Evaluate the Expression ! ! ! ! 1 Evaluate the Expression Evaluate the Expression ! ! ! 0 Evaluate the Expression Evaluate the Expression ! ! 1 Evaluate the Expression Evaluate the Expression ! 0 Evaluate the Expression Evaluate the Expression 1 (true) Evaluate the Expression Evaluate the Expression ! ( (a=3) > 2 && a > 1 ) 1­ True 2­ False Evaluate the Expression Evaluate the Expression ! ( (a=3) > 2 && a > 1 ) 1­ True 2­ False Evaluate the Expression Evaluate the Expression ! ( (a=3) > 2 && a > 1 ) Evaluate the Expression Evaluate the Expression ! ( 3 > 2 && a > 1 ) a3 Evaluate the Expression Evaluate the Expression ! ( 1 && 1 ) a3 Evaluate the Expression Evaluate the Expression ! ( 1 ) a3 Evaluate the Expression Evaluate the Expression 0 (false) a3 Next Lecture Next Lecture Next Topic ...
View Full Document

This note was uploaded on 03/12/2010 for the course ENGIN 101 taught by Professor Jeffringenberg during the Fall '07 term at University of Michigan.

Ask a homework question - tutors are online