2
Good morning, everyone! I’m doing a program to create and add two matrices. First the number of rows and columns is entered, then the values of the elements and then the matrices are summed. However in my code the elements are being repeated and I am not managing to change it. I am doing in C. Can anyone help me? Follows my code:
#include<stdio.h>
int main (){
int n, m;
int mat[n][m],i,j;
int mat2[n][m];
printf ("Digite o tamanho das matrizes (n x m)\n");
printf ("Tamanho da coluna (n): ");
scanf ("%d", &n);
printf ("\nTamanho da linha (m): ");
scanf ("%d", &m);
if (n <=100 && m <= 100){
printf ("\nDigite um por um os valores da matriz 1\n\n");
for ( i=0; i<n; i++ ) {
for ( j=0; j<m; j++ )
{
printf ("\nElemento[%d][%d] = ", i, j);
scanf ("%d", &mat[i][j]);
}
}
printf ("\nDigite um por um os valores da matriz 2\n\n");
for ( i=0; i<n; i++ ) {
for ( j=0; j<m; j++ )
{
printf ("\nElemento[%d][%d] = ", i, j);
scanf ("%d", &mat2[i][j]);
}
}
printf ("\n---------------saida---------------------\n");
printf ("\n--------------matriz 1-------------------\n");
for ( i=0; i<n; i++ ) {
for ( j=0; j<m; j++ )
{
printf ("\nElemento[%d][%d] = %d\n", i, j,mat[i][j]);
}
}
printf ("\n--------------matriz 2-------------------\n");
for ( i=0; i<n; i++ ) {
for ( j=0; j<m; j++ )
{
printf ("\nElemento[%d][%d] = %d\n", i, j,mat2[i][j]);
}
}
printf ("\n--------multiplicação das matrizes----------\n");
for (i=0; i<n; i++) {
for (j=0; j<m; j++){
printf("\nElemento[%d][%d] = %d\n", i, j,mat[i][j]+mat2[i][j]);
}
}
} else {
printf ("\nMatriz muito grande, tamanho máximo 100 x 100\n");
}
return(0);
}
thank you very much!
– Thiago de Oliveira Magdalena