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);
}
5
  • Can you identify what is causing your first error? Do you know what that message means? Commented Jan 17, 2015 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. Commented Jan 17, 2015 at 19:26
  • 2
    Also, are you sure you're programming in C++? the only C++ specific thing in your code is the header <iostream> and using namespace std, everything else is just C (like using printf and variable-length arrays). Commented Jan 17, 2015 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. Commented Jan 17, 2015 at 19:35
  • @DrewDormann I am sorry I am a new C++ programmer and I am not pretty sure about those. Commented Jan 17, 2015 at 19:42

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Accept a right answer to close the question please. Both answers are correct.
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

7 Comments

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.
@JoachimPileborg I have edited.But 1st one works fine for me every time .
Yes, it can be successfully compiled but after running it, it get segmentation fault: len p = 7 15125 Segmentation fault: 11
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
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
|
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);
}

1 Comment

Thanks, the problem is indeed because of the recursion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.