-3

I'm trying to build a recursive call for coin change in c++ . i tried most of the algorithm on the internet but it doesn't seem to apply with vector or it doesn't out the sums of the coin used. Can anyone help me understand what the recursive function has to call? So my algorithm doesn't give me the minimum number of coin used and i don't know how to save the coin used.

int coin(vector<int> denom, int s,int N)
{
    if(N == 0)
    {
        return 1;
    }
    if(N < 0 || (N > 0 && s < 0))
    {
        return 0;
    }

    return min(coin(denom,s - 1, N), 1 + coin(denom, s,N-denom[s-1]));

}


 Input a value N:
    Input: 40
 Input how many denominations:
    Input: 3
 Denominations #1: 
    Input: 5
 Denominations #2:
    Input: 20
 Denominations #3:
    Input: 30

 Output:
 Minimum # of coins: 2
 Coin used: 20 + 20
 Don't want:  30 + 5 + 5
Darkflame
  • 79
  • 2
  • 11
  • What is `s` and `N` in this context? Can you please define the problem a bit clearly, as to what you want to achieve? – Ahmed Akhtar May 14 '16 at 02:06
  • s is size and N is the number. I want the function to give me the minimum # of coin and also produce the result of the coins used – Darkflame May 14 '16 at 02:07
  • I don't see any arrays here. – xaxxon May 14 '16 at 02:15
  • 2
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. http://stackoverflow.com/help/mcve – xaxxon May 14 '16 at 02:16
  • array size is not needed because `vector` has in built function `size()` which does that for you. – Ahmed Akhtar May 14 '16 at 02:19
  • Please also post sample input and desired output to make the problem clear. – Ahmed Akhtar May 14 '16 at 02:20
  • sorry how can i add the input and output? – Darkflame May 14 '16 at 02:25
  • @Darkflame leave your code in the question and ask specifically what issue you're having. – kmdreko May 14 '16 at 02:41

1 Answers1

3

Some points to consider:

  • Firstly, there is no need to send the number of denominations i.e. s as an argument to the coin method as long as a vector is being used, because vector has inbuilt size() method which does that job for us.
  • Secondly, to save the solution you need another vector of int named solution, but this vector is just to keep a record and has nothing to do with the actual recursive implementation and hence, it is defined as a global variable. Alternatively, you could pass it as an argument by reference to the coin method too.
  • Thirdly, the denominations entered by the user should be sorted before passing them to the coin method. For this, I have used the sort method from the algorithm library.

What the recursive algorithm basically does is:

  • At each step, it considers the largest denomination d (last element in the sorted denomination vector denom like denom[denom.size() - 1]) which is then removed from the vector using pop_back method of vector.
  • Using d we find count_d, which is the number of coins of denomination d, used in the solution. We get this by simply applying a div operation like N/d, which gives the Quotient.
  • Then d is added to the vector solution, count_d number of times.
  • The recursive call, adds count_d from this iteration and recalls coin with the reduced denominations vector denom and Remainder of the amount N using N%d.

See Quotient and Remainder for clarity of what div / and mod % operators do.

Here is the code:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<int> solution;

int coin(vector<int> denom, int N)
{
    if(N <= 0 || denom.size() <= 0)
    {
        return 0;
    }

    int d = denom[denom.size() - 1];
    denom.pop_back();

    int count_d = N/d;

    solution.insert(solution.end(), count_d, d);

    return count_d + coin(denom, N%d);
}

int main()
{
    int N,s;
    cout<<"Input a value N:\nInput: ";
    cin>>N;
    cout<<"Input how many denominations:\nInput: ";
    cin>>s;
    vector<int> denom;

    for(int i = 0; i < s; i++)
    {
        int d;
        cout<<"Denominations #"<<i+1<<":\nInput: ";
        cin>>d;
        denom.push_back(d);
    }

    std::sort(denom.begin(), denom.end());

    int minNoOfCoins = coin(denom, N);

    cout<<"\nOutput:\nMinimum # of coins: "<<minNoOfCoins;

    if(minNoOfCoins > 0)
    {
        cout<<"\nCoins used: ";

        for(int i = 0; i < solution.size(); i++)
        {
            if(i > 0)
            {
                cout<<" + ";
            }
            cout<<solution[i];
        }
    }

    cout<<endl;

    system("pause");
}
Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28
  • hey, thx for posting this! However the code doesn't give the minimum coin used; Ex: if we make N = 40 and denom = { 5 , 20 , 30}.... t he result gives me 30 + 5 + 5 when the minimum should be 20 + 20.... Thx though for the help – Darkflame May 14 '16 at 16:13
  • Yeah you are right this is a greedy algorithm, for that you will have to use a brute force or dynamic programming algorithm. – Ahmed Akhtar May 14 '16 at 19:19