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;
}