Dynamic Programming Change Making Problem - Need Someone to Review

I've created a dynamic program in which you can input any amount with any given coin denominations and it should output the least amount of coins needed to solve, HOWEVER, there are some glitches I was hoping some of you could point out and correct. Here's the code for my main method and the code for my instance methods.
public class Changemaker
public static void main (String args[])
          int amt = Integer.parseInt(args[0]); // Initialize the method in which the program will be invoked
          Tuple[][] t = new Tuple[args.length - 1][amt + 1]; // Create the matrix
          int [] coins = new int [args.length - 1]; // Create empty array that stores the amount of coin denominations
          for (int i = 0; i < coins.length; i++) // Store input values into array called coins
               coins[i] = Integer.parseInt ( args[i+1] );
          for (int i = 0; i < coins.length; i++) // Establish a 0 value in the first column of every row
               t[0] = new Tuple (coins.length);
          for (int i = 0; i < coins.length; i++) // Create a tuple that checks if the given coin denomination can create the amount
               for (int j = 0; j < amt + 1; j++)
                    if (j - coins[i] < 0) // Create a null tuple if the amount is less than the arguments
                         t[i][j] = new Tuple (coins.length);
                    else // Mantra
                         int remainder = j - coins[i]; // Take the coin out of the amount
                         t[i][j] = new Tuple (coins.length); // Create a new blank tuple of coin length
                         t[i][j].setElement(i, 1);// Change the tuple location from 0 to 1 to keep track of it
                         t[i][j].add(t[i][remainder]); // Add the tuple in the remainder cell to the existing tuple
                    try
                         if (t[i][j].total() > t[i - 1][j].total()) // Return total elements in tuple directly above
                              if (t[i - 1][j] != null)
                                   t[i][j] = t[i - 1][j];                    
                    catch (ArrayIndexOutOfBoundsException e)
                    System.out.println(t[i][j].toString());
}Class for Instance Methodspublic class Tuple
     private int [] change;
     public Tuple (int n) // Constructor class, elements initialized at zero
          this.change = new int [n];
          for ( int i = 0; i < this.change.length; i++)
               this.change[i] = 0;
     public Tuple (int [] data) // Constructor class that creates a n-tuple from given data
          this.change = new int[data.length]; //Initialize the array change to incorporate data into each element
          for (int i = 0; i < this.change.length; i++)
               this.change[i] = data[i];
     public void setElement (int i, int j) // Set element i to value j
          this.change[i] = j;
     public int getElement (int i)
          return this.change[i];
     public int length()
          return this.change.length;
     public int total() // Return total of elements in tuple
          int sum = 0;
          for (int i = 0; i < this.change.length; i++)
               sum += this.change[i];
          return sum;
     public void add (Tuple t) // adds a tuple t to tuple
          * Make a new "change" array that equals the current one
          * Create another array called tuple t
          * Add the new "change" array with the tuple t
          for (int i = 0; i < this.change.length; i++)
               this.change[i] = this.change[i] + t.getElement(i);
     public boolean equals(Object t) // Return true if tuple identical to t
          * Determine if object is a tuple t
          * Check to see if tuple t is the same size of change
          * If true, loop both and compare
          for (int i = 0; i < this.change.length; i++)
               if (this.change[i] != getElement(i))
                    return false;
          return true;
     public Tuple copy() //Return an exact copy of tuple
          Tuple copy = new Tuple(this.change);
          return copy;
     public String toString() // Returns a string of the tuple
          String s = "";
          for (int i = 0; i < this.change.length; i++)
               s += this.change[i];
          return "<," + s + ",>";
          /* Add for loop
          * Add value at each index of array into string
*Output:*
*java ChangemakerTest 5 1 2 3*
<000>
<100>
<200>
<300>
<400>
<500>
<000>
<000>
<010>
<010>
<020>
<020>
<000>
<000>
<000>
<001>
<001>
<001>
*The correct output using the above execution line should be:*
<000>
<100>
<200>
<300>
<400>
<500>
<000>
<100>
<010>
<110>
<020>
<120>
<000>
<100>
<010>
<001>
<101>
<011>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

A bit of advice on getting advice.
1) Ask a specific question, nobody is going to debug your program for you
2) Avoid the word "Dynamic", especially if you don't know what it means.

Similar Messages

  • Unique Problem, Need someone smart

    I connected my iPod shuffle to my Xbox, and my Xbox formattted it and put new firmware on it, making it useless on Windows. I tried restoring it via the newest iPod updater, but it says "iPod service error." iPod service.exe is NOT on task manager's processes. On My Computer, it showed as a removeable disk. I clicked it and it said "Drive not Formatted. Format Drive?" I formatted it and now it is back to the FAT32 File system, but iPodservice.exe still isn't running resulting in no iPod Updater ALSO resulting in I CAN'T RESTORE IT!!!
    HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1
    Gateway   Windows XP  

    Im have the same problem. I do not even have the original software disc anymore, but I doubt that will help.
    Laptop   Windows XP   None

  • I need someone to review my program and tell me why it won't work

    I can't get this to work. It's a program that generates a set of random numbers, stores them as a .txt file then a second program imports that data and sorts it in descending order and displays the average and median of the data. If this is too messy and there is a better way to post these programs please tell me and I will repost. Sorry about the messyness but here you go:
    It's two separate .java files.
    RandMarks.java:
    import java.io.*;
    public class RandMarks
    public static void main (String [] args) throws IOException
    int randomNum;
    PrintWriter fileOut = new PrintWriter (new FileWriter ("marks.txt"));
    for (int i = 1; i <= 25; i++)
    randomNum = (int) (Math.random () * 60 + 40);
    fileOut.println (randomNum);
    fileOut.close ();
    IcsMarkbook.java:
    import java.io.*;
    public class IcsMarkbook
    public static void main (String [] args) throws IOException
    BufferedReader readFile = new BufferedReader (new FileReader ("marks.txt"));
    int inMarks [] = new int [25];
    int avgMark, medMark;
    int totalMarks = 0;
    for (int i = 0; i < 100; i++)
    inMarks = Integer.parseInt (readFile.readLine ());
    bubbleSort (inMarks);
    for (int p = 0; p < inMarks.length; p++)
    System.out.println ("#" + p + " - " + inMarks [p]);
    totalMarks = totalMarks + inMarks [p];
    avgMark = totalMarks / inMarks.length;
    System.out.println ("The average is " + avgMark);
    medMark = inMarks [13]
    public static void bubbleSort (int array [])
    for (int pass = 0; pass < array.length - 1; pass++)
    for (int element = 0; element < array.length - 1; element++)
    if (array [element] > array [element + 1])
    swap (array, element, element + 1);
    //Swap method
    public static void swap (int array2 [], int first, int second)
    int hold;
    hold = array2 [first];
    array2 [first] = array2 [second];
    array2 [second] = hold;

    RandMarks.java:
    import java.io.*;
    public class RandMarks
         public static void main (String [] args) throws IOException
              int randomNum;
              PrintWriter fileOut = new PrintWriter (new FileWriter ("marks.txt"));
              for (int i = 1; i <= 25; i++)
                   randomNum = (int) (Math.random () * 60 + 40);
                   fileOut.println (randomNum);
              fileOut.close ();
    }IcsMarkbook.java:
    import java.io.*;
    public class IcsMarkbook
         public static void main (String [] args) throws IOException
              BufferedReader readFile = new BufferedReader (new FileReader ("marks.txt"));
              int inMarks [] = new int [25];
              int avgMark, medMark;
              int totalMarks = 0;
              for (int i = 0; i < 100; i++)
                   inMarks = Integer.parseInt (readFile.readLine ());
              bubbleSort (inMarks);
              for (int p = 0; p < inMarks.length; p++)
                   System.out.println ("#" + p + " - " + inMarks [p]);
                   totalMarks = totalMarks + inMarks [p];
              avgMark = totalMarks / inMarks.length;
              System.out.println ("The average is " + avgMark);
              medMark = inMarks [13]
         public static void bubbleSort (int array [])
              for (int pass = 0; pass < array.length - 1; pass++)
                   for (int element = 0; element < array.length - 1; element++)
                        if (array [element] > array [element + 1])
                             swap (array, element, element + 1);
         //Swap method
         public static void swap (int array2 [], int first, int second)
              int hold;
              hold = array2 [first];
              array2 [first] = array2 [second];
              array2 [second] = hold;
    }The message that I got was:
    Exception in thread "main" jaja.lang.NumberFormatException: null
            at java.lang.Integer.parseInt(Integer.java:415)
            at java.lang.Integer.parseInt(Integer.java:497)
            at IcsMarkbook.main(IcsMarkbook.java:13)
    I have gone over the program multiple times and cannot figure out what's wrong.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Map problem,need someone to solve it!!!!

    Map can't find the routes for bus,sometimes car or walker, it's been like that for a few weeks, it used to work before, it always shows that transit locations could not be found between these locations, and it's really weird that even though I searched on google map online, either phone or my mac , it didn't work , so sad:( can someone who's really pro can help me??? Thanks:)

    Maybe vendor took it off?
    Are you regulary updating yout Tablet OS?
    Leader of Ljubljana BlackBerry Developer Group
    BlackBerry Certified Builder for Native Application Development

  • Odd ipod problem needs someone smarter then me

    i plugged in my ipod to charge and update it and that was about 4 days ago. i keep getting two different results. either it just stays at the "do not disconnect" stage, or "please wait, battery is very low". and then sometimes i get a "charged" screen and i disconnect only to find its not charged at all and when i connect it again and it says ipod or itunes file is corrupt. i can't do any of the 5 R's because the battery is dead and it won't let me. i've searched the support pages but can't seem to find anything that works. does anyone have any ideas or suggestions. thanks so much

    Was this by any chance the first time you have connected your iPod since upgrading to iTunes 7 because if it is the new software is causing havoc to any iPod it comes in contact with.

  • Feature request: Showing Program change # in addition to Patch & Set names

    Right now, MS2 is pretty much designed to step forward and backward through patches and sets. Although you do assign program change numbers to each patch and you can set a text box on screen to display the program number once you have selected the patch, it would be nice if we could add a third column in the Set - Patch selection module that also shows the program change #. Having this information displayed on the screen would give you a quick reference of the number that needed to be chosen to jump directly to a patch instead of relying on stepping forward or backwards to get to the patch you want to play. Right now, when I am configuring a concert, I load in previously used sets that included patches that will be played for that song. If we had this information displayed on the screen, we wouldn't have to worry about changing all the program #'s for each patch, since we could just use the same program # we assigned to the patch when we first created it.
    In fact, taking this approach one step further, we could view all of our user patches along with their respective program change numbers just like you can on a hardware sampler or synth.
    I don't know if this is something that could actually be done using text boxes or such that are connected to the program change number, but if someone has a suggested I would be interested.
    thanks

    Having a way to quickly renumber the program changes for the patches would certainly be helpful. However, what I am interested in is a way to display the program change number right along side the patch name in the patch / set list combo box. I know there is a way to display the program change number by itself that displays the currently selected patch, but that doesn't help me either (other than to verify that I have selected the right patch).
    What I am trying to do is to avoid having to change the program change numbers each time I set up a concert. I would like to create a patch, assign a specific program change number to it, and save it as is. Then when I build my concert, using Set's as placeholders for each song, I can see what the program change # is needed to change to a specific patch. This also means that I can assign that specific program change # in my pedal board so that whenever I use that patch in a Concert for a gig, I can call it up on my pedal board as well without having to change anything. I guess I can still save each patch with a unique program change # and then make a notation on my music what program change # I need to select on my pedal board, but it would be nice to have the pc #s displayed so I can just jump to it on the fly without having to previously know what the number is.
    (I am probably making this more complicated that it needs to be ...)
    Thanks

  • I Baught A iMac 2010 From Someone it came with lion but for the programs i use i need snow leopard but when trying to install it won't boot from usb or cd what can be the problem i've been at this for over 24 hours if anyone ca help it'd be greatly apprec

    I Baught A iMac 2010 From Someone it came with lion but for the programs i use i need snow leopard but when trying to install it won't boot from usb or cd what can be the problem i've been at this for over 24 hours if anyone ca help it'd be greatly appreciate it! Thanks!

    Downgrade OS X Lion To Snow Leopard [Video How-To]
    http://www.cultofmac.com/110614/downgrade-os-x-lion-to-snow-leopard-video-how-to /
    How To Downgrade OS X Lion To Snow Leopard: The Complete Tutorial
    http://www.redmondpie.com/how-to-downgrade-os-x-lion-to-snow-leopard-the-complet e-tutorial/
    How To Downgrade from Apple OS X Lion to Snow Leopard
    http://www.pcmag.com/article2/0,2817,2389334,00.asp

  • I need someone to take over my computer and fix this problem for me

    pls help i need someone to fi this problem for me my j4680 will not go wireless can someone take over my computer and find out what is wrong plsssssssssssssss

    Try HP's diagnostic utilities:
    http://h30434.www3.hp.com/t5/Printer-networking-and-wireless/Network-Printer-Problems-Try-the-HP-Hom...
    ******Clicking the Thumbs-Up button is a way to say -Thanks!.******
    **Click Accept as Solution on a Reply that solves your issue to help others**

  • What kind of programming background do you need to start making iOS apps?

    I have zero knowledge of programming. I have done programming about 7 years ago and it was extremely basic stuff and mostly to do with C. I have learnt BASIC language (wonder if its even worth mentioning) in school!! But, I really want to program. Even if it takes a long time. How do I get started??

    Your question "What kind of programming background do you need to start making iOS apps?" does not have a single answer. Thanks to the internet, nowadays its possible to start from zero. You can find an answer for even the most basic question on the internet. It just a question of time and effort. If you have
    the time
    no pressing deadline and a
    high frustration tolerance
    just go ahead and do it.
    Regards - Roe

  • I'm using firefox 6.0.2 It's making problem with Bangla font ONLY on FACEBOOK others bangli website FORNT SIZE are OK. On the FACEBOOK it shows Bangla font in a very / Too small! I change font SIZE in firefox setting but i can't solve. Pls HELP me

    I'm using firefox 6.0.2 It's making problem with Bangla font ONLY on FACEBOOK others bangli website FORNT SIZE are OK. On the FACEBOOK it shows Bangla font in a very / Too small! I change font SIZE in firefox setting but i can't solve. Pls HELP me ..

    Reset the page zoom on pages that cause problems: <b>View > Zoom > Reset</b> (Ctrl+0 (zero); Cmd+0 on Mac)
    *http://kb.mozillazine.org/Zoom_text_of_web_pages
    You can use an extension to set a default font size and page zoom on web pages.
    *Default FullZoom Level: https://addons.mozilla.org/firefox/addon/default-fullzoom-level/
    *NoSquint: https://addons.mozilla.org/firefox/addon/nosquint/

  • Help with dynamic programming, knapsack problem

    Hi, I wrote a code to solve the knapsack 0-1 problem by dynamic programming. At a certain point, around 30 max capacity, the code stops adding new values based on the incrementing max capacity and item values. I have no idea why, it finds the correct solution until the max capacity of the knapsack gets to around 30, and then the answers are screwed up. My code is below. Thanks in advance for the help!
         public Knapsack packDP(int capacity)
              //initialize 2d array
              Knapsack [][] knapsackSolutions = new Knapsack[safe.size() + 1][capacity + 1];
              //fill 1st row and 1st columns with empty knapsacks as sentinel values
              for (int i = 0; i <= safe.size(); i++)
                   knapsackSolutions[0] = new Knapsack();
              for (int j = 0; j <= capacity; j++)
                   knapsackSolutions[0][j] = new Knapsack();
              //each row of the 2d array represents an item. for loop to calculate solutions for each item
              for (int itemNum = 1; itemNum <= safe.size(); itemNum++)
                   Item currentItem = safe.get(itemNum - 1);
                   //get optimal solutions for each weight value in the current item row
                   for (int weight = 0; weight <= capacity; weight++)
                        if (currentItem.size <= weight)
                             System.out.println("weight = " + weight);
                             if ( (currentItem.value + knapsackSolutions[itemNum - 1][weight - currentItem.size].value) >
                                       knapsackSolutions[itemNum - 1][weight].value )
    //create a new knapsack with the new item
                                  knapsackSolutions[itemNum][weight] = new Knapsack(knapsackSolutions[itemNum - 1][weight - currentItem.size], currentItem);
                             else
                                  knapsackSolutions[itemNum][weight] = new Knapsack(knapsackSolutions[itemNum - 1][weight]);
                        else
                             knapsackSolutions[itemNum][weight] = new Knapsack(knapsackSolutions[itemNum - 1][weight]);
                        count++;
              //return last item of the 2d array, which is the optimal solution for the capacity
              return knapsackSolutions[safe.size()][capacity];

    I've been debugging forever and I can't figure out where I went wrong. The test driver for this is:
    public class ThiefDriver
        public static void main(String[] args) {
            /// Set up experiment:
            int INITIAL = 10 ;
            int INCREMENT = 10 ;
            /// Create objects with add(NAME,SIZE,VALUE):
            Thief thief = new Thief();
            thief.add("A",3,4) ;
            thief.add("B",4,5) ;
            thief.add("C",7,10) ;
            thief.add("D",8,11) ;
            thief.add("E",9,13) ;
            /// Run experiment:
            Thief.Knapsack knapsack;
            for (int capacity=INITIAL ; capacity <= thief.safe.capacity ; capacity+=INCREMENT) {
              System.out.println("Capacity = " + capacity) ;
              thief.count = 0 ;
              knapsack = thief.packDP(capacity) ;
              System.out.println( "  DP value/COUNT: " + knapsack.value + " " + thief.count ) ;
          }

  • Need idea on dynamic programing?

    Hi, I am new to ABAP. I need clear idea on dynamic programming. Can any one suggest me where do i get the help on this and also pls suggest any links of sample examples and material on this.

    Hi Niranjan,
    Here are the few links which helps you a lot.
    <li>[Dynamic internal table with dynamic fields.|http://an-abaper.blogspot.com/2009/06/dynamic-internal-table-with-dynamic.html]
    <li>[Dynamic Internal table Creation.|http://an-abaper.blogspot.com/2009/06/dynamic-internal-table-creation_16.html]
    Thanks
    Venkat.O

  • Hi, I need someone to help me change the login screen. The computer I have is a macbook, its second hand and probably around 3-4 years old. The previous owner had the login screen has a lego or something. Could someone please help me change this??

    Hi, I need someone to help me change the login screen. The computer I have is a macbook, its second hand and probably around 3-4 years old. The previous owner had the login screen has a lego or something. Could someone please help me change this??

    computerilliterate52 wrote:
    hi all, i hope someone is very computer litterate about java that is i have a dell 2400 pc windows xp 2, well since msn isnt downloading anymore java updates we all have to download a compatible one to our computer i use explorer and i can play www.iwin.com online games, yet when i go on to the msn browswer it wont load the games all i see is it just trying to load and load nothing said and nothing done. what could be wrong it works on ie and not msn browser? i have zone alarm, i have avg spyware tester and spyblaster, but i thought it may of kept me from downloading all this yet it worked on IE please help and i dont have flash player either it wont seem to work any and i mean any help is welcome.i tried to change the browser settings and did all that stuff i am on broadband DSL and they dont seem to know they say that isnt there job its msn's so i asked them and they said no it is quest broadband since i am with them i just get the run around i have IE 6.0 ihave java plug in 1.4.2. o3 yet it says not verified, i have java 1.6.o o1 i have java plugin 1.6.0 01 i have that twice ? dont know why. and java envirinmemet i have the right stuff cuz like i said it plays or works on explorer not msn browserer bar. i see no picture's in center of the page as it tries to load it is a blank page any help thank you i have been trying going on 3 weeks now thanksI agree with the other posters; try Firefox. It sounds like a bit of a mess and what you may need is an onsite PC tech to do some maintenance on your PC.
    Also, learning to properly use punctuation (particularly periods) might not be a bad idea. As it is, your post was very hard to read.
    Good luck!

  • Help needed regarding Dynamic Programming

    Hi,
    While doing dynamic programming , we bind the context variable with two types of
    values .
    1 . ddic
    2 . extern
    My doubt is in which case we should use ddic and where to use extern .
    Can anybody help me out regarding this.
    Thanks a lot.

    Hi Ki,
    Predefined, Web Dynpro UI-specific, and user-defined Dictionary types all have the
    prefix ddic:.
    wdContext.getNodeInfo()
    .addAttribute(
    "Visibility",
    "ddic:com.sap.ide.webdynpro.uielementdefinitions.Visbility")
    •&#61472;Logical Dictionary types from Adaptive RFC models have the prefix extern:.
    Check this links
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/9214b1e5-0601-0010-fdb0-ec32d43b06e0
    /people/dipankar.saha3/blog/2007/05/31/how-to-create-dynamic-approval-process-using-conditional-loop-block-in-guided-procedure
    Regards,
    Mithu

  • HT4101 Problem sending midi program changes to a Boss Gt100

    Hi I am using an app called Set List Maker to send midi program changes to a Korg Triton keyboard successfully but no such luck with a Boss GT100 guitar processor. The message I get is something like cannot use this device. 

    Hi I am using an app called Set List Maker to send midi program changes to a Korg Triton keyboard successfully but no such luck with a Boss GT100 guitar processor. The message I get is something like cannot use this device. 

Maybe you are looking for