C matrix showing large numbers and not typed

Asked

Viewed 170 times

1

I’m having a problem in at least all my matrices made in class, I even caught a fixed problem of the internet and my matrix on PC is still showing random numbers.

Resultado

the code is as follows: :

void main (void){

int matriz[3][3], i, j, rot[3][3];

    for(i=0;i<3;i++) {
        for(j=0;j<3;j++) {
            printf("Digite o valor da matriz[%i][%i]: ",i+1, j+1);
            scanf("%i", &matriz[j]);
        }
    }
    //mostrando a matriz inicial
    for(i=0;i<3;i++) {
    printf("\n");
        for(j=0;j<3;j++) {
            printf(" %i ", matriz[j]);
        }
    }
    //rotacionando a matriz
    for(i=0;i<3;i++) {
        if(i==0){
            for(j=0;j<3;j++){
                rot[j][2]=matriz[j];
            }
        }
        if(i==1){
            for(j=0;j<3;j++){
                rot[j][1]=matriz[j];
            }
        }
        if(i==2){
            for(j=0;j<3;j++){
                rot[j][0]=matriz[j];
            }
        }
    }
    //mostrando a matriz rotacionada
    printf("\nMATRIZ 90º\n");

    for(i=0;i<3;i++) {
    printf("\n");
        for(j=0;j<3;j++) {
            printf(" %i ", rot[j]);
        }
    }
}

1 answer

2


The biggest problem is that it is manipulating a matrix as a vector, it simply abandoned the columns, if it is a matrix at all times it has to have two indexes, one of row and the other of column, and not having two will pick up garbage. The code is too confusing and complex.

#include <stdio.h>

int main (void) {
    int matriz[3][3], rot[3][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            printf("Digite o valor da matriz[%d][%d]: ", i + 1, j + 1);
            scanf("%d", &matriz[i][j]);
        }
    }
    for (int i = 0; i < 3; i++) {
        printf("\n");
        for (int j = 0; j < 3; j++) printf(" %d ", matriz[i][j]);
    }
    for (int i = 0; i < 3; i++) for (int j = 2; j >= 0; j--) rot[i][j] = matriz[j][2 - i];
    printf("\nMATRIZ 90º");
    for (int i = 0; i < 3; i++) {
        printf("\n");
        for (int j = 0; j < 3; j++) printf(" %d ", rot[i][j]);
    }
}

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

Browser other questions tagged

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