Results 1 to 3 of 3

Thread: [RESOLVED] Can someone help me!!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2011
    Posts
    17

    Resolved [RESOLVED] Can someone help me!!

    I have been working on this for days. It works if you do add and subtract but i am suppose to use integer division and MOD and every different way I am doing it is not coming out with the right answer.
    this is what my problem is..
    Program: Suppose you can buy a chocolate bar from the vending machine for $1 each. Inside every chocolate bar is a coupon. You can redeem seven coupons for one chocolate bar from the machine. You would like to know how many chocolate bars you can eat, including from those redeemed via coupons, if you have n dollars.For example, if you have 20 dollars, then you can initially buy 20 chocolate bars. This gives you 20 coupons. You can redeem 14 coupons for two additional chocolate bars. These two additional chocolate bars give you two more coupons, so you now have a total of eight coupons. This gives you enough to redeem for one final chocolate bar. As a result you now have 23 chocolate bars and two leftover coupons.Also, for $50, you will get 58 chocolate bars and 2 coupons leftover.Write a program that inputs the number of dollars and outputs how many chocolate bars you can collect after spending all your money and redeeming as many coupons as possible. Also output the number of leftover coupons. The easiest way to solve this problem is to use a loop.Then, use a nested loop to ask if they would like to do this again. It should loop as many times as the user would like.
    TEST DATA:
    $20 gives 23 bars, 2 coupons
    $50 gives 58 bars, 2 coupons
    $11 gives 12 bars, 5 coupons
    $6 gives 6 bars, 6 coupons

    ok this is what I have:
    Option Strict On
    Option Explicit On

    Module Module1



    Sub Main()
    ' declare variable
    Dim amountDollar As Integer
    Dim totalCoupons As Integer
    Dim leftOverCoupons As Integer
    Dim numberofChocolate As Integer
    Dim freeChocolate As Integer
    Dim totalchocolate As Integer
    Console.WriteLine("Please enter your amount here")
    'begin outer loop for students
    amountDollar = CInt(Console.ReadLine())
    numberofChocolate = amountDollar
    freeChocolate = CInt(numberofChocolate / 7)
    totalchocolate = numberofChocolate + freeChocolate
    Console.WriteLine("Total number of chocolate: " & totalchocolate)
    leftOverCoupons = freeChocolate Mod numberofChocolate
    Console.WriteLine("Leftover Coupons: " & leftOverCoupons)
    amountDollar = CInt(numberofChocolate / 7)

    Can someone please tell me what I am doing wrong Thanks for looking.Also all my work has to be done with visual basic console application.

  2. #2
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: Can someone help me!!

    Here is the logic...
    Code:
     amountDollar = amountDollar = CInt(Console.ReadLine())
            numberofChocolate = amountDollar
            a = numberofChocolate \ 7
            b = numberofChocolate Mod 7
            If b > 0 Then
                c = a + b
            End If
            freeChocolate = a
            Do While c > 7
                a = c / 7
                b = c Mod 7
                c = a + b
                freeChocolate = freeChocolate + a
            Loop
    
            totalchocolate = numberofChocolate + freeChocolate
            Console.WriteLine("Total number of chocolate: " & totalchocolate)
            Console.WriteLine("Leftover Coupons: " & c)
    thanks
    amrita

  3. #3
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Can someone help me!!

    Here, I modified your code to include the right logic :
    vb.net Code:
    1. Module Module1
    2.     Sub Main()
    3.         ' declare variable
    4.         Dim amountDollar As Integer
    5.         Dim leftOverCoupons As Integer
    6.         Dim numberofChocolate As Integer
    7.         Dim freeChocolate As Integer
    8.         Console.WriteLine("Please enter your amount here")
    9.         amountDollar = CInt(Console.ReadLine())
    10.         numberofChocolate = amountDollar
    11.         leftOverCoupons = numberofChocolate
    12.         While (leftOverCoupons >= 7)
    13.             freeChocolate = Math.Truncate(leftOverCoupons / 7)
    14.             numberofChocolate = numberofChocolate + freeChocolate
    15.             leftOverCoupons = (leftOverCoupons Mod 7) + freeChocolate
    16.         End While        
    17.  
    18.         Console.WriteLine("Total number of chocolate: " & numberofChocolate)
    19.         Console.WriteLine("Leftover Coupons: " & leftOverCoupons)
    20.         Console.ReadLine()
    21.     End Sub
    22. End Module

    Its a little simpler, as it eliminates the need of certain variables.
    The part you had wrong is that once you traded coupons to chocolate bars you didn't added the new coupons that those chocolate bars gives you. Also the modulo calculation was not right. You also didn't add the loop, but you did mention that you needed one in your comment.
    Last edited by stlaural; Mar 10th, 2011 at 09:36 AM.
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width