0

I'm making a code to find the longest common subsequence between two strings, which creates a matrix for this purpose. The idea is that for each position [i][j], you define the value of that position as the greatest sum(or path) until that way and add 1 if the elements in the matrixes match. However, when I run this code it returns a matrix with "NULL" at every position. Could someone clarify me on that? Thanks in advance!

//Part 1
//Implements the LCS (Longest common subsequence) algorithm
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main()
{
    //Initializes two DNA strings to use for trial
    char string_1[]="ATTCTTTAGCAGGCAGGCAGGTGGCAGGTGACGATGGGGATGGAAAAG";
    char string_2[]="ATACTTTAGCATGCGGGCAGGAGGCGAGACGATGTCGGTATGAATG";

    //We want a matrix [m+1][n+1], where string_1[m] and string_2[n]
    //because m = strlen(string_1) + 1 and n = strlen(string_2) +1
    //We create a matrix [strlen(string_1) +2][strlen(string_2)+2]
    int m = strlen(string_1)+2;
    int n = strlen(string_2)+2;
    char lcs_matrix[m][n];

    //We initialize the lcs_matrix
    for (int i=0; i<m; i++)
    {
        for (int j=0; j<n; j++)
        {
            //For all the elements in the first column and line we set the value equal to zero
            if ((i==0)||(j==0))
            {
                lcs_matrix[i][j] = 0;
            }
            //If the elements of the strings match (are equal), we define the value of the 
            //[i][j] position as the previous [i-1][j-1] position + 1
            else if (string_1[i-1] == string_2[j-1])
            {
                lcs_matrix[i][j] = lcs_matrix[i-1][j-1] + 1;
            }
            //If the elements don't match, we define the value of the position 
            //[i][j] to be the value of the greatest of the previous values horizontally or vertically
            else
            {
                if (lcs_matrix[i-1][j] >= lcs_matrix[i][j-1])
                {
                    lcs_matrix[i][j] = lcs_matrix[i-1][j];
                }
                else
                {
                    lcs_matrix[i][j] = lcs_matrix[i][j-1];
                }
            }


        }

    }
    //I print the final matrix
    for (int i=0; i<m; i++)
    {
        for (int j=0; j<n; j++)
        {
            printf("%s ", lcs_matrix[i][j]);
        }
        printf("\n");
    }
    system("pause");
    return 0;
}
francescobfc
  • 107
  • 1
  • 11
  • 2
    Change `prinf("%s ", lcs_matrix[i][j]);` to `prinf("%d ", lcs_matrix[i][j]);` – AliA Oct 29 '19 at 01:51
  • 1
    The posted code does not cleanly compile! Some of the warnings from the compiler are 'critical'. When compiling, always enable the warnings, then fix those warnings. ( for `gcc`, at a minimum, use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` ) Note: other compilers use different options to produce the same results – user3629249 Oct 29 '19 at 03:33
  • 1
    regarding: `int m = strlen(string_1)+2;` and `int n = strlen(string_2)+2;` The function `strlen()` returns a `size_t`, (typically unsigned long int) not an `int`. The compiler will make an effort to work around such problems in the code. However, often the workaround results in code that does not do the desired operation. – user3629249 Oct 29 '19 at 03:36
  • Thanks for the answers! I've found also another error, that is, I had created a char matrix, not a int matrix, and as AliA has pointed, I had to print an integer, not a char. If I could ask you, how do I make my compiler advise about this warnings? I'm using codeblocks. About the size_t problem, what do I have to fix in my code, specifically? I've run it after changing the matrix to int and it appears to work. – francescobfc Nov 02 '19 at 22:54

0 Answers0