1

I have been solving a problem for finding the total no. of for a getting a given sum total amount of money for user given denomination

#include<stdio.h> int denomination(int amt,int coin[],int n){ //int sum=0; if(amt==0){ return 1; } else if(amt<0)return 0; else{ int include=denomination(amt-coin[n-1],coin,n); int exclude=denomination(amt,coin,n-1); return include+exclude; } } int main() { // Insert your code here. int t,amt,n,coin[100]; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d",&coin[i]); } scanf("%d",&t); while(t--){ scanf("%d",&amt); int a=denomination(amt,coin,n); printf("%d\n",a); } return 0; } 

I implemented this code and am getting code segmentation fault

2
  • 1
    This is more of a c code than c++ Commented Jan 19, 2020 at 10:21
  • 1
    What happens when n is >= 100? You should sanitize your inputs. Commented Jan 19, 2020 at 10:23

1 Answer 1

2

This line causes the problem: int exclude=denomination(amt,coin,n-1); because amt is still as it is and n decreases with no base case. Imagine that n became 0 and you have to access coins[n-1] in this call: denomination(amt-coin[n-1],coin,n);

Please, reconsider your base cases again.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.