MIN and MAX programs Flashcards
what’s the difference between using a FOR loop and a WHILE loop for an algorithm to find the highest or lowest value
- FOR loop: will iterate or loop a set number of times
- WHILE loop: uses a condition statement (true or false) to find if the final index value is reached
what is the FOR loop algorithm to find the lowest value from a array list
ages <– [99, 15, 25, 55, 61]
min <–100
FOR index <– 1 TO 5
IF ages[index] < min
THEN
min <– age[index]
ENDIF
NEXT index
OUTPUT “The lowest age is “, min
what is the WHILE loop algorithm that finds the lowest value in an array
ages <– [99, 15, 25, 55, 61]
min <– 100
count <– 1
WHILE count <= LENGTH(ages) DO
IF ages[count] < min
THEN
min <–age[index]
ENDIF
count = count + 1
END WHILE
OUTPUT “The lowest age is “, min //output the lowest age.
FOR loop algorithm to find the highest value from a array list
ages <– [99, 15, 25, 55, 61]
max <– 0
FOR index <– 1 TO 5
IF ages[index] > max
THEN
max <– age[index]
ENDIF
NEXT index
OUTPUT “The highest age is “, max
WHILE loop algorithm that finds the highest value in an array
ages <– [99, 15, 25, 55, 61]
max <– 0
count <– 1
WHILE count <= LENGTH(ages) DO
IF ages[count] > max
THEN
max <– ages[index]
ENDIF
count = count + 1
ENDWHILE
OUPUT “ The highest age is”, max