I'm trying to code a dynamic programming problem and i am not figure out a way to pass that s[][] matrix to that function. Can anyone help me out with a solution. Also i'm getting a segmentation fault when i run the code without including the printparanthesis function in my code. I know that it needs to have bounds in that 2d array but i'm not able to figure out how to pass it so that when i call this function in my matrix order function, it runs correctly.
#include <iostream>
using namespace std;
void printparanthesis(int s[][], int i, int j, char &name)
{
if (i == j)
{cout<<name++<<endl;
return;
}
cout<<"(";
printparanthesis(s,i,s[i][j],name);
printparanthesis(s,s[i][j]+1,j,name);
cout<<")";
}
void matrixorder(int ar[], int n)
{
int i,j,l,k,q;
int m[i][j], s[i][j],p[i];
for (i =1; i<n; i++)
{ m[i][j] = 0; }
for (l=2; l<n; l++)
{
for(i=1; i<n-l+1; i++)
{
j = i+l-1;
m[i][j] = 1.0/0.0;
}
for(k = i; k<j-1;k++)
{
q = ((m[i][k] + m[k+1][j]) + (p[i-1] * p[k] * p[j]));
}
if (q<m[i][j])
{
m[i][j] = q;
s[i][j] = k;
}
}
char name = 'A';
printparanthesis(s,1,n,name);
cout<<"Optimal Cost: "<<m[1][n];
}
int main() {
int ar[] = {4,10,3,12,20,7};
int n = sizeof(ar)/sizeof(ar[0]);
matrixorder(ar,n-1);
return 0;
}