不离开循环,不知道为什么[C ++]

时间:2015-02-25 03:00:15

标签: c++ for-loop

编辑:我发现了错误!该程序实际上并没有首先陷入循环。真的是它陷入了代码中的一个循环中,但是因为在这个循环结束时我的确认cout结束时没有换行符,所以它从未执行过。谢谢你的帮助!

我正在研究一种能够比对两个核苷酸序列的程序。它一直运作良好,直到这一点。循环执行到661,seq1length的大小为663,然后不离开循环。我将一个故障安全放入循环中,这样它就不会永远继续下去,但它似乎不起作用。我觉得我错过了一些愚蠢的东西,我真的很欣赏另一双眼睛。

  //calculate the matrix using the model. Figure out whether a up, left or diagonal move would be best
int CalcMatrix [seq1length][seq2length]; //this will store the cumulative alignment scores at each spot
int DirectionalMatrix [seq1length][seq2length] ; //this will store the direction of the alignment //0 is diagonal, 1 is up, 2 is left
int diag;
int up;
int left;
int incase = 0; //set up a fail safe in case it gets too big
while(incase < 2*(seq1length+seq2length)) // to make sure the loop doesn't go on forever. doesn't work
{
    for(int i = 0; i < seq1length-1; i++) //for the length of the first sequence
    {
        CalcMatrix[i][0] = i*gappenalty; //first row is a cumulative gap penalty
        DirectionalMatrix[i][0] = 2; //to the left

        for(int j = 0; j < seq2length-1; j++)
        {
            CalcMatrix[0][j] = j*gappenalty; //first column is a cumulative gap penalty
            DirectionalMatrix[0][j] = 1; // to the up
            if(i > 0 && j > 0) //if i and j are greater than 0, that is we aren't in the first row or column, make the rest of the matrix
            {
                diag = CalcMatrix[i-1][j-1] + ScoreMatrix[i][j];
                up = CalcMatrix[i-1][j] + gappenalty;
                left = CalcMatrix[i][j-1] + gappenalty;
                CalcMatrix[i][j] = max(diag, max(up,left));
                if(diag == CalcMatrix[i][j]) { DirectionalMatrix[i][j] = 0;}
                else if(up ==  CalcMatrix[i][j]) { DirectionalMatrix[i][j] = 1;}
                else if(left ==  CalcMatrix[i][j]) { DirectionalMatrix[i][j] = 2;}

                incase++;
            }
        }
        cout << "Calculating Alignment... Please Hold... " << i << " Nucleotides Aligned \n";
    }
}

cout << "Yay! You got out of the loop!";

修改

我制作了一个自编的可编辑版本。然而,这个确实成功地离开了循环。这意味着它与我在原始代码中使用的值的大小有关,或者是因为在原始代码中如何将值初始化得更高

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

int main(){
//build matrix of scores
    string seq1seq = "ACTGACTGACTGACTGACTACTG";
    string seq2seq = "ACTGTGTCTTCTGATCTCGATCCA";
    int seq1length = seq1seq.length();
    int seq2length = seq2seq.length();
int gappenalty = -40;
cout << "Sequence 1 length is: " << seq1length << "\n";
cout << "Sequence 2 length is: " << seq2length << "\n";
int incase = 0;
int a [21] = {0};

int ScoreMatrix[seq1length][seq2length];
for (int i = 0; i < seq1length; i++)
{
    for(int j = 0; j < seq2length; j++)
    {
        if(seq1seq[i] == 'A')
        {
            if(seq2seq[j] == 'A')
            {
                ScoreMatrix[0][0] = a[5];//the substitution score array at [0][0] ;
            }
            else if(seq2seq[j] == 'C')
            {
                ScoreMatrix[0][1] = a[6];//the substitution score array at [0][1] that is, An A in sequence 1 and a C in sequence 2
            }
            else if(seq2seq[j] == 'G')
            {
                ScoreMatrix[0][2] = a[7];//the substitution score array at [0][2]
            }
            else if(seq2seq[j] == 'T')
            {
                ScoreMatrix[0][3] = a[8];//the substitution score array at [0][3]
            }
        }
        if(seq1seq[i] == 'C')
        {
            if(seq2seq[j] == 'A')
            {
                ScoreMatrix[1][0] =  a[9];//the substitution score array at [1][0]
            }
            else if(seq2seq[j] == 'C')
            {
         ScoreMatrix[1][1] =  a[10];//the substitution score array at [1][1] that is, An C in sequence 1 and a C in sequence 2
            }
            else if(seq2seq[j] == 'G')
            {
                ScoreMatrix[1][2] =  a[11];//the substitution score array at [1][2]
            }
            else if(seq2seq[j] == 'T')
            {
                ScoreMatrix[1][3] =  a[12];//the substitution score array at [1][3
            }
        }
        if(seq1seq[i] == 'G')
        {
            if(seq2seq[j] == 'A')
            {
                ScoreMatrix[2][0] =  a[13];//the substitution score array at [2][0]
            }
            else if(seq2seq[j] == 'C')
            {
                ScoreMatrix[2][1] =  a[14];//the substitution score array at [2][1] that is, An G in sequence 1 and a C in sequence 2
            }
            else if(seq2seq[j] == 'G')
            {
                ScoreMatrix[2][2] =  a[15];//the substitution score array at [2][2]
            }
            else if(seq2seq[j] == 'T')
            {
                ScoreMatrix[2][3] =  a[16];//the substitution score array at [2][3]
            }
        }
        if(seq1seq[i] == 'A')
        {
            if(seq2seq[j] == 'A')
            {
                ScoreMatrix[3][0] =  a[17];//the substitution score array at [3][0]
            }
            else if(seq2seq[j] == 'C')
            {
                ScoreMatrix[3][1] =  a[18];//the substitution score array at [3][1] that is, An T in sequence 1 and a C in sequence 2
            }
            else if(seq2seq[j] == 'G')
            {
                ScoreMatrix[3][2] =  a[19];//the substitution score array at [3][2]
            }
            else if(seq2seq[j] == 'T')
            {
                ScoreMatrix[3][3] =  a[20];//the substitution score array at [3][3]
            }
        }

    }
cout << "Assembling Score Matrix.... Please Hold.... " << i << " Nucleotides Covered \n" ;
}
   cout << "YAY! Got out of the scoring assembly. Aren't you proud of me?" ;

//calculate the matrix using the silly named model. Figure out whether a up, left or diagonal move would be best
int CalcMatrix [seq1length][seq2length]; //this will store the cumulative alignment scores at each spot
int DirectionalMatrix [seq1length][seq2length] ; //this will store the direction of the alignment //0 is diagonal, 1 is up, 2 is left
int diag;
int up;
int left;
incase = 0; //set up a fail safe incase it gets too big
    for(int i = 0; i < seq1length; i++) //for the length of the first sequence
    {
        CalcMatrix[i][0] = i*gappenalty; //first row is a cumulative gap penalty
        DirectionalMatrix[i][0] = 2; //to the left

        for(int j = 0; j < seq2length; j++)
        {
            CalcMatrix[0][j] = j*gappenalty; //first column is a cumulative gap penalty
            DirectionalMatrix[0][j] = 1; // to the up
            if(i > 0 && j > 0) //if i and j are greater than 0, that is we aren't in the first row or column, make the rest of the matrix
            {
                diag = CalcMatrix[i-1][j-1] + ScoreMatrix[i][j];
                up = CalcMatrix[i-1][j] + gappenalty;
            left = CalcMatrix[i][j-1] + gappenalty;
                CalcMatrix[i][j] = max(diag, max(up,left));
                if(diag == CalcMatrix[i][j]) { DirectionalMatrix[i][j] = 0;}
                else if(up ==  CalcMatrix[i][j]) { DirectionalMatrix[i][j] = 1;}
                else if(left ==  CalcMatrix[i][j]) { DirectionalMatrix[i][j] = 2;}

            }
        }

        //cout << "Calculating Alignment... Please Hold... " << i << " Nucleotides Aligned \n";
}

cout << "Yay! You got out of the loop!";



return 0;}

2 个答案:

答案 0 :(得分:1)

我从你的代码中运行了这个简化的测试应用程序,似乎退出正常:

#include <iostream>
using namespace std;
#define seq1length 3
#define seq2length 3


int main(int argc, char *argv[])
{
    int incase = 0; //set up a fail safe incase it gets too big
    while (incase < 2 * (seq1length + seq2length)) // to make sure the loop doesn't go on forever. doesn't work
    {
        for (int i = 0; i < seq1length - 1; i++) //for the length of the first sequence
        {
            for (int j = 0; j < seq2length - 1; j++)
            {
                if (i > 0 && j > 0) //if i and j are greater than 0, that is we aren't in the first row or column, make the rest of the matrix
                {
                    incase++;
                }
            }
            cout << "Calculating Alignment... Please Hold... " << i << " Nucleotides Aligned \n";
        }
    }

    cout << "Yay! You got out of the loop!";
    getchar();
    return(0);
}

我删除了不依赖于循环条件检查的代码。你能告诉你一些关于你的问题吗...就像你为seq1length和seq2length传递的值...

这是我看到的输出:

enter image description here

编辑:

在您的评论中引用http://ideone.com/Zrvzpa后,我发现它已退出661,因为您的条件为i < seq1length-1;j < seq2length-1;

如果您将其更改为i < seq1length;j < seq2length;。它将循环到662。

请注意,它不应循环到663,因为数组是从 0 - 662 索引的。

答案 1 :(得分:1)

因为你把你的incase ++放在你的嵌套for循环的if语句中:

for(int i = 0; i < seq1length-1; i++){
//Your Code
  for(int j = 0; j < seq2length-1; j++){
    //Your Code
    incase++;
  }     
}

你最内层的嵌套for循环只循环直到小于seq2length-1,然后你的外部for循环只循环直到小于seq1length-1,它不能达到你的2 *(seq1length + seq2length)限制while循环,即使你的if语句总是被执行。

让我们说你的最内层for循环循环1次,你的外循环循环663次,总的来说你的incase值会增加到663 * 1 = 663.但你的while循环条件是2 *(664 + 2) ,这是1332.这永远不会让你的命中达到while循环的极限。

另外,你把你的incase ++放在if语句中,直到你的for循环i和j都大于0才会执行,这可能会使你的incase增量总共少于663次(这解释了)为什么你的incase已增加到只有661)并且它在那里停止,没有达到你的while循环限制。例如,当你的i或j为0时,你的for循环即使是循环,但你的incase不会增加因为i和j都不大于0(if语句条件)。

你可以把你的incase ++放在for循环之外但是在while循环中并正确设置条件(取决于你的程序应该做什么),或者你的while循环条件需要正确设置,考虑到在你的i和j都大于0之前你的if语句不会增加,或者把它放在你的if语句之外,或者改变你的if语句条件。取决于你想做什么。