Print multiple matrices separately

Asked

Viewed 99 times

0

I wonder if you have how to print these three matrices without having to create several for's, the printing of the same on the screen should be separated. ( I need an alternative because I have little space and the code was very extensive when I was printed in the conventional way )

#include <stdio.h>
#include <locale.h>
#include <stdlib.h>

int main () { setlocale(LC_ALL,"Portuguese");

    int mat1[100][100], mat2[100][100], mat3[100][100], linha = 0, coluna = 0;

    // red, blue, green

    printf("\nInforme o número de linhas e colunas: ");
    scanf("%i %i",&linha,&coluna);

    for ( int i = 0; i < linha; i++){
        for ( int h = 0; h < coluna; h++){
            printf("\nInforme um digito para linha %i, coluna %i matriz red: ",i, h);
            scanf("%i",&mat1[i][h]);
        }
        system("cls");
        for ( int j = 0; j < coluna; j++){
            printf("\nInforme um digito para linha %i, coluna %i matriz blue: ",i, j);
            scanf("%i",&mat2[i][j]);
        }
        system("cls");
        for ( int q = 0; q < coluna; q++){
            printf("\nInforme um digito para linha %i, coluna %i matriz green: ",i,q);
            scanf("%i",&mat3[i][q]);
        }
        system("cls");
    }

    // imprimir toda a primeira, depois a segunda e por fim a terceira...

    return 0;
}

1 answer

1


If you want to print 3 or more, put them together.

int matriz[3][100][100];
int x,y,z;
for(x=0; x<3; x++)
    for(y=0; y<100; y++)
        for(z=0; z<100; z++;){
            printf("entre o valor da matriz %x, linha %d, coluna %d: ", z,y,z);
            scanf("%d", &matrz[x][y][z]);
         }

for(x=0; x<3; x++)
    for(y=0; y<100; y++)
        for(z=0; z<100; z++;)
            printf("matriz %d[%d,%d]: %d",x,y,z,matriz[x][y][z]);

The first for walks through the matrices (x == total of matrices) and the other 2 traverse rows and columns.

So you can go through as many matrices as you want, and make the code more organized;

Browser other questions tagged

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