Download Design an algorithm that will receive two numbers as input

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Law of large numbers wikipedia , lookup

Classical Hamiltonian quaternions wikipedia , lookup

Location arithmetic wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Elementary arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Arithmetic wikipedia , lookup

Addition wikipedia , lookup

Transcript
Design an algorithm that will receive two numbers as input, increment the
first number by 5, double the value of the second one and then display to the
screen their sum and quotient. Note that the quotient calculation (first
integer divided by second integer) is only to be performed if the second
integer is not 0.
Defining diagram
Input
Number_1
Number_2
Processing
Initialize Sum and Quotient to 0
Prompt for 2 numbers
Get 2 numbers
Add 5 to the first number
Double the second number
Calculate sum
Display sum
nd
If 2 value is not zero then
Calculate quotient
Display quotient
OR else Display message
Algorithm
Calculating_Sum_and_Quotient
Set Sum and Quotient to zero
Prompt for two numbers
Get Number_1, Number_2
Sum = Number_1 + Number_2
Display Sum
IF Number_2 NOT= 0 THEN
Quotient = Number_1 / Number_2
Display Quotient
ELSE
Display “Can’t divide by 0”
ENDIF
END
Output
Sum
Quotient or
error message
Trace (hand verification)
You will want to consider more than one case in testing this algorithm. Can you come up with good test
cases? You should at least check the two cases with Number_2 = 0 and Number_2 not=0. By doing so you
make sure you trace through every path possible in the algorithm.
Sample data used for trace # 1: Number_1 = -10, Number_2 = 0
Expected result:
Statement
Initialize
Prompt
Get
Increment
Double
Sum
Display
IF
ELSE
Number_1
Sum = -5, Quotient- Error (div. by 0)
Number_2 Sum
0
Yes
-10
-5
Quotient If, else
statement
executed?
0
Yes
0
0
-5
yes
Error
msg.
No
Yes,
Number_2=0
Sample data used for trace # 2: Number_1 = 25, Number_2 = 3
Expected result:
Statement
Initialize
Prompt
Get
Increment
Double
Sum
Display
IF
Quotient
Display
ELSE
Number_1
Sum = 36, Quotient = 5
Number_2 Sum
0
Yes
25
30
Quotient If, else
statement
executed?
0
Yes
3
6
36
yes
Yes
5
yes
No