Tech Support > Computers & Technology > Programming > square root sqrt algorithm and flowchart
square root sqrt algorithm and flowchart
Posted by popeyerayaz on July 8th, 2008


I am a complete beginner and have the following instructions:

Develop a algorithm and flowchart that enables a user to choose one of
the following types of calculations to
perform on an input number.

Calculate the square of the number
Calculate the cube of the number
Round the number to two decimal places
Calculate the square root of the number

I only had to choose one and chose the "Calculate the square root of
the number".
I have the following algorithm

Algorithm

1. Input: a real number X
sqrt(x) = x^0.5
2. If (X < 0) Then
Output: X “ cannot be negative.”
STOP
Edif

My flowchart is on the following web page:
http://www.popeyeray.com/lesson6.htm

I would appreciate someone to check if this is mostly correct.
I also am not sure about the error message and how to put it into my
flowchart.

Thank you
Ray

Posted by Bartc on July 8th, 2008


popeyerayaz wrote:
You might try testing for negativity before calculating the square root
(swap steps 2 and 3 of your flowchart).

--
Bartc



Posted by Jens Thoms Toerring on July 8th, 2008


popeyerayaz <popeyerayaz@gmail.com> wrote:
What does that mean? The specification clearly state that the
user (and not the programmer) should be able to choose one of
four operations to be performed...

Can you rely on that? What do you do if the user enters something
that is not a number (or what measures do you take to keep him/her
from doing that)? Other things one could take into account is the
case that the user tries to input a number that's too large to be
represented by the available floating point data type. You probably
will get bonus points if you can come up with a way to output the
correct result if the square root of that number can still be re-
presented;-)

Looks like you got it the wrong way round. You first have to check
if the operation can be performed (and bail out if it can't) before
you try to do the operation.

And where's the part about checking if the value supplied by the
user is non-negative?
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de


Similar Posts