05 - Iteration and Conditionals

05 - Iteration and Conditionals - Engineering 101 Iteration...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
Engineering 101 Iteration and Conditionals
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Quote of the Day - Martin H. Fischer It is not hard to learn more. What is hard is to unlearn  when you discover yourself wrong.
Background image of page 2
Before we get started… n A note on the increment/decrement operators: x++; x--; is the same as is the same as x = x + 1; x = x - 1; Increment Operator Decrement Operator
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Before we get started… n And some more nice little shorthands: x += 5; x -= 7; is the same as is the same as x = x + 5; x = x - 7;
Background image of page 4
Iteration n So far we have only used  sequence . n Sequence  allows for the computation of  some complicated formulas, but not the  creation of dynamic programs. n Iteration  allows the computer program to  employ  repetition  to achieve a goal.  n Iteration  often allows us to devise  more  general solutions  to problems.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
while  statement n Iteration  can be accomplished with a while  statement in the form while (condition) loop_body n The  condition  evaluates to True or False. n The  loop body  is an expression or compound  statement in {braces}. n If the condition is true  the loop body executes,  then the condition is checked again. n The loop body is repeatedly executed  as long as  the condition holds .
Background image of page 6
Example: Combinatorics n How many ways can you choose 5 balls in  groups of 3? 123 124 125 134 135 145 234 235 245 345 n 10 ways!
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: Combinatorics n In general if you are choosing from m things n  at a time: n So choosing from 5 things 3 at a time ( 29 ! ! ! n m n m n m C - = = ( 29 ( 29 10 2 6 120 1 2 1 2 3 1 2 3 4 5 ! 2 ! 3 ! 5 = = = = C
Background image of page 8
Example: Combinatorics n To compute this n We need to compute the factorial function. n Write a function  to compute N! ( 29 ! ! ! n m n m n m C - = =
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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; } return f; } int main( ){ cout << fact(5); return 0; }
Background image of page 10
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; } return f; } N 5 int main( ){ cout << fact(5); return 0; }
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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; } return f; } N 5 f 1 count 1 int main( ){ cout << fact(5); return 0; }
Background image of page 12
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; } return f; } N 5 f 1 count 1 int main( ){ cout << fact(5); return 0; }
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Factorial Function // factorial, assumes N is positive int fact( int N){
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/30/2012 for the course ENGR 101 taught by Professor Ringenberg during the Winter '07 term at University of Michigan.

Page1 / 62

05 - Iteration and Conditionals - Engineering 101 Iteration...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online