Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 7 rAlternation rIteration Note: Read All Chapter 4(All Self-Check exercises and all remaining exercises).

Similar presentations


Presentation on theme: "Lab 7 rAlternation rIteration Note: Read All Chapter 4(All Self-Check exercises and all remaining exercises)."— Presentation transcript:

1 Lab 7 rAlternation rIteration 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 rThe 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. rThe ph exercise

4 Multiple Alternative Decision Example4 rNote1 : the else also refers to the last if to which an else was not attributed. rExample 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: l 0% for an amount lower than 100Euro. l 1% for an amount superior or equal to 100Euro and inferior with 200Euro. l 3% for an amount superior or equal to 200Euro and inferior with 500Euro. l 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 rNote2: the expression of the switch may be of type int or char, but not of type double. rWrite 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 rNote3: 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. rRemove the break and see the output in case of the absence of break. What is happening ?

8 Common Programming Errors rIf(0<=x<=4) what does it mean ? And how can we check if x is in the range of 0 to 4. rIf(x=10) x=10 is an assignment statement. 10 is the value of the condition !!! rIf (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 rWrite 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. rExample2: Write a C program to compute the factorial as long as the user wants.

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

12 How to Debug and Test Programs rFor(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); } rWhat 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 rEach of the 3 expressions of the for statement is facultative. i=1; for (;i<=5;i++); r 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++); rBreak can be used to got out from the for loop if a given condition is true. rContinue can be used not to execute a given statement in a for loop if a given condition is true.


Download ppt "Lab 7 rAlternation rIteration Note: Read All Chapter 4(All Self-Check exercises and all remaining exercises)."

Similar presentations


Ads by Google