-
-
Save parzibyte/4405ab8730bf865e9b7ba7a2eebdb55b 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
#include <iostream> | |
#include <string> | |
// Definir prototipo de la función | |
double promedioDeArreglo(int arreglo[], int longitudDeArreglo); | |
int main() { | |
// Definición del arreglo | |
int arreglo[] = {95, 100, 94, 97, 100}; | |
// Sacar su longitud, también se podría hacer dentro de la función | |
int longitudDeArreglo = sizeof(arreglo) / sizeof(arreglo[0]); | |
// Obtener e imprimir | |
double promedio = promedioDeArreglo(arreglo, longitudDeArreglo); | |
std::cout << "El promedio es " + std::to_string(promedio); | |
} | |
// Definir función | |
double promedioDeArreglo(int arreglo[], int longitudDeArreglo) { | |
// Vamos a acumular aquí la sumatoria | |
double sumatoria = 0; | |
for (int x = 0; x < longitudDeArreglo; x++) { | |
sumatoria += arreglo[x]; | |
} | |
// El promedio es la sumatoria dividida entre el número de elementos | |
double promedio = sumatoria / longitudDeArreglo; | |
return promedio; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment