Checking the number repeated in the matrix in C

0

I can not seem to find the error of this code. I made it to check if in a 4x4 array the number being inserted already exists. For some reason that I do not know he accepts repeated values sometimes and sometimes not ...

int main()
{
bool bExit = false;
int iMat[4][4], i = 0, j = 0, ii = 0, jj = 0, iValue = 0;

setlocale(LC_ALL, "Portuguese");

printf("Digite 16 números diferentes para completar a matriz 4x4: \n");

//Recebendo os valores da matriz...
for(i=0; i<4; i++)
{
    for(j=0; j<4; j++)
    {
        printf("%i%i -> ", i, j);
        if(i!=0 || j!=0)
        {
            //Verificando se são repetidos antes de salvar...
            do{
                scanf("%i", &iMat[i][j]);
                for(ii=0; ii<i; ii++)
                {
                    for(jj=0; jj<j; jj++)
                    {
                        if((iMat[i][j]) == (iMat[ii][jj]))
                        {
                            printf("\nNúmero repetido, digite outro: \n");
                            printf("%i%i -> ", i, j);
                            ii = 5;
                            jj = 5;
                            bExit = true;
                        }else{
                            bExit = false;
                        }
                    }
                }
            }while(bExit);
        }else{
            scanf("%i", &iMat[i][j]);
        }
    }
}

//Imprimindo a matriz...
printf("\n");
for(i=0; i<4; i++)
{
    for(j=0; j<4; j++)
    {
        printf("%i ", iMat[i][j]);
    }
    printf("\n");
}


return 0;
}
    
asked by anonymous 26.02.2018 / 00:34

2 answers

0

This was a solution that a friend helped create. Thank you guys who tried to help. :)

#include <tchar.h>
#include <stdio.h>
#include <stdbool.h>

int _tmain(int argc, _TCHAR* argv[])
{
bool bExit = false;
int iMat[4][4],
    i = 0,
    j = 0,
    ii = 0,
    jj = 0,
    contador = 0;

printf("Numero: \n");

for (i = 0; i < 4; i++)
{
    for (j = 0; j < 4; j++)
    {
        if (i != 0 || j != 0)
        {
            do
            {
                printf("%i %i -> ", i, j);
                scanf("%i", &iMat[i][j]);
                int temp = contador;
                bool numeroRepetido = false;

                for (ii = 0; ii < 4; ii++)
                {
                    for (jj = 0; jj < 4; jj++)
                    {
                        if (iMat[i][j] == iMat[ii][jj])
                        {
                            printf("Numero repetido em [%i,%i],contador=%i \n", ii,jj,contador);
                            ii = 5;
                            jj = 5;
                            bExit = true;
                            numeroRepetido = true;
                        }

                        temp--;

                        if (temp == 0)
                        {
                            ii = 5;
                            jj = 5;
                        }
                    }
                }

                if (!numeroRepetido)
                {
                    contador++;
                    bExit = false;
                }

            } while (bExit);
        }
        else
        {
            printf("%i %i -> ", i, j);
            scanf("%i", &iMat[i][j]);
            contador++;
        }
    }
}

// Imprime
printf("\n");
for (i = 0; i < 4; i++)
{
    for (j = 0; j < 4; j++)
    {
        printf("%2i ", iMat[i][j]);
    }
    printf("\n");
}

system("pause");

return 0;
}
    
03.03.2018 / 18:14
1

The problem is that the ii loop must be <= , because you need to check when it is 0 too, you can not skip row and column at the same time. I did the right thing and to simplify I separated in function to avoid the use of goto and flag that complicate the code:

#include <stdio.h>

int repetido(int iMat[4][4], int i, int j) {
    for (int k = 0; k <= i; k++) {
        for (int l = 0; l < j; l++) {
            if (iMat[i][j] == iMat[k][l]) {
                printf("\nNúmero repetido, digite outro: \n");
                return 1;
            }
        }
    }
    return 0;
}

int main() {
    int iMat[4][4];
    printf("Digite 16 números diferentes para completar a matriz 4x4:\n");
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            while (1) {
                printf("%i%i -> ", i, j);
                scanf("%i", &iMat[i][j]);
                if (!repetido(iMat, i, j)) break;
            }
        }
    }
    printf("\n");
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) printf("%2i ", iMat[i][j]);
        printf("\n");
    }
}

If you prefer with goto , but I would not go from flag , it is very confusing,

#include <stdio.h>

int repetido(int iMat[4][4], int i, int j) {
    for (int k = 0; k <= i; k++) {
        for (int l = 0; l < j; l++) {
            if (iMat[i][j] == iMat[k][l]) {
                printf("\nNúmero repetido, digite outro: \n");
                return 1;
            }
        }
    }
    return 0;
}

int main() {
    int iMat[4][4];
    printf("Digite 16 números diferentes para completar a matriz 4x4:\n");
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
continua:       while (1) {
                printf("%i%i -> ", i, j);
                scanf("%i", &iMat[i][j]);
                for (int k = 0; k <= i; k++) {
                    for (int l = 0; l < j; l++) {
                        if (iMat[i][j] == iMat[k][l]) {
                            printf("\nNúmero repetido, digite outro: \n");
                            goto continua;
                        }
                    }
                }
                goto fim;
            }
fim:    }
    }
    printf("\n");
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) printf("%2i ", iMat[i][j]);
        printf("\n");
    }
}
    
26.02.2018 / 01:43