Created
October 29, 2019 02:30
-
-
Save parzibyte/aeb7df89c643334d0ba5350b7e827d84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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