Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help with square root program
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ILIKEMILK




PostPosted: Wed Apr 27, 2011 12:20 am   Post subject: Help with square root program

What is it you are trying to achieve?
I need to solve the following problem
Write a program that asks the user to enter a positive integer. The program determines whether the square root of the integer entered is an integer. If the square root is an integer, display the value of the square root, otherwise display ?the square root is not an integer?. Your program may NOT use a fractional exponent or the built?in Turing square root (sqrt) function.
What is the problem you are having?
When the square root is an integer, nothing is displayed. When the square root isn't an integer the user is told so. I need to tell the user when the square root is an integer. Please Help.
Turing:


% Square Root Program
% Created By: Matthew Arora
% Created on April 24, 2011

% Variable Declaration
var userNum : int
% The user is told to enter a positive integer
put "Please enter a positive integer. " ..
get userNum

% If the user enters a negative integer there is a runtime error and the program ends
assert userNum > 0
% The for loop begins
for a : 0 .. userNum
    cls
    exit when a * a = userNum 
    put "The square root of ", userNum, " is not an integer. "
end for


Sponsor
Sponsor
Sponsor
sponsor
ILIKEMILK




PostPosted: Wed Apr 27, 2011 12:21 am   Post subject: RE:Help with square root program

If i put a message after the end for, then that emssage and the messae after the exit when statement are both outputted if the square root is not an integer.
Insectoid




PostPosted: Wed Apr 27, 2011 12:58 am   Post subject: RE:Help with square root program

You need to check your program flow. If a*a=userNum, what do you want to happen?

You don't need an exit statement. The for loop will exit itself, by reaching userNum (note that the for loop need only run to userNum div 2). If the for loop exits without finding the square root of userNum, you know that userNum is not a perfect square. If you DO detect a square root (via an if statement) you can output a message and quit the program.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: