0

I was trying to solve longest common subsequence problem . I am facing problem in integer array to pointer conversion . Last element of my 2D array will be result . Here is my code :

#include<iostream>
#include<cstring>
using namespace std;

char firstSequence_X[999999],secondSequence_Y[999999];
int length_M,length_N;


void LCS_LENGTH(char*,char*);

int main(void)
{
    LCS_LENGTH(firstSequence_X,secondSequence_Y);
    return 0;

}

void LCS_LENGTH(char X[],char Y[])
{
    //scan
    cout<<"Enter first sequence : ";
    cin>>X;
    length_M=strlen(X);
    cout<<"Enter second sequence : ";
    cin>>Y;
    length_N=strlen(Y);
    cout<<endl;
    cout<<"First sequence's length is : "<<length_M<<endl;
    cout<<"Second sequence's length is : "<<length_N<<endl;
    //declare 2 matrix : C for length , B for sign
    int C[length_M+1][length_N+1],B[length_M+1][length_N+1];
    //loop variable
    int i,j;
    //clear all
    for(i=0;i<=length_M;i++)
    {
        C[i][0]=0;
        B[i][0]='n';
    }
    for(j=0;j<=length_N;j++)
    {
        C[0][j]=0;
        B[0][j]='n';
    }
    //dp
    for(i=1;i<=length_M;i++)
    {
    for(j=1;j<=length_N;j++)
    {
        if(X[i]==Y[j])
        {
            C[i][j]=C[i-1,j-1]+1;//diagonal+1
            B[i][j]='d';
        }
        else if(C[i-1][j]>=C[i][j-1])
        {
            C[i][j]=C[i-1,j];//up row copy
            B[i][j]='u';
        }
        else
        {
            C[i][j]=C[i-1,j];//left column copy
            B[i][j]='l';
        }
    }
}
cout<<endl<<"C[i,j] :"<<endl;
for(i=0;i<=length_M;i++)
{
    for(j=0;j<=length_N;j++)
    {
        cout<<C[i][j]<<" ";
    }
    cout<<endl;
}
cout<<endl<<"B[i,j] :"<<endl;
for(i=0;i<=length_M;i++)
{
    for(j=0;j<=length_N;j++)
    {
        cout<<B[i][j]<<" ";
    }
    cout<<endl;
}
}

I have to print B and C array . But this error message is showed : invalid conversion from int* to int . Please correct my mistake . TIA

5
  • Where do to get the errors or warnings? Is it the complete error/warning output by the compiler? Please edit your question to show some marker in the source to show where you get the errors or warnings, and also to include the complete, verbatim compiler messages. Commented Aug 4, 2016 at 11:26
  • yes it is complete error . imgur.com/EQ39IcG Commented Aug 4, 2016 at 11:28
  • 1
    Also, C++ doesn't have variable-length arrays so your code is technically not valid C++. Some compiler have it as an extension, but don't rely on it. And the stack (where things like local variables, including arrays) are stored is limited, having a large array on the stack might exhaust the stack space. I recommend you use std::string and std::vector instead of old C-style strings and arrays. Commented Aug 4, 2016 at 11:28
  • 1
    Lastly, remember that for a C-style string the value returned by strlen() will give the index to the terminating '\0' character. That means your loops all go one step to far. Commented Aug 4, 2016 at 11:31
  • thanks , I will follow these suggestion Commented Aug 4, 2016 at 11:33

1 Answer 1

3

Looks like you are doing the following things wrong.

C[i][j]=C[i-1,j-1]+1;//diagonal+1

i think this should be

C[i][j]=C[i-1][j-1]+1;//diagonal+1

and

 C[i][j]=C[i-1,j];//up row copy

should be

 C[i][j]=C[i-1][j];//up row copy

and

 C[i][j]=C[i-1,j];//left column copy

should be

 C[i][j]=C[i-1][j];//left column copy
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.