Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created October 29, 2019 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/aeb7df89c643334d0ba5350b7e827d84 to your computer and use it in GitHub Desktop.
Save parzibyte/aeb7df89c643334d0ba5350b7e827d84 to your computer and use it in GitHub Desktop.
/**
* Sumatoria y promedio de matriz en C
*
* @author parzibyte
* https://parzibyte.me/blog
*
* */
#define ALTURA 3
#define ANCHURA 3
#include <stdio.h> // Para printf
int main() {
int matriz[ALTURA][ANCHURA] = {
{20, 50, 80},
{500, 12, 44},
{56, 4, 3},
};
// Necesitamos la sumatoria, al inicio es 0 y después vamos aumentándola
int sumatoria = 0;
int cantidadElementos = ALTURA * ANCHURA;
// Recorrer la matriz y sumar
for (int y = 0; y < ALTURA; y++) {
for (int x = 0; x < ANCHURA; x++) {
int elementoActual = matriz[y][x];
sumatoria = sumatoria + elementoActual;
}
}
// El promedio es = sumatoria / cantidadDeElementos
double promedio = (float) sumatoria / (float) cantidadElementos;
printf("Sumatoria: %d\n", sumatoria);
printf("Promedio: %0.2f\n", promedio);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment