1 / 21

Lab 3

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). Alternation.

elindgren
Télécharger la présentation

Lab 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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).

  2. 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”.

  3. 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.

  4. Algorithm: 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

  5. 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

  6. 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

  7. 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.

  8. Algorithm 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

  9. 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)

  10. While Loop

  11. 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).

  12. 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

  13. For Loop

  14. For Loop Example • Compute n! • Precondition: n is greater or equal to 0.

  15. 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

  16. Algorithm Start • Get n • factorial =1 • For(i=n;i>1;i--) {factorial = factorial * i} EndFor • Display « The n factorial is » factorial End

  17. Do Loop (Post Loop)

  18. Do Loop Example • Compute n! as long as the user wants. • Precondition: n is greater or equal to 0.

  19. 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

  20. 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

  21. Common Use of Do Loop • Testing the input • Do { • Display « enter n>0 » • Get n } while (n<=0)

More Related