Jump to content

Algorithm help.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Guest_patience_*

Guest_patience_*
  • Guests
Algorithm Verification

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

input X

if (0 <= X and X < 49)
output "you fail"

else if (50 <= X and X < 70)
output "your grade is" X
output "you did OK"

else if (70 <= X and X < 85)
output "your grade is" X
output "you did well"

else if (85 <= X and X < 100)
output "your grade is" X
output "you did great"

endif
output "how did you do?"



I am so confused with these questions can someone help me please.


Answer the following question about the information of algorithm verification.

1. What will be printed if the input is 0?
2. What will be printed if the input is 100?
3. What will be printed if the input is 51?
4. What will be printed if the user enters “Wingding”?
5. Is this design robust? If so, explain why. If not, explain what you can do to make it robust.
6. How many levels of nesting are there in this design?
7. Give a set of test values that will cause each of the branches to be executed.
8. Give a set of test values that test the abnormal operation of this program segment.

#2
icepack

icepack

    Programmer

  • Members
  • PipPipPipPip
  • 115 posts
well the input is X, so if X is 0....which one of the statements will be rendered "true" for both cases?

else if (70 <= X and X < 85)

is that true? is 0 greater than or equal to 70 and less than 85?...

think logically and this is very easy

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
It is simple logic that you usually learn in 7th grade. Lets look at some basic truth tables.

Lets assume:
^ is the AND operator
V is the OR operator
T is TRUE
F is FALSE

T ^ T = T
T ^ F = F
F ^ T = F
F ^ F = F

This means that an AND statement can only be true IF and ONLY IF both parts of the statement are true.


T V T = T
T V F = T
F V T = T
F V F = F

This means that an OR statement is true only when both parts of the statement are FALSE.

Algorithms operate in most cases from top to bottom left to right. With that said lets look at your first question.

1) What if X = 0? Well start from the top and evaluate each statement.

Is X >= 0? TRUE
AND
Is X < 49? TRUE

Since both are TRUE, that statement is logically true. By evaluating the rest the same way, you see that all the other statements are not true.

2)
What if X = 100?
0 <= X < 49
T F ---------- FALSE
50 <= X < 70
T F ---------FALSE
70 <= X < 85
T F ---------FALSE
85 <= X < 100
T F -------FALSE (100 is not less than 100)

(T ^ F) = F
Since none of the statements are true, nothing will appear other than maybe "How did you do?"
3)
What if X = 51
0 <= X < 49
T F ---------- FALSE
50 <= X < 70
T T ---------TRUE
70 <= X < 85
F F---------FALSE
85 <= X < 100
F F-------FALSE

4) Repeat the same thing. But you can easily see that Wingding is not in integer (its a string) and will never be greater or equal to another integer.