How much were the coins worth?

Can you guys help me a little more with the programming stuff, I'm lost here.

As long as people follow our posting guidelines, they do not need to ask permission to post.

If your question(s) belong to a different exercise, please start a new thread.

Cheers :cool:
 
Module LostCoins


Sub Main()

Const p As String = "Pennies"
Const n As String = "Nickels"
Const d As String = "Dimes"
Const q As String = "Quarters"
Const h As String = "Half bucks"
Const b As String = "Bucks"
Const WEIGHT_IN_PENNY As Double = 2.500
Const WEIGHT_IN_NICKEL As Double = 5.000
Const WEIGHT_IN_DIME As Double = 2.268
Const WEIGHT_IN_QUARTER As Double = 5.670
Const WEIGHT_IN_HALFBUCK As Double = 11.340
Const WEIGHT_IN_BUCK As Double = 8.100

This is what I did. I looked at Denis pointer, but I just can't make it look right.
 
Do you not have a teacher teaching you that weird language?

I've never heard of it (plus sure not interested in learning it!);
perhaps someone else here has been exposed to it...good luck.

Visual BASIC? C'mon Denis.
 
I just want to know if I declared those variables right and how should I do next?

And yes, it is Visual basic... So far, I only learn basic codes straight out of the book, and fill codes in blank that the teacher provided. Thus, writing from a scratch is new and I need help. I won't ask much, beside check the codes for me and tell me why it is wrong, and a few pointer.
 
Do it the old fashion way. Hit compile. No errors and you're good to go ;)

No point in declaring constant strings like that though, and you're being a bit wordy in your declarations in my opinion (code commenting is just as good, and I have done so below). Constant declarations have their uses, but in most situations they are unnecessary. Finally, the Double precision datatype will work, but is a waste of memory for numbers this close to 0. I would proceed as follows:

Dim pWt As Single = 2.500 'weight for pennies
Dim nWt As Single = 5.000 'weight for nickles
Dim dWt As Single = 2.268 'weight for dimes
Dim qWt As Single = 5.670 'weight for quaters
Dim hWt As Single = 11.340 'weight for half-dollars
Dim wWt as Single = 8.100 'weight for whole dollars

Also since you'll be multiplying by 1000 anyway, you may as well declare the weights as Shorts to save half more memory, and display the weight in milli-grams (Shorts are 16-bit signed integers having max value 32767). So instead of the above:

Dim pWt As Short= 2500 'weight for pennies in mg
Dim nWt As Short = 5000 'weight for nickles in mg
Dim dWt As Short = 2268 'weight for dimes in mg
Dim qWt As Short = 5670 'weight for quaters in mg
Dim hWt As Short = 11340 'weight for half-dollars in mg
Dim wWt as Short = 8100 'weight for whole dollars in mg


 
Last edited:
Thanks so much, daon2! I'm gonna borrow that chunk of code, and see what I can do next.
 
So I did everything I could, compile it, and unfortunately, it didn't give me any error and at the same time I can't make it to work/show what I want it to show. So here it is:

Code:
Option Explicit On
Option Strict On


Imports System


Module LostCoins


    Sub Main()
    
        Const LOWEST As Integer = 0
        Const HIGHEST As Integer = 5
        
        Dim weights() As Integer = { 2500, 5000, 2268, 5670, 11340, 8100 }
        Dim coins() As String = { "Pennies", "Nickels", "Dimes", "Quarters", "HalfBucks", "Bucks" }
        
        Const TARGET As Integer = 304588
        
        Dim i1, i2, i3, i4, i5, i6 As Integer
        
        For i1 = LOWEST To HIGHEST - 5
            For i2 = i1 + 1 To HIGHEST - 4
                For i3 = i2 + 1 To HIGHEST - 3
                    For i4 = i3 + 1 To HIGHEST - 2
                        For i5 = i4 + 1 To HIGHEST - 1 
                            For i6 = i5 + 1 To HIGHEST
                            
                                If weights(i1) + weights(i2) + weights(i3) + weights(i4) + weights(i5) + weights(i6) = TARGET Then
                    
                                    Console.WriteLine("{0} + {1} + {2} + {3} + {4} + {5} = {6}", weights(i1), weights(i2), weights(i3), weights(i4), weights(i5), weights(i6), TARGET)
                    
                                End If
                            
                            
                            
                            Next
                        Next
                    Next
                Next
            Next
        Next
        
    End Sub
    
  End Module

Hope anyone can see what's wrong with it.
 
I'm sorry, but I am not sure at all what your for loop bounds are doing. I eliminated some dead weight from your module, and this works fine. I also followed Denis' suggestion.


Code:
Module Module1




    Sub Main()


        Const TARGET As Integer = 304588
        Const NumCoins As Short = 43
        Dim i1, i2, i3, i4, i5 As Integer


        For i1 = 0 To NumCoins
            For i2 = 0 To NumCoins - i1
                For i3 = 0 To NumCoins - i1 - i2
                    For i4 = 0 To NumCoins - i1 - i2 - i3
                        For i5 = 0 To NumCoins - i1 - i2 - i3 - i4


                            If i1 * 2500 + i2 * 5000 + i3 * 2268 + i4 * 5670 + i5 * 11340 + (43 - i1 - i2 - i3 - i4 - i5) * 8100 = TARGET Then


                                Console.WriteLine("The weight of {0} Pennies, {1} Nickles, {2} Dimes, {3} Quaters, {4} Half-dollars, and {5} Dollars is {6}. The value is ${7}", i1, i2, i3, i4, i5, 43 - i1 - i2 - i3 - i4 - i5, TARGET, (i1 + 5 * i2 + 10 * i3 + 25 * i4 + 50 * i5 + 100 * ((43 - i1 - i2 - i3 - i4 - i5))) / 100)


                            End If






                        Next
                    Next
                Next
            Next
        Next
        Console.ReadLine()


    End Sub
 
Thank you very much for your help. I'm still trying to get use and understand your code, in case if my teacher ever ask me. Some of the stuff in there are new to me.
 
Thank you very much for your help. I'm still trying to get use and understand your code, in case if my teacher ever ask me. Some of the stuff in there are new to me.

What don't you understand?

i1 = # of pennies, i2 = # of nickles, etc

If the number of pennies is 1, the loop for the nickles will top out at 43-1=42, since there are 43 coins. As Denis stated, if we know the number of pennies, nickles, dimes, quarters, and half-dollars, the number of whole dollar coins (what i6 would have been) is determined by 43 minus the sum of the others, so I removed that loop.
 
Thank you so much, the code just got so much clearer and make sense now; plus I know what's wrong with my previous code now.

Just one more question though, if you don't mind:

Console.WriteLine("The weight of {0} Pennies, {1} Nickles, {2} Dimes, {3} Quaters, {4} Half-dollars, and {5} Dollars is {6}. The value is ${7}", i1, i2, i3, i4, i5, 43 - i1 - i2 - i3 - i4 - i5, TARGET, (i1 + 5 * i2 + 10 * i3 + 25 * i4 + 50 * i5 + 100 * ((43 - i1 - i2 - i3 - i4 - i5))) / 100)

Can you explain the one I underlined, I don't quiet understand that.

Thank you very much.
 
Don't COPY code, please!

Any of you guys who get here by Google, please only use this as a reference. This one is already my assignment.
 
Me:
For i1 = 0 To NumCoins
For i2 = 0 To NumCoins - i1
For i3 = 0 To NumCoins - i1 - i2
For i4 = 0 To NumCoins - i1 - i2 - i3
For i5 = 0 To NumCoins - i1 - i2 - i3 - i4
i6 = 43 - i1 - i2 - i3 - i4 - i5
If i1 * 2500 + i2 * 5000 + i3 * 2268 + i4 * 5670 + i5 * 11340 + i6 * 8100 = TARGET

Easier if you need the value of i6 somewhere else...
Ok, thank you so much for the suggestion. I can see how they both related. daon just a bit more specific, and your version is a bit shorter, which is nice and easy to look.
 
Last edited:
Any of you guys who get here by Google, please only use this as a reference. This one is already my assignment.
Did anyone else notice the unintended irony?

Drews gets all sorts of tips from daon and denis on his coding problem, but stakes out a claim to intellectual property. The ways of the human mind are fascinating.
 
Did anyone else notice the unintended irony?

Drews gets all sorts of tips from daon and denis on his coding problem, but stakes out a claim to intellectual property. The ways of the human mind are fascinating.


Yes, I would like no one copying my code either (obviously being sarcastic here).

Drews, this is very basic coding. Any of your classmates who may see this could easily change the look of the code to make it "unique". I wouldn't be surprised if others have came up with a similar idea on their own, either. I can't think of many other approaches to the problem anyhow :cool:
 
Top