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

Username:   Password: 
 RegisterRegister   
 please help
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
pokerface




PostPosted: Wed Sep 29, 2004 8:24 pm   Post subject: please help

hello, im making a program at school where we have to roll the die.
i got as far as:

******************************************
var roll : int
var first := 0
var die := 0
var die1, die2, die3, die4, die5, die6 := 0
var answer : string (1)

loop
put "Would you like to roll the die? " ..
getch (answer)

if answer = "y" or answer = "Y" then
put "Yes"
put ""
put ""

randint (die, 1, 6)

case die of
label 1 :
put "You rolled a ", die
die1 := die1 + 1

label 2 :
put "You rolled a ", die
die2 := die2 + 1

label 3 :
put "You rolled a ", die
die3 := die3 + 1

label 4 :
put "You rolled a ", die
die4 := die4 + 1

label 5 :
put "You rolled a ", die
die5 := die5 + 1

label 6 :
put "You rolled a ", die
die6 := die6 + 1

end case
put ""

elsif answer = "n" or answer = "N" then
put "Exiting"
exit
delay (100)
else
put ""
end if

end loop

******************************
i want this program to be able to remember the first roll but continue rolling till the first roll is rolled again? then i want it to say how many rolls it took to get to the first roll!

eg>

(you rolled a 5
you rolled a 1
you rolled a 6
you rolled a 2
you rolled a 3
you rolled a 5

It took you 6 rolls)

i understand that this question is pretty easy but im just learning turing now! so im REALLY new to turing!
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Wed Sep 29, 2004 8:29 pm   Post subject: (No subject)

hmmm since ur new, i wont flame u, but nxt time plz dont put a topic thats more specific... and as for ur problem, just put the rolling part in a loop and have an if statement that exits the loop if the die number has a value of 1
pokerface




PostPosted: Wed Sep 29, 2004 8:35 pm   Post subject: (No subject)

what do u mean put a if statement that exits the loop if the die number has a value of 1. i dont want it to exit i want it to count how many rolls it took before getting the first roll again. could u give me an example then?
wtd




PostPosted: Wed Sep 29, 2004 8:40 pm   Post subject: (No subject)

If I may suggest one improvement...

Use an array to keep track of how many times each side was rolled.

Instead of:

code:
var die1, die2, die3, die4, die5, die6 := 0


Use:

code:
var timesRolled : array 1 .. 6 of int := init(0, 0, 0, 0, 0, 0)


Now:

code:
case die of
   label 1 :
      put "You rolled a ", die
      die1 := die1 + 1
   label 2 :
      put "You rolled a ", die
      die2 := die2 + 1
   label 3 :
      put "You rolled a ", die
      die3 := die3 + 1
   label 4 :
      put "You rolled a ", die
      die4 := die4 + 1
   label 5 :
      put "You rolled a ", die
      die5 := die5 + 1
   label 6 :
      put "You rolled a ", die
      die6 := die6 + 1
end case


Can become:

code:
put "You rolled a ", die
timesRolled(die) += 1


As for keeping track of the first roll... just start a counter outside the loop.

code:
var counter := 1
var firstRolled : int

loop
   % do your normal stuff

   if counter = 1 then
      firstRolled = die
   elsif firstRolled = die then
      % yay!  The same number was rolled again
   end

   counter += 1
end loop
pokerface




PostPosted: Wed Sep 29, 2004 8:46 pm   Post subject: (No subject)

i have to keep it in case construct! is there anything else that would do it while using case construct?
wtd




PostPosted: Wed Sep 29, 2004 8:49 pm   Post subject: (No subject)

pokerface wrote:
i have to keep it in case construct! is there anything else that would do it while using case construct?


The first part of my post was independent of the second part. Your use of six different variables and the case statement will work, but it's way too much work. Smile

My suggestions for the counter and how to detect if the roll is the same as the first roll will still work.
Dan




PostPosted: Wed Sep 29, 2004 8:51 pm   Post subject: (No subject)

[mod:bebab79606="hacker dan"]
Dang, you all posted b4 me and b4 i saw u post anything. Oh well tho my awser is not bad even tho he whonts to case:?
[/mod:bebab79606]

Well 1st thing you should make more meaningfull subject titles for your posts. If you read the things that side read this befor asking for help you whould know not to say "i need help" or "pleas help" as the topic for the post. You are post in turing help so we know u need help Wink

Any how, you are not far off from what you have posted so far. You just need to sotre the 1st dice roll then comper all the others. now to do this we need to know if it is the 1st roll or not so i whould make a boolean var to be ture if it is the 1st roll and flase other wise:

code:

var firstRoll: boolean := true


Then we need to put some ifs in the area that runs when they enter y for whonting to roll agaen. the 1st if will check and see if it is the 1st time they rolled and if it is set the value of first to that roll and then set the value of firstRoll to false. If it is not the firstRoll we then need to check the value that was rolled and see if it was the same as the first roll:

code:

if firstRoll then
first := die
firstRoll := false
else
if first = die then
put "that is the same as the 1st roll"
exit
end if
end if


now to make your code better u could get ride of all that case stuff and replace it with a simple:

code:

put "You rolled a ", die


b/c you do not need to keep track or how many rolls of each die but just how many rolls total.

to do the total rolls you could simpley set roll to 0 to start with then add a line to add one to roll each time you roll after the 1st time, like so:

code:

%%%%%%at top%%%%%%
var roll : int := 0

.......

%%%%%%in the else of if firstRolll%%%%%
roll+=1

........



Also that dealy you have at the end will never be ran since u exit 1st, you may as well get ride of it. Affter all that your code will look somting like this:


code:

var roll : int := 0
var first := 0
var firstRoll : boolean := true
var die := 0
var answer : string (1)

loop
    put "Would you like to roll the die? " ..
    getch (answer)

    if answer = "y" or answer = "Y" then
        put "Yes"
        put ""
        put ""

        randint (die, 1, 6)

        put "You rolled a ", die

        if firstRoll then
            first := die
            firstRoll := false
        else
            roll += 1
            if first = die then
                put "that is the same as the 1st roll"
                exit
            end if
        end if


    elsif answer = "n" or answer = "N" then
        put "Exiting"
        exit
    else
        put ""
    end if

end loop

put "it toke ", roll, " trys"


And if you whont to make it even better you could probly get ride of the boolean var and check the value of roll insted but i think this way is easy to understand
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
wtd




PostPosted: Wed Sep 29, 2004 8:57 pm   Post subject: (No subject)

In your example, the "roll += 1" should be outside the conditional. You'll want the number of rolls to be incremented by one even if it's the first roll.

Also...

code:
put "it toke ", roll, " trys"


code:
put "It took ", roll, " tries."


Wink
Sponsor
Sponsor
Sponsor
sponsor
Dan




PostPosted: Wed Sep 29, 2004 9:04 pm   Post subject: (No subject)

wtd wrote:
In your example, the "roll += 1" should be outside the conditional. You'll want the number of rolls to be incremented by one even if it's the first roll.

Also...

code:
put "it toke ", roll, " trys"


code:
put "It took ", roll, " tries."


Wink


i thought he whonted it the other way? b/c how could u be trying to roll the 1st roll if u have not rolled it yet?
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
pokerface




PostPosted: Wed Sep 29, 2004 9:07 pm   Post subject: (No subject)

how would it work if its case thou? im understanding what u did but i dont want to show the teacher that i did something she hadnt though yet!
eg true, false never learn't that !

sorry i just found out it dont have to be case, it could be an if statement but i still dont want to show her stuff that i never learned before!
wtd




PostPosted: Wed Sep 29, 2004 9:17 pm   Post subject: (No subject)

The five minute version, with (I think) more clear variable names.

code:
var rolls : int := 0
var firstRoll : int

loop
    var thisRoll : int
    var answer : string

    put "Would you like to roll the die? " ..
    getch (answer)

    if answer = "y" or answer = "Y" then
        put "Yes"
        put ""
        put ""

        randint (thisRoll, 1, 6)

        put "You rolled a ", thisRoll

        if rolls = 0 then
            firstRoll := thisRoll
        elsif first = die then
                put "That is the same as the 1st roll"
                put "It took ", rolls, " rolls."
                exit
        end if

        rolls += 1
    elsif answer = "n" or answer = "N" then
        put "Exiting"
        exit
    else
        put ""
    end if
end loop
Dan




PostPosted: Wed Sep 29, 2004 9:18 pm   Post subject: (No subject)

[mod:e4cdd87e1c="hacker dan"]
Could i ever post 1st for once, grrrrr. dang you wtd and your fast posting!!!![/mod:e4cdd87e1c]

pokerface wrote:
how would it work if its case thou? im understanding what u did but i dont want to show the teacher that i did something she hadnt though yet!
eg true, false never learn't that !

sorry i just found out it dont have to be case, it could be an if statement but i still dont want to show her stuff that i never learned before!


But if you do understand it from what we posted you have learned it Wink

Just tell her you did some resherch on your own. There is no reason to write bad code b/c the avg person in your class dose not know how to write good code :p

Any how if u do not like the boolean stuff you could do what wtd side and put the roll += 1 out side and under the if and check to see if roll = 0 rather then checking the boolean var in my method.

Then at the end if u whont the times that were rolled after the 1st dice witch is how i interpited the question you whould just take 1 away from roll.
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
wtd




PostPosted: Wed Sep 29, 2004 9:18 pm   Post subject: (No subject)

pokerface wrote:
how would it work if its case thou?


Then show here that you understand it. That's great. However, using it here doesn't serve any purpose, and part of being a good programmer is knowing when not to use certain techniques.
pokerface




PostPosted: Wed Sep 29, 2004 9:29 pm   Post subject: (No subject)

ty guys/gurls (not sure ur gender) i learned alot. thanks for your time!
rizzix




PostPosted: Wed Sep 29, 2004 9:37 pm   Post subject: (No subject)

pfft we are all girls sheesh.. i thought it was quite obvious Laughing
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 2  [ 22 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: