由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Please help me take a look at the program
相关主题
这个在c++ const不变,不能用相同地址的变量改,咋做的A aimple C++ question
[合集] 为什么10进制输出是负的?这个地址咋回事?
reverse words, not the Microsoft one!!!面试问题
c++ 得最基本问题请教一个简单字符串程序 (转载)
来,出个题为什么foo1可以而foo2不行?
question about shift菜鸟求教,一个c++的困惑
一个指向指针的指针的引用?C++ formatted output question
问个char*的问题function call?
相关话题的讨论汇总
话题: length话题: lcs话题: int话题: sequence话题: char
进入Programming版参与讨论
1 (共1页)
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);
: }

1 (共1页)
进入Programming版参与讨论
相关主题
function call?来,出个题
问个指针array 的简单问题question about shift
小白请教一个C++问题:问什么我不能把两个指针=起来?一个指向指针的指针的引用?
compare double to float问个char*的问题
这个在c++ const不变,不能用相同地址的变量改,咋做的A aimple C++ question
[合集] 为什么10进制输出是负的?这个地址咋回事?
reverse words, not the Microsoft one!!!面试问题
c++ 得最基本问题请教一个简单字符串程序 (转载)
相关话题的讨论汇总
话题: length话题: lcs话题: int话题: sequence话题: char