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

Username:   Password: 
 RegisterRegister   
 var topic
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
nin




PostPosted: Tue Nov 21, 2006 7:20 pm   Post subject: var topic

im workin on a worksheet that my teacher gave me n its due tomorrow. I dont really know what to do on the last 3 assignments. and so far im having difficulties...n this is my code so far:

loop
%Declaration Section
var number : int
var answer : int := 28

%Program Title
locate (1, 34)
put "Guessing Game"

%Program intorduction
locate (3, 1)
put "See if you can guess a number between ten and thirty!"

%User Input
locate (5, 1)
put "Enter a number between 10 and 30:" ..
get number

%Possible guess results
if number >= 29 then
put "You need to come down a little!" ..

elsif number = 28 then
put "Good job!" ..

elsif number >= 10 then
put "Way down there?" ..

end if

end loop


%End of Program


the assignments are the following:
a) add an if structure so that only an input between ten and thirty will be accepted. Test your program. Then input section will repeat until the user enters the correct value.

Add an if structure so that only one of the possible guess will output. Test your program. Save it as IfStructure3.t Print source code and hand it in with this assignment.

b) add a for loop structure so that the program will execute four times. Test your program.

c)Add a for loop structure so that the program will execute four times and exit if the user guesses the correct answer before the fourth time. Test your program.

so how can i do the following assignments... and am i on the right track so far?


[/quote]
Sponsor
Sponsor
Sponsor
sponsor
Expirant




PostPosted: Wed Nov 22, 2006 7:59 am   Post subject: (No subject)

I think you're starting to get on the right track for question a). A couple things you may want to consider though is instead of declaring the answer right at the beginning of the code, use a variable to hold its value to make it easier for you or the teacher to read and possibly allow you to change it to a random number.

And checking if a number is between two numbers should look something like:
code:
if userNum >= lowerBound and userNum <= upperBound then
put "The number is greater than ",intstr(lowerBound)," and greater than ", intstr(upperBound)
end if


Keep at it,
Expirant
rdrake




PostPosted: Wed Nov 22, 2006 8:45 am   Post subject: Re: var topic

nin wrote:
the assignments are the following:
a) add an if structure so that only an input between ten and thirty will be accepted. Test your program. Then input section will repeat until the user enters the correct value.

Add an if structure so that only one of the possible guess will output. Test your program. Save it as IfStructure3.t Print source code and hand it in with this assignment.
Long solution above, but it should work.

nin wrote:
b) add a for loop structure so that the program will execute four times. Test your program.
Honestly not hard. Press F10 and look up the for statement.

nin wrote:
c)Add a for loop structure so that the program will execute four times and exit if the user guesses the correct answer before the fourth time. Test your program.
Use the exact same as above, then research the exit keyword.

nin wrote:
and am i on the right track so far?
You are.
rdrake




PostPosted: Wed Nov 22, 2006 11:47 am   Post subject: Re: var topic

After rereading your post, I noticed you had a wee problem in your code. If you want between the values, then you want to adjust the first condition. Otherwise, adjust the last condition. Regardless, see two posts above for a fix.
nin wrote:
code:
    %Possible guess results
    if number >= 29 then
        put "You need to come down a little!" ..

    elsif number = 28 then
        put "Good job!" ..

    elsif number >= 10 then
        put "Way down there?" ..

    end if
Missing things like this happen when linear algebra, physics, and sleep deprevation come together. Not being allowed to edit posts is also a problem.

Oh, and might I suggest you change the following line so you do not have to hardcode the number in twice.
code:
elsif number = 28 then
As I recall, you created a variable called answer that holds this number already Wink.
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  [ 4 Posts ]
Jump to:   


Style:  
Search: