Dynamic programming

Hi, I am trying solve coin change making problem using dynamic programming but I have double problem because I can't use 1 coins. My coins are {2,5,25,50};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 #include <iostream>
#include <cstdlib>

using namespace std;
int dynammic(int *coins)
{
  int change;
  cout<<"Enter change: "<<endl;
  cin>>change;
    int table[change+1][4];
 for(int i=0; i<4; i++)
 {
     table[i][0] = 0;
 }
 int count;
 for(int i=0; i<4; i++)
 {
     ile = 0;
     int Min = change+1;
   for(int j=1; j<change+1; j++)
   {
     if(coins[i]>j)
     {
       //  cout<<i-1<<endl;
         table[i][j] = table[i][j-1];

     }
     else
     {
          
          table[i][j] = table[i][j-coins[i]]+1;
          count = table[i][j];
         if(ile<Min)
            Min = count;
     }
   }
 }

 for(int i=0; i<4; i++)
 {
     for(int j=0; j<change+1; j++)
     {
        cout<<table[i][j]<<" ";
     }
       cout<<endl;
 }
   cout<<count;
}

int main()
{
   int  coins[4] = {15,10,5,2};
    dynammic(coins);
    return 0;
}
you cannot make change that results in 1 and 3, then. It simply cannot be done unless you have pieces of 8 and can cut them in half :P

in the case of 3, you can give them a 2 and short them by 1 or over pay them by 1.
in the case of 1, you can over pay by 1.

Any other result is possible, so its on you to get that to happen. Maybe think of it like a bank's teller machine: it can't give 1's and I don't think it can give 5's, so you have to tell the user to stop being an idiot and put in a reasonable amount until they comply.
Last edited on
By the way the following is not legal in C++:
1
2
  cin>>change;
    int table[change+1][4];

In C++ array sizes must be compile time constants.

Also this appears to be wrong as well:
1
2
3
4
5
6
7
8
9
    int table[change+1][4];
...
 for(int i=0; i<4; i++)
 {
...
   for(int j=1; j<change+1; j++)
   {
...
         table[i][j] = table[i][j-1];

It appears that you have swapped the sizes of the array in your for() loop condition statement.
Last edited on
Topic archived. No new replies allowed.