Thanks so much.
The compiler I am using is Dev c++ 4.9.9.2
Can I use New operator inside LCS like follows? It shows more compiling time
error?
Or should I declare c[][] outside the helping function LCS or inside Main()
function?
I want to pass the arrayc[][] and b[][] at the end of LCS to another Helping
function called
Print_LCS.
From your point of view, where should I declare the c[][] and b[][]
If i use New operator as follows to declare and initialize c[][], c[][],
both c[][] and b[][] go out of scope when LCS returns?
How could I access the data inside c[][] and b[][] then when outside LCS at
all?
#include
#include
using namespace std;
const int length_1=5;
const int length_2=9;
void LCS(char* sequence_1, char* sequence_2, int length_1, int length_2);
void Print_LCS(char* sequence_1, char b[][length_2+1], int i, int j);
void Print_LCS(char* sequence_1, char b[][length_2+1], int i, int j)
{
if (i==0|| j==0)
return;
if( b[i][j]=='\'){
Print_LCS(sequence_1, b, i-1, j-1);
cout<}
else if ( b[i][j]=='|'){
Print_LCS(sequence_1, b, i-1, j);
}
else
Print_LCS(sequence_1, b, i, j-1);
}
int main(int argc, char * argv[]){
char sequence_1[]="agcga";
char sequence_2[]="cagatagag";
LCS(sequence_1,sequence_2,length_1,length_2);
system("pause");
return 0;
}
void LCS(char* sequence_1, char* sequence_2, int length_1, int length_2)
{
//int c[length_1+1][length_2+1];
int *c;
c=new int[length_1+1][length_2+1];
char *b;
b=new char[length_1+1][length_2+1];
// char b[length_1+1][length_2+1];
for (int i=0; ic[i][0]=0;
}
for (int j=0; jc[0][j]=0;
}
for(int i=0; ifor(int j=0; jc[i][j]=0;
b[i][j]='0';
}
for (int i=1; i{
for (int j=1; j{
if(sequence_1[i-1] == sequence_2[j-1])
{
c[i][j]=c[i-1][j-1]+1;
b[i][j]='\';
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
b[i][j]='|';
}
else
{
c[i][j]=c[i][j-1];
b[i][j]='-';
}
} //end of double for loop
}
cout<for (int i=0;ifor (int j=0;j{
cout<
}
cout<}
cout<for (int i=0;ifor (int j=0;j{
cout<
}
cout<}
Print_LCS(sequence_1, b[length_2+1], length_1+1, length_2+1);
}