-
Mar 10th, 2011, 08:18 AM
#1
Thread Starter
Junior Member
[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.
-
Mar 10th, 2011, 09:13 AM
#2
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)
-
Mar 10th, 2011, 09:30 AM
#3
Re: Can someone help me!!
Here, I modified your code to include the right logic :
vb.net Code:
Module Module1 Sub Main() ' declare variable Dim amountDollar As Integer Dim leftOverCoupons As Integer Dim numberofChocolate As Integer Dim freeChocolate As Integer Console.WriteLine("Please enter your amount here") amountDollar = CInt(Console.ReadLine()) numberofChocolate = amountDollar leftOverCoupons = numberofChocolate While (leftOverCoupons >= 7) freeChocolate = Math.Truncate(leftOverCoupons / 7) numberofChocolate = numberofChocolate + freeChocolate leftOverCoupons = (leftOverCoupons Mod 7) + freeChocolate End While Console.WriteLine("Total number of chocolate: " & numberofChocolate) Console.WriteLine("Leftover Coupons: " & leftOverCoupons) Console.ReadLine() End Sub 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|