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) :