Associate Program Material

Associate Program Material

Appendix J

Algorithm Verification

Consider the following selection statement, where testScore is an integer test score between 0
and 100.

declare testScore as Integer; values 0-100
input testScore
if (0 <= testScore and testScore < 49)
output “You fail.”
else if (50 <= testScore and testScore < 70)
output “Your grade is” testScore
output “You did OK.”

else if (70 <= testScore and testScore < 85)
output “Your grade is” testScore
output “You did well.”
else if (85 <= testScore and testScore < 100)
output “Your grade is” testScore
output “You did great.”
endif
output “How did you do?”

Answer the following questions:

• What will be printed if the input is 20?
“You Fail.” Will be printed because:
if (0 <= testScore and testScore < 49)
output “You fail.”
• What will be printed if the input is 100?
“How did you do?” will be printed.

• What will be printed if the input is 73?
“Your grade is” 73 “You did well.” Will be printed because:
else if (70 <= testScore and testScore < 85)
output “Your grade is” testScore
output “You did well.”

• What will be printed if the user enters “score”?
“score” Will cause an error. “How did you do?” could be printed again

• Is this design robust? If so, explain why. If not, explain what you can do to make it robust.
This design is not robust because there is no statement for error or exception handling. To
make it more robust I would add a statement like the user can only enter integers from 0
to 100. If the user enters other numbers or data types they will get an error or an invalid
message such as like “Invalid entry. Enter a number from 0 to 100” It also doesn’t handle the
case of entering 49. It should be: else if (49 <= testScore and testScore < 70), because as it is
now, when 49 is entered all it will say is “How did you do?” The same thing will happen when
entering 100, else if (85 <= testScore and testScore <= 100) would fix this problem.

How many levels of nesting are there in this design?

Appendix J
IT/210 Version 5

2

There is no nesting.
• Provide a set of values that will test the normal operation of this program segment. Defend
your choices.
Any number from 0 to 100 will test the normal operation of this program because the algorithm
only covers that code.

• Provide a set of test values that test the abnormal operation of this program segment.
Test values that will test abnormal operation would be:
Negative values such as “-20” or any number that is < 0
A numerical value that is > 100 such as “120” or
String values such as “score”
Any value that is not between 0 through 100 will check the abnormal operation of this program
segment.

Leave a Reply