-1

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;
}
  • Passing a 2D array when you don't know the size of the array at compile time is impossible. If allowed, [use something like this](https://stackoverflow.com/a/2076668/4581301). If not allowed, do something like that anyway, but without the `vector` – user4581301 Nov 15 '21 at 07:29
  • Also don't try to allocate storage for somehting before you know how much storage is needed. `int m[i][j]` depends on `i` and `j` for sizing and the values of `i` and `j` are unknown at that time. – user4581301 Nov 15 '21 at 07:30

1 Answers1

1

Please read Passing a matrix in a function (C)

There are some more flaws in your code

void matrixorder(int ar[], int n)
{
    int i,j,l,k,q;
    int m[i][j], s[i][j],p[i];
...

will lead to a memory bug. i and j are not initialized. int m[i][j] is no valid C. This might compile, but you may get some nasty bugs (different behavior between debug and release build). You tagged your question C++. Thus my advise is to have a look at std containers, such as std:vector. They can be easily passed by reference.

If you like to perform some calculations have a look at eigen or armadillo.

schorsch312
  • 5,553
  • 5
  • 28
  • 57
  • C and C++ are different languages, so the provided link is iffy. – user4581301 Nov 15 '21 at 07:38
  • @user4581301 The code from OP is more C than C++. This is why I suggested the usage of proper containers or C++ libs. I he likes to stick to C like pointer usage, the link will help. – schorsch312 Nov 15 '21 at 08:20
  • Okay. Also i am getting a segmentation fault when i try to run the code without the prinparanthesis function. Do you know what is the reason – Harsh Mohan Sason Nov 15 '21 at 11:18