while-continue. problem: write a program that calculates the sum of a collection of exam scores....

19
While-continue

Upload: allyson-armstrong

Post on 28-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

While-continue

Page 2: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

Problem: Write a program that calculates the sum of a

collection of exam scores. The program should work regardless of class size, hence the program must use the sentinel value to exit.

Analyze: Input – exam score Output – sum of exam scores Formula - sentinel loop i. initialize sum to zero ii. get the first score iii. while score is not the sentinel iv. add score to sum v. get next score

Page 3: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

Incorrect formula for sentinel…WHY ??????◦Initialize sum to zero◦While score is not the sentinel◦Get score◦Add score to sum

2 problems: 1. with no initializing input statement, there

will be no value for score that is used to judge the loop repetition condition when it is first tested.

Page 4: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

2. the last statement , ◦ add score to sum, telling that data value is copied

into score and added to the accumulating sum.◦ On the last iteration, the attempt to get another

score obtains the sentinel value. This fact will not cause the loop to exit until

the loop repetition condition is tested again. Before exit occurs, the sentinel is added to

sum. THIS IS NOT RIGHT!!!!! Hence, it is important to set up sentinel-

controlled loops using the first method.

Page 5: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

In the first method; there are :◦ One input to get the loop going-the initialization

input◦ A second one is to keep it going – the updating

input.

Page 6: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

Sample dialogue when user enter scores 55,33 and 77:

Enter first score or -99 to quit : 55Enter next score or -99 to quit:33Enter next score or -99 to quit : 77Enter next score or -99 to quit:-99

Sum of exam scores is 165

Page 7: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

Begin initialize sum to zeroget the first score while score is not the sentinel add score to sum get next scoreWhen score equal to sentinel Display sum End

Page 8: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence
Page 9: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

Shirts are on sale for RM 4 each if more than two are purchased and RM 6 each otherwise. Write a program that able to read in an input number of shirts purchased and display the total cost. Your program must run 5 times before terminate. Upon termination, the total sales in RM must be displayed on screen. Write an appropriate pseudocode for the problem above.

Page 10: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

Analyze:◦ Input – no. shirts◦ Output- total cost after 5 times running the

program◦ Formula – if buy 2, each RM 6◦ if buy more than 2, each RM 4 ◦ Use loop to make the program run 5 times.

Page 11: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

Begin set c=1 set tcost=0 while c < 6 input number of shirt purchased, n if n>2 tcost=n*4 else tcost=n*6 c=c+1 print “the total cost of shirt is RM :’,tcostend

Page 12: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

Design an algorithm and the corresponding flowchart for adding the test scores as given below:

26, 49, 98, 87, 62, 75

Page 13: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

1. Start2. Sum = 03. Get the first testscore4. Add first testscore to sum5. Get the second testscore6. Add to sum7. Get the third testscore8. Add to sum9. Get the Forth testscore10. Add to sum11. Get the fifth testscore12. Add to sum13. Get the sixth testscore14. Add to sum15. Output the sum16. Stop

Page 14: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence
Page 15: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence
Page 16: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

An employee earns RM 5 per hour for the first 30 hours and RM 10 per hour for each hour over 30. Write a program that able to compute an employee’s weekly wage.

Your program will terminate once user enter a character ‘e’.

Page 17: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

Do:1. Pseudocode2. flowchart

Page 18: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

Keith’s Sheet Music needs a program to implement its music teacher’s discount policy. The program is to prompt the user to enter the purchase total to indicate whether the purchaser is a teacher. This program will keep running until the user enter the sentinel value i.e. -1 to terminate the program. Assumed that the manager of the store need to know the number of teachers and non teacher that buy from the store. Your program must be able to determine this.

Music teachers receive a 10% discount on their sheet music purchases unless the purchase total is RM100 or higher. In that case, the discount is 12%. The discount calculation occurs before addition of the 5% sales tax.

The sample outputs are as below: two output , 1 for teacher another one is not teacher.

Page 19: While-continue.  Problem: Write a program that calculates the sum of a collection of exam scores. The program should work regardless of class size, hence

Total purchases RM122.00Teacher’s discount RM 14.64Discounted total RM 107.36Sales tax RM 5.37Total RM 112.73

Total purchases RM 24.90Sales tax RM 1.25Total RM 26.15

Number of teacher = 1Number of non teacher =1

What you need to do?1.Analyze the problem- Identify input-Identify output-Identify formula2. Write a pseudo code3.Draw a flowchart