简体   繁体   中英

Segmentation fault in recursive program

I am doing the coin problem, the problem says that,

Given a set of coin values coins = {c1, c2,..., ck} and a target sum of money n , our task is to form the sum n using as few coins as possible.

suppose you have 9 dollars and you have set of {6,5,1} so, the minimum no. of sum/change for 9 dollars would be ( 6+1+1+1=9) ie 4 .

i tried doing it recursively using this formula:

solve(x) = min( solve(x−6)+1, solve(x−5)+1, solve(x−1)+1 )

,but I don't know why I'm getting Segmentation fault in my code.

There are plenty of codes available online, but I want to know what am I doing wrong here, I'm new to recursion please help me, The code goes here:

//my code

#include<bits/stdc++.h>
using namespace std;
int solve (int x, int a[], int n)
{
  if (x < 0)
    {
      return INT_MAX;
    }
  if (x == 0)
    {
      return 0;
    }
  int best = INT_MAX;
  for (int i = 0; i < n; i++)
    {
      best = min (best, solve (x - a[i], a, n) + 1);
    }
  return best;
}

int main ()
{
int a[] = { 6, 5, 1 };
int x = 9;
int n = 3;
cout << solve (x, a, n);
  return 0;
}

The code which have been took from: https://www.geeksforgeeks.org/find-minimum-number-of-coins-that-make-a-change/

    #include <iostream>
    using namespace std;
    
    int minCoins(int coins[], int m, int amount) {
        if (amount == 0) return 0;

        int res = INT_MAX;

        for (int i = 0; i < m; i++) {
            if (coins[i] <= amount) {
                int sub_res = minCoins(coins, m, amount - coins[i]);

                if (sub_res != INT_MAX && sub_res + 1 < res) { // avoid overflow 
                    res = sub_res + 1;
                }
            }
        }
        return res;
    }

    int main() {
        int coins[] = { 6, 5, 1 };
        int amount = 9;
        cout << "Min coins is " 
             << minCoins(coins, sizeof(coins) / sizeof(coins[0]), amount) 
             << endl;
        return 0;
    }

About the problem:

  1. Your Segmentation fault comes from the line: best = min (best, solve (x - i, a, n) + 1); The reason is: xi will always gives you the same value so if you are run the program without debugging, your program crashing. So don't try to debug it because it will takes a lot of time to see this crashing. For starters change to: best = min (best, solve (x - a[i], a, n) + 1); .

  2. After fixing the section 1, the if case: if (x < 0) return INT_MAX; will causes problem and will return always the same value, which is: -INT_MAX . So you need to check the "if cases" again.

  3. The algorithm you try to implement is not correct, see the pseudo-code of this algorithm:

     minchange(M): if M = 0: return 0 v <- infinity for c in denominations <= M: v <- min { minchange(M - c) + 1, v } return v
  4. Better use: sizeof(a) / sizeof(a[0]) instead of int n = 3 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM