noob noob - 4 months ago 15
C++ Question

Type conversion from pointer to array

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

Answer

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