here is the code:int coin-change(int value,int index)
{if(value==0)
return 1;
if(index<0)
return 0;
if(index<0&&value>=1)
return 0;
else
return coin-change(value-coins[index],index)+coin-change(value,index+1);
}
int coins={1,5,10,20,50};int t=10;
i can't run it on c++ (no syntax error) but when i try running it starting from coin-change(t,4) it refuses and gives me logical error (the code itself comes from algorithmist site
Update:here is the link
http://www.algorithmist.com/index.php/Coin_Change
Copyright © 2024 QUIZLS.COM - All rights reserved.
Answers & Comments
Verified answer
- names in C++ must not contain dashes
- miss-copied the second if statement
- coins must be declared (as array) before it is used
- would help if you explained what problem you were trying to solve and gave a link to the source of the algorithm
int coins[] = {1,5,10,20,50};
int t=10;
int coinchange(int value,int index)
{
if(value==0)
return 1;
if(value<0)
return 0;
if(index<0 && value>=1)
return 0;
else
return coinchange(value-coins[index],index) + coinchange(value, index - 1);
}