Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing Sums and Averages 04/17/15. Totaling Up a List Write a program to input a list of four numbers and output the total.

Similar presentations


Presentation on theme: "Computing Sums and Averages 04/17/15. Totaling Up a List Write a program to input a list of four numbers and output the total."— Presentation transcript:

1 Computing Sums and Averages 04/17/15

2 Totaling Up a List Write a program to input a list of four numbers and output the total.

3 Algorithm 1. Set MAXCOUNT to 4 2.Intialize count to 1, total to 0.0 3. while(count <= MAXCOUNT) A)Input num B)Add num to total C)Output total D)Increment count 4.Output “Final total is of” MAXCOUNT “numbers is” total

4 A First Book of ANSI C, Fourth Edition4 Ensures that any previous value present in the storage locations assigned to the variable total is overwritten and the total starts at a correct value Accumulating statement Computing Sums and Averages Using a while Loop (continued)‏

5 Algorithm -- How to Average? 1. Set MAXCOUNT to 4 2. total to 0.0 3. while(count <= MAXCOUNT) A)Input num B)Add num to total C)Output total 4.Output “Average is” total/MAXCOUNT

6 A First Book of ANSI C, Fourth Edition6 Calculating an average Computing Sums and Averages Using a while Loop (continued)‏

7 Participation Write a program that finds the sum of tips earned on five mornings at Flying-M. Ask for the amount earned each day. Output the sum. How would you find the average?

8 Sentinels

9 A program can be made much more general by removing the restriction that exactly five numbers are to be entered  The user enters the number of values to be entered.  A sentinel can be used. Value to mark end of list. Cannot be a legitimate data value.

10 Tips Program with Sentinel Algorithm? tipsSentinels.c

11 Quiz When We Get Back Quiz Next Time –Lab Quiz on For – loop –Read section 5.4 before then. Read pp.271-307 for Wed., April 7.

12 A First Book of ANSI C, Fourth Edition12 Sentinels

13 Example sentinel.c

14 Continue Here

15 A First Book of ANSI C, Fourth Edition15 Sentinels (continued)‏ One useful sentinel in C is the named constant EOF (End Of File)‏ –The actual value of EOF is compiler-dependent, but it is always assigned a code that is not used by any other character –EOF is defined in stdio.h

16 A First Book of ANSI C, Fourth Edition16 Sentinels (continued)‏

17 A Program on onyx scores.c

18 Three Statements Seen in Loops Won't cover: – break – Continue null

19 The Null Statement A semicolon with nothing preceding it is also a valid statement, called the null statement while(x > 20); Use the null statement where a statement is syntactically required, but no action is needed Be care about accidentally creating a while loop with a null statement.

20 Participation Write a program to input a line of characters. Output the line in all-caps. The toupper( ) function can be used to change a lowercase letter to an uppercase letter. Stop the input when a "." is input. To get a started program: cp ~jjarboe/allCaps.c.


Download ppt "Computing Sums and Averages 04/17/15. Totaling Up a List Write a program to input a list of four numbers and output the total."

Similar presentations


Ads by Google