I'm working on a longest common subsequence program and for some reason my array keeps being filled with garbage even after I init everything inside of it to NULL.
#include "main.h"
int main()
{
//Provided char arrays containing sequences
char X[] = { ' ', 's', 'k', 'u', 'l', 'l', 'a', 'n', 'd', 'c', 'r', 'o', 's', 's', 'b', 'o', 'n', 'e', 's' };
char Y[] = { ' ', 'l', 'u', 'l', 'l', 'a', 'b', 'i', 'e', 's', 'f', 'o', 'r', 'b', 'a', 'b', 'i', 'e', 's' };
//Char array that will contain the directions
//for the longest subsequence
char b[ARRAY_ROW][ARRAY_COL];
int c[ARRAY_ROW][ARRAY_COL];
//Envoking LCS function
LongestCommonSubsequence(X, Y, b, c);
int row = ARRAY_ROW;
int col = ARRAY_COL;
//Envoking traverse function
Traverse(b, X, row, col);
cout << "END PHASE" << endl;
return 0;
}
void LongestCommonSubsequence(char x[], char y[], char b[ARRAY_ROW][ARRAY_COL], int c[ARRAY_ROW][ARRAY_COL])
{
//hardcoded length look back here
int m = ARRAY_ROW - 1;
int n = ARRAY_COL - 1;
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
c[i][j] = 0;
b[i][j] = 0;
}
}
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (x[i] == y[j])
{
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] = '-';
}
}
}
return;
}
void Traverse(char b[][ARRAY_COL], char x[], int i, int j)
{
if (i == 0 || j == 0)
return;
if (b[i][j] == '\\')
{
Traverse(b, x, i - 1, j - 1);
cout << x[i];
}
else if (b[i][j] == '|')
{
Traverse(b, x, i - 1, j);
}
else
{
Traverse(b, x, i, j - 1);
}
return;
}
in my LongestCommonSubsequence function the first thing I do is init the 2D array to NULL with hard coded sizes. However after I init the array it is still filled with garbage. So when I hit my Traverse function none of the if statements get hit because it never equals those characters.