1 / 13

Lab 7

Lab 7. Alternation Iteration Note: Read All Chapter 4(All Self-Check exercises and all remaining exercises). Multiple Alternative Decision Example1.

leanos
Download Presentation

Lab 7

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 7 • Alternation • Iteration Note: Read All Chapter 4(All Self-Check exercises and all remaining exercises).

  2. Multiple Alternative Decision Example1 Write a C program to compute the tax due based on the Tax Table. Precondition: the salary is defined. Post-condition: Returns the tax due for 0.0<=salary<=150,000.00, and returns -1.0 if salary is outside the table range. Salary Range ($) Base Tax($) % of Excess ----------------------------------------------------------------------------------------- 0.00-14,999.99 0.00 15 15,000.00-29,999.99 2,250.00 18 30,000.00-49,999.99 5,400.00 22 50,000.00-79,999.99 11,000.00 27 80,000.00-150,000.00 21,600.00 33

  3. Multiple Alternative Decision Example2 and 3 • The Air Force has asked you to write a program to label supersonic aircraft as military or civilian. Your program is to be given the plane’s observed speed in km/h and its estimated length in meters. For planes traveling in excess of 1100km/h, you will label those longer than 52 meters « civilian » and shorter aircraft as « military ». For planes at slower speeds, you will issue an « aircraft type unknown » message. • The ph exercise

  4. Multiple Alternative Decision Example4 • Note1 : the else also refers to the last if to which an else was not attributed. • Example 4: Write a C program about invoices. It reads a simple price net of taxes and calculates the price including all taxes corresponding to a constant rate of TVA of 18.6%. it established a handing-over of which the rate depends on the value obtained: • 0% for an amount lower than 100Euro. • 1% for an amount superior or equal to 100Euro and inferior with 200Euro. • 3% for an amount superior or equal to 200Euro and inferior with 500Euro. • 5% for an amount superior or equal to 500Euro.

  5. Switch Statement The switch statement is a multiway conditional statement generalizing the if-else statement. The following is a program extract that shows a typical example of the switch statement: Switch (Value) { case 1: printf(“Good morning\n”); break; case 2: case 3: printf(“Good Evening\n”); break; default: printf(“Good Night\n”); }

  6. Switch Statement Example2 • Note2: the expression of the switch may be of type int or char, but not of type double. • Write a C program to show the expected brightness of a standard light bulb given its wattage. Display Unknown bulb if the watts input is not in the table. Watts Brightness (in Lumens) -------------------------------------------------------- 15 125 25 215 40 500 60 880 75 1000 100 1675 Otherwise -1

  7. Switch Statement Example3 • Note3: the statements following a case label may be one or more C statements, so we don’t need to make multiple statements into a single compound statement using braces. • Remove the break and see the output in case of the absence of break. What is happening ?

  8. Common Programming Errors • If(0<=x<=4) what does it mean ? And how can we check if x is in the range of 0 to 4. • If(x=10) x=10 is an assignment statement. 10 is the value of the condition !!! • If (x>0) sum= sum+x; printf( " Greater than zero\n "); else printf(" Less than zero\n ");

  9. For Statement Definition /While Statement The for statement is like the while. It is used to execute code iteratively. Consider a construction of the form: for(expr1;expr2;expr3) Statement Next statement It is semantically equivalent to expr1; While (expr2) { Statement; expr3; }

  10. Examples • Write a c program that displays a table of angle measures along with sine and cosine values. Assume that the initial and final angle measures (in degrees) are available in init_degree and final_degree (type int variables) and that the change in angle measure between table entries is given by step_degree (also a type int variable). Remember that the math library’s sin and cos functions take arguments that are in radians. • Example2: Write a C program to compute the factorial as long as the user wants.

  11. Examples • Write a nests of loops that cause the following output to be displayed : 0 01 012 01 0 • Write a C program to display a triangle filled with stars as the following : * ** *** **** *****

  12. How to Debug and Test Programs • For(i=0;i<n;i++) { sum+=score; if(DEBUG) printf(“****** score is %d, sum is %d \n”, score, sum); printf(“ Enter next score (%d to quit )>”, SENTINET); scanf(“ %d”,&score); } • What if you want to execute a given statement n times ? for (count =0; count<=n; ++count); what if count is initialized to 1 ?

  13. Notes • Each of the 3 expressions of the for statement is facultative. i=1; for (;i<=5;i++); • C permits to group a lot of actions in one expression: for (i=0, sum=0;i<=5;i++); for (i=0, sum=0;i<=5;printf(“ sum= %.2lf”, sum) ,i++); • Break can be used to got out from the for loop if a given condition is true. • Continue can be used not to execute a given statement in a for loop if a given condition is true .

More Related