Make a program that reads a matrix and adds its elements

1

My brother was sent to do the exercise mentioned in the title, he did it but he does not get the sum, but only returns the entered data;

#include<stdio.h>

#define MAXFIL 20
#define MAXCOL 30

void leermatriz( int a[][MAXCOL], int nfilas, int ncols );
void sumamatriz( int a[][MAXCOL], int nfilas, int ncols );
void resultado( int a[][MAXCOL], int nfilas, int ncols );

main( ) {
  int nfilas, ncols;
  int a[MAXFIL][MAXCOL];

  printf( "\ncantidad de filas de la matriz" );
  scanf( "\n%d", &nfilas );
  printf( "\ncantidad de columnas de la matriz" );
  scanf( "\n%d", &ncols );
  printf( "\ndigite la matriz" );

  leermatriz( a, nfilas, ncols );
  sumamatriz( a, nfilas, ncols );
  printf( "\n\nsumas todos sus elementos:\n\n" );
  resultado( a, nfilas, ncols );
}

void leermatriz( int a[][MAXCOL], int m, int n ) {
  int fila, col;

  for( fila=0; fila <m; ++fila ) {
    printf( "\ndatos de fila n° %2d\n", fila + 1 );
    for( col=0; col<n; ++col ) {
      printf( "\nlos elementos %2d\n", col + 1 );
      scanf( "%d", &a[fila][col] );
    }
  }

  return;
}

void sumamatriz( int a[][MAXCOL],int m, int n ) {
  int fila, col;

  for(fila=0; fila<m; ++fila)
    for(col=0; col<n; ++col)
      a[fila][col];

  return;
}

void resultado( int a[][MAXCOL], int m, int n ) {
  int fila, col;

  for( fila = 0; fila < m; ++fila ) {
    for( col=0; col<n; ++col )
      printf( "%4d", a[fila][col] );

    printf( "\n" );
  }

  return;
}

I spent a good part trying to find the fault and in the end it could not, I would appreciate it if you could tell me what is wrong so that it does not give you the desired result (add correctly in a matrix)

    
asked by Kevinrealk 06.10.2018 в 08:16
source

2 answers

1

The error is here:

void sumamatriz( int a[][MAXCOL],int m, int n ) {
  int fila, col;

  for(fila=0; fila<m; ++fila)
    for(col=0; col<n; ++col)
      a[fila][col];

  return;
}

Obviating the fact that it does not add anything and that also, that function does not do anything and it is bad where you see it.

As it is a void function, without return, it does not go:

return;

And, in the event that the function returned something, it would have to be something like:

return algo;

The two for loops and that thing of a[fila][col] is completely useless, that is, I think you have to rethink the problem, first solve it in some kind of pseudocode and then pass it to C, because if you do not understand what you want to get, you can not start programming.

    
answered by 06.10.2018 в 09:57
0

Reviewing your code I see that you are not adding and accumulating your variable. Imagine that you have a matrix [1 2; 2 3], to add it up you have to do (in indexical notation)

m11 + m12 + m21 + m22 = sum

If you check the code of your function a[fila][col]; and the same happens in the statement return; where you are not returning any value.

Taking your code you simply need to add and accumulate:

int suma = 0; //variable donde acumularemos, NO OLVIDAR INICIALIZAR EN CERO
//para no obtener valores raros o inesperados
for(fila=0; fila<m; ++fila)
{
for(col=0; col<n; ++col)
  {
    suma = suma + a[fila][col];
  }
}

A recommendation, use the {} in the definition of the loops, however much you have a single sentence, helps make the code more beautiful and readable. Many luck!

    
answered by 19.10.2018 в 02:32