Coins

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mia023
    New Member
    • May 2007
    • 89

    Coins

    Hi i have an assignment that i don't understand and need help in.

    Write a program that reads in a command line integer N(number of pennies) and prints out the best way(fewest number of coins) to make change using US coins (quarters, dimes, nickels and pennies only). Your algorithm should dispense as many quarters as possible, then dimes, then nickels, and finally pennies.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by mia023
    Hi i have an assignment that i don't understand and need help in.

    Write a program that reads in a command line integer N(number of pennies) and prints out the best way(fewest number of coins) to make change using US coins (quarters, dimes, nickels and pennies only). Your algorithm should dispense as many quarters as possible, then dimes, then nickels, and finally pennies.
    Here's a little hint: suppose you've got 81 pennies; watch this:

    81/25 == 3 (quarters)
    81%25 == 6 (rest of the change)

    etc. etc.

    kind regards,

    Jos

    Comment

    • mia023
      New Member
      • May 2007
      • 89

      #3
      why did you do 81%25 and how can i get the rest

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by mia023
        why did you do 81%25 and how can i get the rest
        The % is operator is Java's (and C and C++'s) modulo operator. 81%25 == 6
        because the rest is 6 when you divide 81 by 25.

        kind regards,

        Jos

        Comment

        • mia023
          New Member
          • May 2007
          • 89

          #5
          should i use nested loops or if statements

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            I don't think you'll need either - just repeat Jos' set of commands for quarters, dimes, and nickels, and whatever is left is pennies.

            Comment

            • mia023
              New Member
              • May 2007
              • 89

              #7
              this is my code but i think it is wrong


              public class Coin{
              public static void main(String[]args){
              int N=Integer.parse Int(args[0]);

              int q=N%25;
              int d=N%10;
              int n=N%5;

              int p=q+d+n;

              System.out.prin tln(N/25+" quarters");
              System.out.prin tln(N/10+" dimes");
              System.out.prin tln(N/5+" nickels");
              System.out.prin tln(p+" pennies");
              }
              }

              Comment

              • mia023
                New Member
                • May 2007
                • 89

                #8
                sorry i send the code 2 times it was by mistake

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by mia023
                  sorry i send the code 2 times it was by mistake
                  No need to worry, I removed one of them; and indeed your code is completely wrong.
                  Back to the example again: for 81 pennies you can fit 81/25 == 3 quarters and
                  you have 81%25 == 6 pennies left. Now generalize this and don't sprinkle in
                  the / and % operators where they don't make sense. Do your math.

                  kind regards,

                  Jos

                  Comment

                  • mia023
                    New Member
                    • May 2007
                    • 89

                    #10
                    Originally posted by JosAH
                    No need to worry, I removed one of them; and indeed your code is completely wrong.
                    Back to the example again: for 81 pennies you can fit 81/25 == 3 quarters and
                    you have 81%25 == 6 pennies left. Now generalize this and don't sprinkle in
                    the / and % operators where they don't make sense. Do your math.

                    kind regards,

                    Jos
                    should i loop them and for example generalize the formula by N%num where N is the number of pennies and num is how many pennies are worth in dimes,nickels and quarters

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by mia023
                      should i loop them and for example generalize the formula by N%num where N is the number of pennies and num is how many pennies are worth in dimes,nickels and quarters
                      Indeed you should generalize it.

                      Comment

                      • mia023
                        New Member
                        • May 2007
                        • 89

                        #12
                        Originally posted by r035198x
                        Indeed you should generalize it.
                        Can you please tell me the conversions(pen nies to quarters,etc... .) and tell me what is wrong with the code above.

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by mia023
                          Can you please tell me the conversions(pen nies to quarters,etc... .) and tell me what is wrong with the code above.
                          I don't know what you mean by conversions (pennies to quarters ...), I've never even seen any of them ...

                          What you need to do is have your algorithm (pseudo code) down.
                          Store the denominations in, say an array. Biggest coing on position 0, second biggest on position 1, e.t.c and smallest coin on last posistion in the array.
                          The algorithm should be very easy to construct from there using the greedy algorithm.

                          Comment

                          Working...