Google Answers Logo
View Question
 
Q: Qbasic Problem ( Answered,   3 Comments )
Question  
Subject: Qbasic Problem
Category: Computers > Programming
Asked by: mrfony-ga
List Price: $2.00
Posted: 17 Sep 2002 17:10 PDT
Expires: 17 Oct 2002 17:10 PDT
Question ID: 66191
What is logically wrong with the following program?
INPUT D, E
D = E / F
Print "The answer is" ; D 
End
Answer  
Subject: Re: Qbasic Problem
Answered By: coldfusion-ga on 17 Sep 2002 18:11 PDT
 
Hello!
The simple answer to this question is that you are trying to divide by
zero.

Since you are attempting to divide E by F, qbasic wonders 'Where is
this F you're talking about?!'. Not finding it, it assigns a value of
0 to it.

Thus, the program you gave will show as:

INPUT D, E
D = E / 0
Print "The answer is" ; D

I believe what you meant to do was:

INPUT D, E
F = D / E
Print "The answer is" ; F

You may wish to do some simple syntax checking as well to make sure
the USER doesn't enter a 0.
With syntax checking the syntax would be:

INPUT D, E
if e > 0 then
  f = d / e
  print "The answer is "; F
else
  print "You cannot divide by 0!"
end if

Thanks!
Todd

Clarification of Answer by coldfusion-ga on 17 Sep 2002 18:13 PDT
Oops the line that says:
if e > 0
should read:
if e <> 0 
Assuming you want to allow division by negative numbers.
Thanks

Clarification of Answer by coldfusion-ga on 17 Sep 2002 18:14 PDT
And just to summarize the answer to the exact question you posted:
It is logically wrong to try to divide by a variable that does not
exist, which causes division by 0.
Comments  
Subject: Re: Qbasic Problem
From: pinkfreud-ga on 17 Sep 2002 17:17 PDT
 
If F is not defined, then isn't this the same thing as dividing by zero?
Subject: Re: Qbasic Problem
From: sherlockh-ga on 17 Sep 2002 18:05 PDT
 
The program asks for the user to input variables d and e,
but then divides e by f (when f hasn't been defined, so will give a
'division by zero error')

Also, the program could also tell the user what it is doing, ie. the
program says "this answer is ', but it doesn't tell the user what the
calculation was, eg. it could say:

INPUT D,E
ANSWER=D/E
PRINT D;" divided by ";e;" is ";answer
Subject: Re: Qbasic Problem
From: sherlockh-ga on 17 Sep 2002 18:12 PDT
 
So what it is doing which is logically wrong is:
In it's calculation it is using a variable that has not been defined (f)

ie. f has not been set to a value before e is divided by f

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy