none
Need Starting Point For School Assignment

    Question

  • This is in regards to repetitions (do while loops) and this is one of the apps I have to develop. It has one button and one label. I can figure out how much money the guy has in a totally different way but am clueless as to how to use two nested loops to get the answer.

    "A man pays $1 to get into a gambling casino.  He loses half of his money there and then has to pay $1 to leave.  He goes to a second casino, pays another $1 to get in, loses half of his money again and pays another $1 to leave.  Then he goes to a third casino, pays another $1 to get in, loses half of his money again and pays another $1 to leave.  After this, he’s broke.  Write a program to determine the amount of money he began with.  You will need 2 nested loops."
    Monday, November 16, 2009 9:37 PM

Answers

  • since you have a working model, I'll post now what I came up with so you see what I'm talking about


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim casinos As Integer = 3
            
            'single loop
            'Dim StartingCash As Integer = 0
            'Do Until casinos = 0
            '    StartingCash += 1
            '    StartingCash *= 2
            '    StartingCash += 1
            '    casinos -= 1
            'Loop
            'Debug.WriteLine(StartingCash)
    
            'double loop
            Dim myCash As Double = 1
            Dim myStartCash As Double = 0
            Do Until myCash = 0
                myCash = myStartCash + 1
                myStartCash = myCash
                Do Until casinos = 0
                    casinos -= 1
                    myCash = ((myCash - 1) / 2) - 1
                Loop
                casinos = 3
            Loop
            Label1.Text = "Starting cash amount is " & myStartCash.ToString("c")
            
        End Sub
    


    don't forget to mark any Posts as Helpful if they helped, or as Answer if they gave you the answer to your specific question
    Tuesday, November 17, 2009 4:01 PM
  • "A man pays $1 to get into a gambling casino.  He loses half of his money there and then has to pay $1 to leave.  He goes to a second casino, pays another $1 to get in, loses half of his money again and pays another $1 to leave.  Then he goes to a third casino, pays another $1 to get in, loses half of his money again and pays another $1 to leave.  After this, he’s broke.  Write a program to determine the amount of money he began with.  You will need 2 nested loops."

    He finished with $0 after paying $1 to exit the last casino so he would of had $2 when he started gambling in the last casino (half of $2 is $1) so he must of had $3 before he entered the final casio ($1 to enter). Ok so he paid $1 to leave the second casino so thats $4. He lost half his money in the second casino so he would of had $8 and he paid $1 to get in so thats $9 before he entered the second casino. He paid $1 to leave the first casino so that was $10 and he lost half his money there so thats $20 and then $1 to enter the first casino; so he had $21 when he started.

    Without knowing the correct result you'll never know your code will work.... you can do this with one loop.

    dollars = 0
    3.times do
      dollars += 1  # exit fee
      dollars *= 2  # loses half his money
      dollars += 1  # entry fee
    end
    puts dollars


    p.s. yeah I know about helping people doing their homework but I know for a fact that if the original poster hands this in as his assignment the teacher will know they got help.... they'll just know. Also I posted in Ruby and I hope having the answer of 21 will give the original poster a target to aim for.
    Tuesday, November 17, 2009 5:13 PM

All replies

  • As MSDN policy, we are not suppose to do homework in full.  If you post your code that you have so far, then we can fix it for you. Take look at this link for nested loop http://www.java2s.com/Code/VB/Language-Basics/NestedForLoop.htm

    kaymaf
    If that what you want, take it. If not, ignored it and no complain
    Monday, November 16, 2009 10:25 PM
  • I'm not looking for exact code, and at the moment I don't have any. I'm just drawing a blank at where to start it at all. I just finished another one so I'm gonna' take a look at this one again and see if it comes to me.
    Monday, November 16, 2009 10:37 PM
  • This uses two loops, but it is such a strange way to do it that it's not possible to guess whether or not this is what is meant.

        Dim Starting As Single = 10000
        Dim Balance As Single
        Do
            Starting = Starting - 1
            Balance = Starting
            For I As Integer = 1 To 3
                Balance = Balance - 1
                Balance = Balance / 2
                Balance = Balance - 1
            Next I
        Loop Until Balance = 0
        MsgBox(Starting.ToString)
    Monday, November 16, 2009 10:38 PM
  • So this is the code I have so far and it semi-works...  Only problem is that even if it's a fraction like 0.anything it works. I could be over thinking this whole thing...

            Dim Casinos As Integer = 3
            Dim RandomNo As New Random
            Dim MyCash As Integer = 1
            Dim StartCash As Integer
            Do Until MyCash = 0
                MyCash = RandomNo.Next(1, 100)
                StartCash = MyCash
                Do Until Casinos = 0
                    Casinos -= 1
                    MyCash = ((MyCash - 1) \ 2) - 1
                    If Casinos = 0 AndAlso MyCash = 0 Then
                        Me.lblResult.Text = "Starting cash amount is " & StartCash.ToString("c")
                    End If
                Loop
                If Casinos = 0 Then
                    Casinos = 3
                End If
            Loop
    Tuesday, November 17, 2009 12:29 AM
  • Thanks, but For Loops haven't been introduced yet so it wouldn't be used.
    Tuesday, November 17, 2009 12:30 AM
  • You can use the above code but replace the For / Next loop with your own counter so it executes three times exactly.

    Tuesday, November 17, 2009 12:49 AM
  • do you have to use 2 nested loops? You can do this in 1

    What I would do is use the Do Until Casinos as your outside loop.

    Then work backwards. Meaning you know cash needs to end at 0, so use that as your starting point. So in this case StartCash = 0.

    Then use what you know about each trip to each casino, working backwards

    At each casino, working from when he left to when he entered, first add $1 to his balance (this would be equal to when he paid $1 when he left).

    Then double it - because that is the opposite of taking half.

    Then add $1 to it again (this would be equal to when he paid $1 when he entered)

    so write those 3 steps in your Do until Casinos = 0 loop so they are repeated 3 times. The final number of StartCash is what he started with.

    Tuesday, November 17, 2009 1:03 AM
  • do you have to use 2 nested loops? You can do this in 1

    What I would do is use the Do Until Casinos as your outside loop.

    Then work backwards. Meaning you know cash needs to end at 0, so use that as your starting point. So in this case StartCash = 0.

    Then use what you know about each trip to each casino, working backwards

    At each casino, working from when he left to when he entered, first add $1 to his balance (this would be equal to when he paid $1 when he left).

    Then double it - because that is the opposite of taking half.

    Then add $1 to it again (this would be equal to when he paid $1 when he entered)

    so write those 3 steps in your Do until Casinos = 0 loop so they are repeated 3 times. The final number of StartCash is what he started with.

    I think I know what you mean. This was the original solution that I had, working backwards adding instead of subtracting. But unfortunately, I do need to use 2 loops.
    Tuesday, November 17, 2009 1:25 AM
  • Well I guess my original code was correct, I just needed to change the values from integers to doubles and booyah!

            Dim Casinos As Integer = 3
            Dim RandomNo As New Random
            Dim MyCash As Double = 1
            Dim StartCash As Double
            Do Until MyCash = 0
                MyCash = RandomNo.Next(1, 100)
                StartCash = MyCash
                Do Until Casinos = 0
                    Casinos -= 1
                    MyCash = ((MyCash - 1) / 2) - 1
                    If Casinos = 0 AndAlso MyCash = 0.0 Then
                        Me.lblResult.Text = "Starting cash amount is " & StartCash.ToString("c")
                    End If
                Loop
                If Casinos = 0 Then
                    Casinos = 3
                End If
            Loop
    Tuesday, November 17, 2009 2:20 AM
  • good job figuring it out. your code is similar to what I came up with except for a couple of differences...

    but you can shorten it some... there is no need for the If casinos = 0 and myCash = 0 block. The reason is because you are saying do until both of those are 0, so your loop will stop when that condition is met. And you also don't need the If casinos = 0 then casinos = 3. Again it will stop looping when casinos = 0, so at that point just set it back to 3.

    and why use a Random number generator? Go through the numbers systematically, incrementing it by 1 with each loop

    so, keep your variables Casinos, MyCash and StartCash - except set a starting value of StartCash = 0 in the declaration

    Keep your Do Until MyCash = 0

    then instead of using a Random, set MyCash = StartCash + 1 on one line and right below that line set StartCash = MyCash

    so first you are incrementing MyCash by +1, and then keeping track of what that number is by setting it back to StartCash. You need this because you are going to be changing MyCash in the next loop.

    Then comes your Do Until Casinos = 0

    and in that loop you decrement casinos by -1, then do your adjustment to MyCash as you have. But then get rid of the whole If...Then block

    Then after the Loop line just set Casino = 3 (as I explained above, no need to test for it being 0 because you are looping until it is 0)

    After the last Loop (the one for the Do Until MyCash = 0), put the line for the label display.

    I think you'll find the code is shorter and a little neater without both of those If...Then.
    Tuesday, November 17, 2009 6:44 AM
  • good job figuring it out. your code is similar to what I came up with except for a couple of differences...

    ROFL

    By the way, I would first look at the problem before I started to make code.

    Probably I came then with a loop (because it is told that it should be)  for the last casino and then for the first.

    Why a loop in a loop. He did not visit the second casino when he was still in the first.



    Success
    Cor
    Tuesday, November 17, 2009 7:20 AM
  • and why use a Random number generator? Go through the numbers systematically, incrementing it by 1 with each loop
    True, never even thought of that and would be more logical. By the way, now that I come to class, the teacher said that we coulda' just used one loop if we had a way, which is the way you  and I both thought of in the first place. I figured we needed two, making it more difficult than the one, since the question did say "You will need 2 nested loops".
    Tuesday, November 17, 2009 3:02 PM
  • since you have a working model, I'll post now what I came up with so you see what I'm talking about


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim casinos As Integer = 3
            
            'single loop
            'Dim StartingCash As Integer = 0
            'Do Until casinos = 0
            '    StartingCash += 1
            '    StartingCash *= 2
            '    StartingCash += 1
            '    casinos -= 1
            'Loop
            'Debug.WriteLine(StartingCash)
    
            'double loop
            Dim myCash As Double = 1
            Dim myStartCash As Double = 0
            Do Until myCash = 0
                myCash = myStartCash + 1
                myStartCash = myCash
                Do Until casinos = 0
                    casinos -= 1
                    myCash = ((myCash - 1) / 2) - 1
                Loop
                casinos = 3
            Loop
            Label1.Text = "Starting cash amount is " & myStartCash.ToString("c")
            
        End Sub
    


    don't forget to mark any Posts as Helpful if they helped, or as Answer if they gave you the answer to your specific question
    Tuesday, November 17, 2009 4:01 PM
  • I hope they once will give minus points for helping with school appointments with code.


    Sorry that I become a little bit sad that you behave like this.

    It is very unhelpfull  in my opinion.

    For the same sake you could have given it in pseudo.

    I was expecting from you better.





    Success
    Cor
    Tuesday, November 17, 2009 5:01 PM
  • "A man pays $1 to get into a gambling casino.  He loses half of his money there and then has to pay $1 to leave.  He goes to a second casino, pays another $1 to get in, loses half of his money again and pays another $1 to leave.  Then he goes to a third casino, pays another $1 to get in, loses half of his money again and pays another $1 to leave.  After this, he’s broke.  Write a program to determine the amount of money he began with.  You will need 2 nested loops."

    He finished with $0 after paying $1 to exit the last casino so he would of had $2 when he started gambling in the last casino (half of $2 is $1) so he must of had $3 before he entered the final casio ($1 to enter). Ok so he paid $1 to leave the second casino so thats $4. He lost half his money in the second casino so he would of had $8 and he paid $1 to get in so thats $9 before he entered the second casino. He paid $1 to leave the first casino so that was $10 and he lost half his money there so thats $20 and then $1 to enter the first casino; so he had $21 when he started.

    Without knowing the correct result you'll never know your code will work.... you can do this with one loop.

    dollars = 0
    3.times do
      dollars += 1  # exit fee
      dollars *= 2  # loses half his money
      dollars += 1  # entry fee
    end
    puts dollars


    p.s. yeah I know about helping people doing their homework but I know for a fact that if the original poster hands this in as his assignment the teacher will know they got help.... they'll just know. Also I posted in Ruby and I hope having the answer of 21 will give the original poster a target to aim for.
    Tuesday, November 17, 2009 5:13 PM
  • I hope they once will give minus points for helping with school appointments with code.


    Sorry that I become a little bit sad that you behave like this.

    It is very unhelpfull  in my opinion.

    For the same sake you could have given it in pseudo.

    I was expecting from you better.





    Success
    Cor

    Hi Cor, yeah I hear you and I'm not taking any sides here but it's really difficult to not help. It's a difficult one here as this sort of thing is like pair programming. The original poster is obviously trying. It's a difficult one Cor but nothing terribe happened here, I think the original poster has learned from this experience and Jwavila's help. It's pair programming man and all the advantages that come from it (ok lets say it's a kind of pair programming).
    Tuesday, November 17, 2009 5:26 PM
  • How did you know he ended with 0, I've read he was broke, could be less then zero


    Success
    Cor
    Tuesday, November 17, 2009 5:26 PM
  • :)

    Your right Cor I don't have that information... but when I have $0 then I'm usually broke. Without a definition of broke there is no way to answer the riddle. 0 seems reasonable definition for broke; he can not enter any other casinos and needs to go home to explain things to the wife.

    Broke may refer to:

    • To being currently (but not necessarily permanently) out of money

    $0 is out of money.... http://en.wikipedia.org/wiki/Broke

    The specification hasn't been defined correctly :) Take it easy Cor.

    Tuesday, November 17, 2009 5:34 PM
  • Well, as you can see I didn't post any code at first - just lent some guidance.

    Then once the OP posted some code that worked, I posted an explanation of how it could be streamlined with an example of what I was talking about. And in cases like this if I know it is homework, I don't post code until they post some mostly functioning code.

    So in my opinion I didn't do his homework for him - he came up with functioning code. And as Derek said, he/she is obviously trying.

    And yes, we are assuming broke = 0. But it can't be negative because once he gets to 0, the next casino won't let him in or allow him to keep gambling. So, again in my opinion, 0 is a good definition of broke in this case.
    Tuesday, November 17, 2009 6:42 PM
  • "A man pays $1 to get into a gambling casino.  He loses half of his money there and then has to pay $1 to leave.  He goes to a second casino, pays another $1 to get in, loses half of his money again and pays another $1 to leave.  Then he goes to a third casino, pays another $1 to get in, loses half of his money again and pays another $1 to leave.  After this, he’s broke.  Write a program to determine the amount of money he began with.  You will need 2 nested loops."

    He finished with $0 after paying $1 to exit the last casino so he would of had $2 when he started gambling in the last casino (half of $2 is $1) so he must of had $3 before he entered the final casio ($1 to enter). Ok so he paid $1 to leave the second casino so thats $4. He lost half his money in the second casino so he would of had $8 and he paid $1 to get in so thats $9 before he entered the second casino. He paid $1 to leave the first casino so that was $10 and he lost half his money there so thats $20 and then $1 to enter the first casino; so he had $21 when he started.

    Without knowing the correct result you'll never know your code will work.... you can do this with one loop.

    dollars = 0
    3.times do
      dollars += 1  # exit fee
      dollars *= 2  # loses half his money
      dollars += 1  # entry fee
    end
    puts dollars


    p.s. yeah I know about helping people doing their homework but I know for a fact that if the original poster hands this in as his assignment the teacher will know they got help.... they'll just know. Also I posted in Ruby and I hope having the answer of 21 will give the original poster a target to aim for.
    This is the way I seen it from the beginning but was under the impression that two nested loops had to be used. So what I came up with worked and resulted in $21.00 but would have been much easier this way. 

    I appreciate the help from everyone, and yes, I did try and came up with a result on my own before given any real guidance. It's also nice to see other people's way of thinking.
    Tuesday, November 17, 2009 7:40 PM
  • Just realised Jwavila you posted the algorithm in an earlier post.

    Yeah guys nothing bad happened here; it was all good.

    Couple of points came from it:

     Try and understand the problem and any potential solutions before coding; very important that.

     Sometimes you make assumptions about the requirements (being broke) that may result in getting things wrong.

     Getting things wrong is good; celebrate, fix and learn from it.

     Sometimes the requirements say things that throw you off track a little, narrow your thought process.

     Don't hand in pro-developer code for your homework because your teacher is not daft! 
     In fact your teacher would rather you tried and got it wrong rather than cheat... see above, wrong is good.

    and

     Be excellent to each other.

    See you hoopy froods around.

    Tuesday, November 17, 2009 7:54 PM