Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l...

21
Lab 3 Alternation Multiple Alternative Decision Nested If Statements Iteration While Loop For Loop Do Loop Note: Look at math.h (Appendix B AP13), and limits.h (Appendix B AP19). Read about C operators (Appendix C AP22).
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    218
  • download

    2

Transcript of Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l...

Page 1: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Lab 3 Alternation

Multiple Alternative Decision Nested If Statements

Iteration While Loop For Loop Do Loop

Note:

Look at math.h (Appendix B AP13), and limits.h (Appendix B AP19).

Read about C operators (Appendix C AP22).

Page 2: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Alternation

Definition : Alternative Steps to take according to a particular condition.

Exp1: You need to develop an algorithm to control the warning signs at the exits of major tunnels.

If road_status is slick , you will have to check the temperature. If It is higher than 0, you will display the message “Wet road”, otherwise, you will display “Icy road”.

If the road is not slick, you want to display the message “Drive carefully”.

Page 3: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Exp1 Solution

Specification of the problem:

The problem’s objective is to display a warning message given the road_status.

Analysis Input : road_status, temp. Output: warning message.

Page 4: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Start

Get road_status

If ((road_status ==‘S’) || (road_status ==‘s’) )

{ Get temp

if (temp>0)

{display « Wet Road»}

else

{display « Icy Road»}

Endif }

Else

{display «Drive Carefully»}

Endif

End

Algorithm:

Page 5: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Exp2: Write a condition that identifies single males between the ages 16 and 26 inclusive, and display the message “All Criteria Are Met”.

Exp3: Implement the following decision table using nested if statement. Assume that the wind speed is given as an integer.

Wind Speed (mph) Category below 25 not a strong wind 25-38 strong wind 39-54 gale 55-72 whole gale above 72 hurricane

Page 6: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Exp2 Solution

If ((marital_status == ‘S’)|| (marital_status == ‘s’))

{ if ((gender == ‘M’)|| (gender == ‘m’))

{ if ((age>=18)&&(age<=26))

display « All Criteria are Met»

endif}

endif

} endif

Page 7: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Exp3 Solution

Specification of the problem:

The problem’s objective is to display the Wind’s Category according to the Wind’s speed.

Analysis Input: Wind_speed. Output: Category message.

Page 8: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Start

Get Wind_speed

If (Wind_speed <=24)) {display « not a strong wind»}Else if(Wind_speed <=38) {display « strong wind»} Else if(Wind_speed <=54) {display « gale»} Else if(Wind_speed <=72) {display «whole gale»}Else {display «hurricane»} Endif

End

Algorithm

Page 9: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Iteration

Definition : Certain steps that got repeated while a given condition is true.

While loop (variable number of times)

For loop (fixed number of times)

Do loop (variable number of times)

Page 10: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

While Loop

Page 11: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

While Loop Example

Write an algorithm that calculates the sum of a collection of exam scores. (if the class is very large, the instructor may not know the number of students who took the exam). The program should work regardless of the class size (use the SENTINEL).

Page 12: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Exp4 Solution

Start

SENTINEL=-99

Sum=0

Get score

While (score != SENTINEL)

{ sum += score

diplay «  Enter next score ( »Sentinel « to quit )»

Get score

} EndWhile

Display « The sum of scores is » sum

End

Page 13: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

For Loop

Page 14: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

For Loop Example

Compute n!

Precondition: n is greater or equal to 0.

Page 15: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Exp5 Solution

Specification of the problem:

The problem’s objective is to display the factorial of n.

Analysis Input : n. Output: factorial. Relevant formula: factorial = factorial * i

Page 16: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Algorithm

Start

Get n

factorial =1

For(i=n;i>1;i--)

{factorial = factorial * i}

EndFor

Display « The n factorial is » factorial

End

  

Page 17: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Do Loop (Post Loop)

Page 18: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Do Loop Example

Compute n! as long as the user wants.

Precondition: n is greater or equal to 0.

Page 19: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Exp5 Solution

Specification of the problem:

The problem’s objective is to display the factorial of different n as long as the user wishes.

Analysis Input : n, again. Output: factorial. Relevant formula: factorial = factorial * i

Page 20: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Solution

Start

do

{Get n

factorial =1

For(i=n;i>1;i--)

{factorial = factorial * i}

EndFor

Display « The n factorial is » factorial

Display « One more time ? (1 to continue – 0 to quit) »

Get again

} while (again==1)

End

Page 21: Lab 3 rAlternation l Multiple Alternative Decision l Nested If Statements rIteration l While Loop l For Loop l Do Loop Note: rLook at math.h (Appendix.

Common Use of Do Loop

Testing the input

Do { Display « enter n>0 » Get n} while (n<=0)