Examples/Patterns Some general patterns for calculating a running total from user input are as follows.. Each of these p
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Examples/Patterns Some general patterns for calculating a running total from user input are as follows.. Each of these p
Examples/Patterns
Some general patterns for calculating a running total from user input are as follows.
Each of these patterns has a different purpose and usage.
Each algorithm pattern is described below using pseudocode.
A. Sentinel
SENTINEL
total = 0
-1
get value
while value != SENTINEL
total = total + value
get value
display total
You would want to use a sentinel when:
• you don't know how many values there will be beforehand;
• there is some obvious invalid value vou can use to be the sentinel and mark the end.
Note that you can use a range of invalid values, e.g., all negative numbers.
E.g., calculating the average age of a line of people, where you don't know how many there are beforehand (you can use -1 for the sentinel).
B. Ask-the-user-to-auit
Examples/Patterns Some general patterns for calculating a running total from user input are as follows.. Each of these patterns has a different purpose and usage. Each algorithm pattern is described below using pseudocode. A. Sentinel to search SENTINEL=-1 total = 0 get value while value 1- SENTINEL total total value get value display total You would want to use a sentinel when • you don't know how many values there will be beforehand; • there is some obvious invalid value you can use to be the sentinel and mark the end. Note that you can use a range of invalid values, e.g, all negative numbers. Eg, calculating the average age of a line of people, where you don't know how many there are beforehand (you can use 1 for the sentinel) B. Ask the-user-to-quit O Bi