0
I’m trying to fill a matrix recursively to get how many vector values are equal, but it’s not working and I don’t know where the problem is.
The matrix is generated by comparing 2 strings
If the values between vector 1[x] and vector 2[y] are equal to the matrix position[x][y] is given a value
If they are not, one of the matrix values is repeated
Follow the code I have:
int rec(char vet1[], char vet2[], int mat[][tam], int x, int y)
{
if(mat[x][y] != NULL ) \\ a matriz completa é preenchida com NULL inicialmente, seguida por primeira linha e coluna com 0. Desta forma a recursão pode parar.
{
return mat[x][y];
}
else
{
if(vet1[x] == vet2[y]) \\ compara 2 valores de um vetor de char, se forem iguais, preenche a posição da matriz com a posição anterior + 1
{
mat[x][y] = rec(vet1, vet2, mat, x-1, y-1) + 1;
}
else // mas se não forem iguais, repete o maior valor achado anteriormente na matriz
{
mat[x-1][y] = rec(vet1, vet2, mat, x-1, y);
mat[x][y-1] = rec(vet1, vet2, mat, x, y-1);
if(mat[x-1][y] < mat[x][y-1])
{
mat[x][y] = mat[x][y-1];
}
else
{
mat[x][y] = mat[x-1][y];
}
}
}
}
Post the code being executed.
– Igor Carreiro
Missing variables declaration, initialization, ...
mat[x][y] != NULL
, for example, it cannot be correct!!!– LS_ᴅᴇᴠ
This part of the code was actually causing problems, because the matrix understood that 0 was also null and I wasn’t getting it right. This way I didn’t make the exact calculation.
– Juliana Joyce Pires