Error when comparing matrices

Asked

Viewed 79 times

0

The question asks for a comparison of matrices,I tried to compare them, but the answer if they are equal appears several times and I do not know how to solve this problem, is what is causing the 10% error that Ri is informing.

Here the link to the question

My code:

#include <stdio.h>

int i, j, teste, a;
int main()
{
  int matriz[9][9] = {{1, 3, 2, 5, 7, 9, 4, 6, 8},
                        {4, 9, 8, 2, 6, 1, 3, 7, 5},
                        {7, 5, 6, 3, 8, 4, 2, 1, 9},
                        {6, 4, 3, 1, 5, 8, 7, 9, 2},
                        {5, 2, 1, 7, 9, 3, 8, 4, 6},
                        {9, 8, 7, 4, 2, 6, 5, 3, 1},
                        {2, 1, 4, 9, 3, 5, 6, 8, 7},
                        {3, 6, 5, 8, 1, 7, 9, 2, 4},
                        {8, 7, 9, 6, 4, 2, 1, 5, 3}
                    }, sudoku[9][9];

    scanf("%d", &teste);
    for(a = 0; a < teste; a++)
    {
        for(i = 0; i < 9; i++)
        {
            for(j = 0; j < 9; j++)
            {
                scanf("%d", &sudoku[i][j]);
            }
        }

        for(i = 0; i < 9; i++)
        {
            for(j = 0; j < 9; j++)
            {
                if(matriz[i][j] != sudoku[i][j])
                {
                    printf("Instancia %d\n", a + 1);
                    printf("NAO\n\n");
                }
                else
                {
                    printf("Instancia %d\n", a + 1);
                    printf("SIM\n\n");
                }
            }
        }
    }

    return 0;
}
  • That is not what the question calls for. From the statement: "In the second line, your program should print "YES" if the matrix is the solution of a Sudoku problem, and "NO" otherwise"

  • look I did so I created an array that contains the correct answer,as soon as the user type,I will compare what the user typed with the correct answer,if the matrices are the same will get yes, otherwise get no other solution to this problem,?

  • Give a better read on C Matrices, and avoid using more than 2 for, use functions, because your code is ineligible.

1 answer

0

The answer is shown several times because it is inside the loop, you should switch to store the result in a flag and ask outside the loop (second group of loops), like this:

//  Continua no laço enquanto encontrar valores iguais.
bool bIguais = true;
for(i = 0; i < 9 && bIguais; i++)
{
    for(j = 0; j < 9 && bIguais; j++)
    {
        bIguais = matriz[i][j] != sudoku[i][j];
    }
}

if(bIguais)
{
    printf("Instancia %d\n", a + 1);
    printf("NAO\n\n");
}
else
{
    printf("Instancia %d\n", a + 1);
    printf("SIM\n\n");
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.