1

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);
}
Deduplicator
  • 41,806
  • 6
  • 61
  • 104
Yun Li
  • 327
  • 4
  • 10
  • Can you identify what is causing your first error? Do you know what that message means? – Drew Dormann Jan 17 '15 at 19:26
  • 3
    When posting questions with code, *please* don't include line numbers. It makes it much harder to copy-paste the code. Instead mark the lines the question is about in some other way. – Some programmer dude Jan 17 '15 at 19:26
  • 2
    Also, are you *sure* you're programming in C++? the only C++ specific thing in your code is the header `` and `using namespace std`, everything else is just C (like using `printf` and variable-length arrays). – Some programmer dude Jan 17 '15 at 19:27
  • @JoachimPileborg Sorry for the confusion, I am actually writing in C++ but I am a learner and used some C grammars. Thanks. – Yun Li Jan 17 '15 at 19:35
  • @DrewDormann I am sorry I am a new C++ programmer and I am not pretty sure about those. – Yun Li Jan 17 '15 at 19:42

3 Answers3

1

The first error you have is due to a typo in your syntax for passing the array. It should be:

void findtrace(int i, int j, int s[][7])

not:

void findtrace(int i, int j, int[][7] s)

Also, take a look at this answer.

Community
  • 1
  • 1
Mustafa
  • 1,634
  • 3
  • 15
  • 24
0

The error is here

void findtrace(int i, int j, int[][7] s)

it should be

void findtrace(int i, int j, int *s[])
                                ^^^

and call it as

findtrace(1,len-1,(int **)s);

If compiler is C99 compatible

From C99, C language supports variable sized arrays to be passed simply by specifying the variable dimensions

void findtrace(int i, int j, int s[][7])

will be fine also

Ankur
  • 3,113
  • 1
  • 19
  • 32
  • 2
    *Almost* right, but your change will cause the function to expect an array of pointers, instead of a pointer to an array. Those two are *very* different. Also, an array of arrays is *not* the same as a pointer to pointer, so your cast is wrong too. – Some programmer dude Jan 17 '15 at 19:29
  • @JoachimPileborg I have edited.But 1st one works fine for me every time . – Ankur Jan 17 '15 at 19:37
  • Yes, it can be successfully compiled but after running it, it get segmentation fault: len p = 7 15125 Segmentation fault: 11 – Yun Li Jan 17 '15 at 19:39
  • Well i see that your array is having 7 elements and you are accessing `s[][7]` that will be invalid because in c index start from 0 – Ankur Jan 17 '15 at 19:42
  • Could you please tell me where do I access s[ ][7]? In the code, len = 7, but I pass len-1 to the function findtrace as j. Since the maximum of j is len-1 = 6, so I am still a little confulsed – Yun Li Jan 17 '15 at 19:54
  • @Shan See [this old answer of mine](http://stackoverflow.com/a/25156132/440558), those illustrations should tell you why a pointer to pointer and an array of arrays is not the same. As for the array of pointer versus pointer to array in your function declaration, the way you declare the argument `s` (as an array of pointers) is similar to the illustration of pointers to pointers, which is not correct. – Some programmer dude Jan 17 '15 at 19:58
  • @YunLi : It looks like that the problem is in your recursion.Try to debug corner cases of matrices. – Ankur Jan 17 '15 at 20:17
0

Problem seems to be in recursive function : Some sort of Access Volition because of some logical mistake

variable i saves the vales -858993460 so when it comes to printf("%d\t",s[i][j]); it gives access violation because such an index doesn't exist

another problem is const int len = sizeof(p)/sizeof(int); instead of using simple int,

a little bit refined form is here , but logic still need to be fixed

void findtrace(int i, int j, int s[][7])
{
    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};
    const int len = sizeof(p)/sizeof(int);
    //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);
}
Khurram Sharif
  • 474
  • 1
  • 4
  • 20