Problem creating 2 n x m matrices in C

Asked

Viewed 37 times

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);
}

1 answer

1


When you declare variables n and m, they end up having values, so your matrices end up having a size even before you ask the user the size of them.


See this example, just displaying the values of n and m without having scanf:

#include <stdio.h>

int main (){
    
    int n, m;

    printf("Valor de n: %d\n", n);
    printf("Valor de m: %d\n", m);

    return(0);
}

https://repl.it/repls/PutridUnripeAutomaticvectorization


One way to correct, would be to declare the matrices after obtaining the values of n and m:

int n, m, i, j;
  
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);  

int mat[n][m];
int mat2[n][m];

With this, your complete code would look more or less like this:

#include <stdio.h>

int main() {
    
    int n, m, i, j;
      
    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);  

    int mat[n][m];
    int mat2[n][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;
}

https://repl.it/repls/GrossExoticScale

Browser other questions tagged

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