# Beginner square root question

Discussion in 'Java' started by Stefan Ram, Jul 8, 2008.

1. ### Stefan RamGuest

Ray Leon <> writes:
>I have the following algorithm
>I would appreciate someone to check if this is mostly correct.

To check whether it is correct, one needs to
compare its actual behavior with the specification
for its required behavior.

Without a specification, it is »not even wrong«.

Also, this is a Java newsgroup, but your post does
not refer to Java.

Stefan Ram, Jul 8, 2008

2. ### Ray LeonGuest

I have the following algorithm

Algorithm

Sqrt

1. Input: a real number X
If (X < 0) Then
Display: X ³ cannot be negative.²
STOP
Else
sqrt(X) = X^0.5
Print ³The square root of X is² sqrt(X)

2. Exit

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

Ray Leon, Jul 8, 2008

3. ### Mark SpaceGuest

Stefan Ram wrote:

> To check whether it is correct, one needs to
> compare its actual behavior with the specification
> for its required behavior.

To the OP:

To expand on this a bit, what is your algorithm supposed to do when X is
negative? Obviously, you display "X cannot be negative" and stop, but
was that the required behavior? What if in the case of negative number
input the algorithm was really supposed to make X positive, take the
square root, then display "Ri" to signify an imaginary number? (R =
square root.)

So, your algorithm is probably correct, but you do need to get the idea
down of a specification. Please make sure to tell us first what the
algorithm is supposed to do, then give us an implementation to check.

It's like those problems in geometry where you're "Given X, Y, Z, show
that A is true." Then show the work. You've just shown us the work, we
aren't sure what you were given to do.

And it would be nice if you included Java in there somehow.

Mark Space, Jul 8, 2008