0

Given a value V. You have to make change for V cents, given that you have infinite supply of each of C{ C1, C2, .. , Cm} valued coins. Find the minimum number of coins to make the change.

My code is showing segmentation fault. Someone please help.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
    //code
    int q;
    cin>>q;
    while(q--)
    {
        int V,m;
        int i,j,a[m];
        cin>>V;
        for(i=0;i<m;i++) cin>>a[i];
        int t[m+1][V+1];
        for(i=1;i<=m;i++) t[i][0]=0;
        for(j=0;j<=V;j++) t[0][j]=INT_MAX-1;
        for(j=1;j<=V;j++) { if(j%a[0]==0) t[1][j]=j/a[0]; else t[1][j]=INT_MAX-1;}
        for(i=2;i<=m;i++)
        {
            for(j=1;j<=V;j++)
            {
                if(a[i-1]<=j) t[i][j]=min(t[i-1][j],t[i][j-a[i-1]]+1);
                else t[i][j]=t[i-1][j];
            }
        }
        int ans=t[m][V];
        if(ans==INT_MAX) cout<<"-1";
        else cout<<ans;
        cout<<endl;
        
    }
    return 0;
}
Pratiksha
  • 3
  • 3
  • What is the input you're testing with to cause the segfault? – scohe001 Sep 09 '20 at 13:03
  • the last index in an array with N elements is N-1. Accesing arrays out-of-bounds may lead to seg fault – 463035818_is_not_a_number Sep 09 '20 at 13:03
  • 2
    Also, it looks like you never set `m` *anywhere*, but then you do `int a[m]`. Is this intentional? – scohe001 Sep 09 '20 at 13:04
  • 2
    also note that `int a[m];` is not standard C++, you should either not use it (because there is no need to when you can use `std::vector`) or read your compilers manual to know what it actually does. See also [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_a_number Sep 09 '20 at 13:05
  • 1
    and don't forget to consult: [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – 463035818_is_not_a_number Sep 09 '20 at 13:06
  • one advantage of writing standard C++ is that there are many coders that understand the language and that any compliant compiler can compile it. Using a compiler extension for `int a[m];` and with the second line in your code you deliberately chose to write non-standard C++, something that not every C++ compiler can compile and something not every C++ programmer can understand. Thats the price, consider if is worth to pay it to get ... I really don't know what is the advantage. – 463035818_is_not_a_number Sep 09 '20 at 13:09
  • well maybe it wasnt "deliberately" but it is a decision that should be taken deliberately not just out of lazyness or because you saw it in some online tutorial and "It works" – 463035818_is_not_a_number Sep 09 '20 at 13:12
  • Got it ! Thank you so much. – Pratiksha Sep 09 '20 at 13:31

0 Answers0