Does not take or print the declared array C

1

my problem is the following, I do not know why I do not take the matrix as I declare 3x3 take me and print as if it were a common arrangement, I hope you can help me. Thank you very much!

int main() {

    int mat [3][3];
    int i, x;

    printf("Ingresa el contenido de la matriz: \n");


    for(i=0; i<3; i++) {
        for(x=0; i<3;i++) {

            scanf("%d", &mat[i][x]);

        }
    }

    for(int i=0; i<3; i++) {
        for(int x=0; i<3;i++) {
            printf("%d \t",mat[i][x]);

        }
        printf("\n");
    }

    return 0;
}
    
asked by Josias99 28.06.2017 в 18:55
source

1 answer

1

First of all, your mistake is that in the second for, you compare the first variable and increase it, causing an erroneous behavior.

It should be mentioned that in a for you can not declare and set a variable, ANSI C does not allow it, you must first define it and initialize it in the for.

#include <stdio.h>
int main() 
{
    int i,j, matriz[3][3]; // ANSI C impide declarar variables dentro de un for

    puts("Ingresa el contenido de la matriz");    

    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) { // nota que se debe incrementar j y no i
            scanf("%d", &matriz[i][j]);
        }
    }

    for(i = 0; i < 3; i++) {
        for(j = 0; j < 3; j++) { // lo mismo, se incrementa j y no i
            printf("%d \t",matriz[i][j]);
        }
        puts("");
    }

    return 0;
}
    
answered by 28.06.2017 / 19:05
source