f******5 发帖数: 11 | 1 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; i
c[i][0]=0;
}
for (int j=0; j
c[0][j]=0;
}
for(int i=0; i
for(int j=0; j
c[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<<"Count of longest Common subsequence from: "<
for (int i=0;i
for (int j=0;j
{
cout<
}
cout<
}
cout<<"Longest common : "<
for (int i=0;i
for (int j=0;j
{
cout<
}
cout<
}
Print_LCS(sequence_1, b[length_2+1], length_1+1, length_2+1);
} | a***y 发帖数: 2803 | 2 compile一下,根据错误改正就好了.
1. new了以后记得delete
2.void Print_LCS(char* sequence_1, char b[][length_2+1], int i, int j)
{
if (i==0|| j==0)
return;
這個函数是void的,为什么要return呢?
time
)
Helping
【在 f******5 的大作中提到】 : 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[][]
| M**********n 发帖数: 432 | 3 return can be used in any function. For void function you just don't have to
return anything.
【在 a***y 的大作中提到】 : compile一下,根据错误改正就好了. : 1. new了以后记得delete : 2.void Print_LCS(char* sequence_1, char b[][length_2+1], int i, int j) : { : if (i==0|| j==0) : return; : 這個函数是void的,为什么要return呢? : : time : )
| a***y 发帖数: 2803 | 4 如果是我写lz那段函数,不会用return.
to
【在 M**********n 的大作中提到】 : return can be used in any function. For void function you just don't have to : return anything.
| r*******y 发帖数: 1081 | 5 sometimes you have to use return even for a void return function
【在 a***y 的大作中提到】 : 如果是我写lz那段函数,不会用return. : : to
| p***o 发帖数: 1252 | 6 那你用啥?
【在 a***y 的大作中提到】 : 如果是我写lz那段函数,不会用return. : : to
| a***y 发帖数: 2803 | 7 void Print_LCS(char* sequence_1, char b[][length_2+1], int i, int j)
{
if (! (i==0|| j==0)) {
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);
}
}
【在 p***o 的大作中提到】 : 那你用啥?
| p***o 发帖数: 1252 | 8 栈太深,记不住。
【在 a***y 的大作中提到】 : void Print_LCS(char* sequence_1, char b[][length_2+1], int i, int j) : { : if (! (i==0|| j==0)) { : 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); : }
|
|