Repeat number check on matrix in C

Asked

Viewed 3,022 times

0

I am not able to find the error of this code. I did it to check if in a 4x4 matrix the number being inserted already exists. For some reason that I do not know he accepts the times repeated values 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;
}
  • Has already drawn up a Table test of your program?

  • Yes... I just discovered that it is not entering the for(ii=0; ii<i; ii++), actually sometimes it enters, sometimes not... Now I’m trying to understand this hahaha

2 answers

1

The problem is that the bond of ii must be <=, because you need to check when it’s 0 too, you can’t skip row and column at the same time. I did the right thing and to simplify separated into 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");
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you prefer with goto, but I wouldn’t go from flag, is very confusing, moves in state:

#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");
    }
}
  • Thanks for the help! I tried to do without using function, but after I saw your code I realized that there is no way to solve without function.

  • I made my code based on yours and found that in fact, nothing has changed, keeps giving the same bug. I tested your code too and it gives the bug in the same place... when it is in element 10 of the matrix, it accepts equal numbers, because the test is does not enter... Monday night if my teacher resolves, I bring the answers here, anyway thank you all. :)

  • @Roninf.Matsumoto has no problem: https://ideone.com/jwZD2l

  • every time you place a repeated value on element a1a0 (column 1 line), the program is accepting existing numbers, because it is not entering the test repetition structure, this is happening because when it is in element 10 and the comparison is l < j, this returns a false and does not enter the for code block, in this case I am 0 and j is 1, so my problem continues, because every time you put an existing number, it will be accepted, look at this picture please. https://photos.app.goo.gl/ZeIFjSMujfzCOEHa2

0


This was a solution that a friend helped create. Thank you to those 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;
}

Browser other questions tagged

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