0

i am going to create a needleman-wunsch global sequence alignment. But my answer is wrong. Please help me check my code. Sometimes when two sequence match, but it will still runs the mismatch function. Ignore my poor english , thanks.

#include <iostream>
using namespace std;

int max(int a , int b , int c ) ; // declaration

int main (){

  string A = "ABBC" ;
  string B = "CBAC" ;
  int a = 3 ; // match
  int b = -1 ; // mismatch
  int c = -2 ; // gap
  int i , j ;

    // calculate the string size
    int m = A.size();
    int n = B.size();

    //construct a table
    int M[m+1][n+1] = {0} ;

    //insert first row and column with gap
    for (i=0; i<=m ;i++){
        M[i][0]=i*c;
    }
    for (j=0; j<=n ;j++){
        M[0][j]=j*c;
    }

  //calculate the whole matrix
    for (j=1; j<=n ; j++){
        for (i=1 ; i<=m ; i++){
            if (A[i-1] == B[j-1]){
                M[i][j] = M[i-1][j-1] + a ;
            }else{
                M[i][j] = max(M[i-1][j-1]+b ,
                              M[i-1][j]+c ,
                              M[i][j-1]+c);
            }
        }
    }
        //PRINT
    cout << "        ";
    for(  i = 0; i <= m; i++ ){
        cout << A[i] << "   ";
    }
    cout << "\n  ";

    for(  i = 0; i <= n; i++ ){
        if( i > 0 ){
            cout << B[i-1] << " ";
       }

        for(  j = 0; j <= m; j++ ){
            cout.width( 3 );
            cout << M[i][j] << " ";
        }
        cout << endl;
    }
}

// function for maximum value
int max(int a , int b , int c ){
    if(a>b && a>c){
        return a ;
    }else if(b>a && b>c){
        return b ;
    }else {
        return c ;
    }
}

and here is my output (the highlighted is the wrong answer) :

enter image description here

derk
  • 43
  • 3
  • 2
    Please post code, errors, sample data or textual output here as plain-text, not as images that can be hard to read, can’t be copy-pasted to help test code or use in answers, and are barrier to those who depend on screen readers. You can edit your question to add the code in the body of your question. For easy formatting use the `{}` button to mark blocks of code, or indent with four spaces for the same effect. The contents of a **screenshot can’t be searched, run as code, or copied and edited to create a solution.** – tadman May 19 '20 at 07:28
  • 1
    Did run your code in a **debugger** to see where that error occurs, then run it again with a breakpoint near that failure so you can step carefully ahead and watch what happens leading up to that point? – tadman May 19 '20 at 07:28

0 Answers0