I have a recrusive program -Im new to it!- that gives all the possible ways to make a money to smaller coins for example :
we want 10$
the coins are 5$ , 2$ , 1$

we can give 1 5$ , 2 2$ , 1 1$
or only 10 1$

the inpit is n the number of coins , then the coins , then the money we want to change

#include <iostream>

using namespace std;

int coins[100],n,m,nowqcoins[100];

void halatcoint ( int m , int nowcoins[] , int mon )
{
    int h;
    h=m;
    h++;
    if (mon==0) 
    {
        for (int i=0;i<n;i++)
            cout<<coins[i]<<"*"<<nowcoins[i]<<"     ";
        cout<<endl;
    }

    else 
    while (mon>=0 && m<n)
    {
        halatcoint( h , nowcoins , mon);
        mon -= coins[m];
        nowcoins[m]+=1;
    }
}

int main()
{
    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>coins[i];
    }
    cin>>m;
    halatcoint ( 0 , nowqcoins , m);
    cin>>n;
}

heres my code but I cant find my problem with it , in first numbers it gives right numbers but then it gives heroic numbers . thank you!

Recommended Answers

All 2 Replies

found it myself the only thing I should have done was

void halatcoint ( int m , int nowcoins[] , int mon )
{
    int h;
    h=m;
    h++;
    if (mon==0) 
    {
        for (int i=0;i<n;i++)
            cout<<coins[i]<<"*"<<nowcoins[i]<<"     ";
        cout<<endl;
    }

    else 
    while (mon>=0 && m<n)
    {
        halatcoint( h , nowcoins , mon);
        mon -= coins[m];
        for (int i=m+1;i<n;i++)
            nowcoins[i]=0;
        nowcoins[m]+=1;
    }
}
#include <iostream>
#include <vector>

using namespace std;

void PrintOptions (int amount, const int coinOptions[], const int coinCount, vector<int> paid = vector<int>());


void PrintOptions (int amount, const int coinOptions[], const int coinCount, vector<int> paid)
{
    // There's nothing left to pay!
    if (amount == 0)
    {
        // Show what we've paid to achieve this.
        for (int i = 0; i < paid.size(); i++)
        {
            cout << paid[i] << " ";
        }
        cout << endl;
    }

    // Still something to try to pay with.
    else if (coinCount > 0 && amount > 0)
    {
        // Try to pay as much big coins as possible, starting at 0 of them.
        do
        {
            // Print the options for the remaining amount, without the biggest coin and "paid" containing what we paid so far.
            PrintOptions(amount, coinOptions + 1, coinCount - 1, paid);

            // Pay another one of the biggest coin.
            paid.push_back(coinOptions[0]);
            amount      -= coinOptions[0];
        }
        while (amount >= 0);
    }
}

int main()
{
    const int COINS[] = { 5, 2, 1 };

    PrintOptions(10, COINS, sizeof(COINS) / sizeof(int));

    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.