Repetition Structures: Maximum and Minimum

advertisement
LOOPING/REPITITION STRUCTURES
How do you know which loop to use:




If any of the following keywords or phrases are used in the problem, then use the while
construct; dummy value, terminated by, terminates, continue as long as.
If it is necessary to input several entries for the same attribute and an actual number is stated,
then use a For construct.
If the number of entries is implied, or the user has to input a value which determines the number
of entries, then use a FOR construct.
If a table is required than use a FOR construct.
Method for developing the While loop







Determine whether some instructions need to be used for initializing variables. For example
when counting, totaling, or finding the maximum and minimum. Write these instructions first.
Write the prompt and read instructions
Place a WHILE statement immediately following the read statement
Set the condition for the while statement based on the dummy value that was supplied and a
variable that was read. Select the appropriate operator to complete the condition. Complete
with the word DO.
Write the instructions that are to be repeated.
Close the loop with the ENDWHILE statement; make sure that a prompt and read statement is
placed just before the ENDWHILE.
Write the remaining instructions that are no to be repeated.
Method for developing the FOR loop

Determine whether some instructions need to be used for initializing variables. For example
when counting, totaling, finding the maximum or minimum. Write these instructions first.

Write the FOR instruction

Write the instructions that are to be repeated

Close the loop with the endfor statement

Write the remaining instructions that are not to be repeated.
Repetition Structures: Maximum and Minimum
Students should be able to:

Understand how to find the highest and lowest number in a series of values using the
for or the while loop
When finding the maximum value from a series of positive values, a loop must be used. A variable
called max should be initialized and set to a low value which is lower than the values being entered. The
initial value of the variable could be set to 0. A comparison must be made between the max variable and
the input variable. If the input value is greater than the max variable, this value is assigned to the max
variable.
Ezamples
1. Find the largest of a series of integers terminated by 0.
Algorithm Maximum
{This algorithm finds the largest integer from a series of integers}
Max
integer
Num
integer
Start
Max
0
Print ’Enter an integer’
Read Num
While Num<>0 Do
If Num>Max then
Max
Endif
Print ’Enter an integer’
Num
Read Num
Endwhile
Print ‘Largest No’, Max
Stop.
If other information is required about the maximum, then another variable must be used to store
that information. This variable must be assigned the required information from the correct input
variable. The assignment statement for performing this task must be within the selection system
which determines the maximum.
2. Write an algorithm to input the name and age of 30 students. Determine the name of the oldest
student and output it.
Algorithm Age
{This algorithm determines the name of the oldest student}
Max integer
A
integer
Name string
Age integer
Oldname string
Start
max
0
For A
1 to 30 Do
Print ‘Enter the name and age of a student’
Read name, age
If age > max then
Max
age
Oldname
name
Endif
Endfor
Print ‘ name of the oldest student’, oldname
Stop.
When finding the minimum value from a series of positive values, a loop must be used. A loop
called Min should be initialized and set to a high value which is higher than the values being
entered. It could be set to 99999999. A comparison must be made between the Min variable
and the input variable. If the input value is smaller than the Min variable, this value is assigned
to the Min variable.
1. Find the smallest of a series of integers terminated by 0.
Algorithm smallest
{This algorithm finds the smallest integer in a series of integers}
Min
Num
Start
integer
integer
Min
9999999
Print ‘Enter an integer’
Read Num
While num <> 0 Do
If Num <Min then
Min
Num
Endif
Print ’Enter an integer’
Read Num
Endwhile
Print ‘Smallest Number’, MIN
Stop.
If other information is required about the minimum, then another variable must be used to store
that information. This variable must be assigned the required information from the correct input
variable. The assignment statement for performing this task must be within the selection system
which determines the minimum.
2. Write an algorithm to input the name and age of 30 students. Determine the name of the
youngest student and output it.
Algorithm Young
{This algorithms finds and outputs the name of the youngest student in a class}
Min
A
name
age
yname
integer
integer
string
integer
string
Start
Min
999
For A
1 to 30 do
Print ‘Enter the name and age of a student’
Read name, age
If age < min then
Min
age
yname
name
Endif
Endfor
Print ’Name of the youngest student’, yname
Stop.
Repetition Structures: Developing tables using the FOR and the While LOOP
Students should be able to:

Write algorithms to develop tables using the FOR and the while loop
Content
A table is a list of information that is displayed in columns and rows. Each column may have a title which
describes the information in the column. Tables are developed using the FOR loop and the While loop.
Data entry that is needed to generate the table must be read prior to printing the column titles and
column titles must be printed before starting the FOR loop. This is necessary to avoid having the data
entry or column titles printed within the table information. Column titles and table information can be
printed in columns by using the comma to separate each column. The loop variable represents the first
column to be printed and all calculations are based on that variable.
Examples.
1. Print a table of numbers that runs from 1 to 10 and the corresponding square for each number.
Solution
Algorithm table
{This algorithm prints a table of numbers and their corresponding squares}
Num
integer
Sqr
integer
Start
Print ‘Number’,’ Square’
For Num
Sqr
1 to 10 Do
Num *Num
Print Num, Sqr
Endfor
Stop.
2. Print a table that converts yards to feet and inches. The table runs from 10 yards to 100 yards in
steps of 5 yards.
How can we produce this table? Can we use the FOR loop as in the example above. Unfortunately no,
since the Pascal for loop increments the loop variable by 1 only, as seen in all previous examples from
lessons 7 to 8. For this table where the increment is 5 we use a while loop and increment the yards by 5
inside the loop. The algorithm solution is below;
Solution
Algorithm table
{This algorithm develops a table that converts yards to feet and inches}
yard
feet
inch
integer
integer
integer
Start
Print ‘yards’, ’feet’, ’Inches’
yard
10
While yard < = 100 do
feet
yard *3
inch
yard* 36
Print yard,feet,inch
yard
yard +10
Endwhile
Stop.
Exercises
1. Write a structured algorithm to read the name and population of a number of countries in
the Caribbean terminated by 0. Print the name of the country with the highest population.
2. Write a structured algorithm to input the name and weight of a number of students in a
class terminated by ‘END’. Print the name and weight of the lightest student as well as the
average weight of the class.
3. Write the pseudocode algorithm that requests the user to read the scores for 30 students in
a class. It should find the highest score and output it with a suitable label.
4. Write a structured algorithm that reads the temperature for each day in a month
terminated by 999. Find the average temperature and the lowest temperature and print
them with suitable labels.
5. Write a structured algorithm that prompts the user to input the name and amount of money
collected by each student in a class of twenty. Find the name of the student who collected
the most money and output it together with 10% of the amount the student collected.
6. Write an algorithm to enter 10 numbers and output the highest and lowest numbers
appropriately labeled.
7. Write an algorithm that read the result of 10 games played by a team and finds the
percentage of games won by the team. Output the percentage of games won.
8. A school has a house system implemented. Points are awarded to a house based on the
performance of its members;
1st place 4points
2nd place
3rd place
4th place
5th place
3points
2points
1point
no points
For ten members of the Aripo house read the place they came in the events; calculate and print
the total awarded to the house.
9. Write an algorithm which finds the total of the numbers 10 to 25 and print the total.
10. Write a structured algorithm that prompts the user to read the amount of rainfall for each
day of the month of November. Print the total rainfall for the month.
11. Write a structured algorithm to read the amount of rainfall for each month of a year. Print
the total rainfall for the year and the monthly average appropriately labeled.
12. Write a structured algorithm to read a positive integer, which is stored in N, followed by N
Numbers. Print the total and the average.
13. Write a structured algorithm that prompts the user to input the number of passengers who
travelled for each day in January on a minivan. Each passenger paid a fare of $1.25. Print the
daily and the monthly revenue with suitable labels.
14. A day could be sunny, rainy or overcast. Write a structured algorithm to read the weather
condition for each day in December. Output the number of days for each weather condition.
15. A minibus charges $1.50 per passenger. Write a structured algorithm to input the number of
passengers for each day terminated by 0. Calculate the daily revenue. And output the
number of passengers for each day and the daily revenue.
16. Write a structured algorithm to read the scores for a number of batsmen who played in a
cricket match and the number of extras made by the team. Calculate and output the total
runs made by the team. The data is terminated by entering 999.
17. Write a structured algorithm that prompts the user to input the amount of rainfall over a
period of days. Calculate and print the average rainfall. The data is terminated by 9999.
18. A school has two sets A and B. Write a structured algorithm to read a set and the points
awarded for winning each race in an athletic final terminated by C. calculate and print the
total points gained by each set appropriately labeled.
19. Data entry for a number of accounts is terminated if the amount of money being entered
exceeds $1000000. Write a structured algorithm to input the amount for each account and
output the total.
1. Print the conversion table from Barbados currency to US currency. The table ranges from
$20BDS to $200BDS In steps of $5 BDS. ( $2BDS = $1US)
2. Print a conversion table from centigrade to Fahrenheit. The table ranges from 10°C to 50°C. (°F=
32 + (9*°C)/5)
3. Print a conversion table from yards to metres. The table ranges from N yards to M yards in steps
of P yards (1yd =.91M)
4. Print a conversion table from miles to km. the table ranges from 1 mile to 25 miles. (1mile
=1.61Km).
5. Print a conversion table from pounds to kg. The table ranges from 1lb to X lbs (1lb =.45kg)
6. Print a table that shows 3% and 5% of numbers ranging from 100 to 500 in steps of 10.
7. Write an algorithm to output the sum of all the even numbers in the range 2 to 40.
8. Write an algorithm to produce a two times table from 1 to 12.
Download