I wrote a c++ program to calculate matrix multiplication problem using dynamic programming. I use s[][]
to store the places of inserting parentheses. However, I get those errors about using the two dimensional array as parameter:
matrics.cpp:4:38: error: expected ')'
void findtrace(int i, int j, int[][7]s){
^
matrics.cpp:4:15: note: to match this '('
void findtrace(int i, int j, int[][7]s){
^
matrics.cpp:8:17: error: use of undeclared identifier 's'
printf("%d\t",s[i][j]);
^
matrics.cpp:9:18: error: use of undeclared identifier 's'
findtrace(i,s[i][j],s);
^
matrics.cpp:10:13: error: use of undeclared identifier 's'
findtrace(s[i+1][j],j,s);
^
matrics.cpp:42:2: error: no matching function for call to 'findtrace'
findtrace(1,len-1,s);
^~~~~~~~~
matrics.cpp:4:6: note: candidate function not viable: no known conversion from 'int [len][len]' to 'int (*)[7]' for 3rd argument
void findtrace(int i, int j, int[][7]s){
^
5 errors generated.
It cost me two hours to debug this; however, I still have that error. Anyone can help me?
#include <iostream>
using namespace std;
void findtrace(int i, int j, int[][7] s){
if(i==j)
printf("%d\t", i);
else{
printf("%d\t",s[i][j]);
findtrace(i,s[i][j],s);
findtrace(s[i+1][j],j,s);
}
}
int main(){
int p[] = {30,35,15,5,10,20,25};
int len = sizeof(p)/sizeof(p[0]);
//void findtrace(int[][len]s, int i, int j);
int m[len][len];
int s[len][len];
printf("len p = %d\n",len);
for(int i=1;i<=len;i++)
m[i][i] = 0;
int temp;
for(int k=1;k<len;++k){
for(int j=k+1;j<len;++j){
int i = j-k;
m[i][j] = m[i][i]+m[i+1][j]+p[i]*p[i-1]*p[j];
s[i][j] = i;
for(int t=i+1;t<j;++t){
if( m[i][t]+m[t+1][j]+p[i-1]*p[j]*p[t]<m[i][j] )
m[i][j] = m[i][t]+m[t+1][j]+p[i-1]*p[j]*p[t];
s[i][j] = t;
}
//printf("m[%d][%d] = %d\t",i,j,m[i][j]);
}
}
printf("\n%d\n",m[1][len-1]);
findtrace(1,len-1,s);
}