Algorithm Design
Plan Solution
Problems:
1. Write a program to get
sum of any two numbers.
BEGIN
1. Prompt user entering two numbers.
2. Take users numbers into memory boxes.
3. Add these two numbers and store the
sum into another box.
4. Print the result.
END
2. Develop a set of algorithms to read in two whole numbers and
find the quotient of the first number
dividing by the second numbers.
BEGIN
Reserve memry boxes.
(Input: two numbers, output:
quotient)
Write a user prompt
to
enter two whole numbers.
Store user's numbers into two
memory boxes.
Check whether the second number is
zero.
If yes,
output an error message and stop program.
If no,
divide two numbers each other and store result in a memory box.
Print result.
END
3. Develop a set of algorithms to read in an employee's
total weekly hours worked and rate of pay. Determine
the gross weekly pay using "time-and
-a-half" for anything over
40 hours.
BEGIN
Write a program description message to the user.
Write a user prompt to enter rate of pay (Rate).
Take user input into "Rate" box.
Write a user prompt to enter weekly hours worked (Hours).
Take user input into "Hours" box.
if Hours <=
40 then
Calculate Gross =
Hours
×
Rate.
else
Calculate Gross = (40 ×
Rate)
+ [1.5 × (Hours – 40) × Rate].
Print the information of hours worked, rate of pay, and
gross
pay (Hours, Rate, Gross).
END.
4. Develop a set of algorithms that will allow the entry of three
integer coefficients of a quadratic equation and generate
the solutions of the equation.
Provide for an error message if complex roots exist.
Initial Algorithm
main()
BEGIN
Obtain the equation coefficients from the user and check for A=0.
Calculate the roots of the equation and check for complex roots.
Display the solutions of the equation.
END.
First Level of Refinement
GetData()
BEGIN
Write a program description message to the user.
Write a prompt to enter the coefficient of the squared term.
Read(A).
if A is 0 then
Write an error message, since division by zero is impossible.
else
Write a prompt to enter the coefficient of the first order
term.
Read(B).
Write a promp to enter the constant term.
Read ( C).
END.
Solutions()
BEGIN
Calculate D = B2 – 4AB.
if D < 0 then
Write a message that the equation
has complex roots.
else
Solution1 = [–B + (B2
– 4AC)0.5] / (2A)
Solution2 = [–B – (B2
– 4AC)0.5] / (2A) .
END.
DisplayResults()
BEGIN
Write (Solution1 and Solution2).
END.
|