Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created May 17, 2021 00:00
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/5a8215cdfe6ae5093427b53e00a59198 to your computer and use it in GitHub Desktop.
Save parzibyte/5a8215cdfe6ae5093427b53e00a59198 to your computer and use it in GitHub Desktop.
<?php
# https://parzibyte.me/blog
$notas = [];
$cantidadNotas = 5;
for ($i = 0; $i < $cantidadNotas; $i++) {
echo "Ingrese la nota #" . ($i + 1) . ": ";
fscanf(STDIN, "%f", $nota);
// Agregar al arreglo
$notas[] = $nota;
}
// Ordenamos el arreglo
for ($i = 0; $i < $cantidadNotas; $i++) {
for ($j = 0; $j < $cantidadNotas - 1; $j++) {
if ($notas[$j] > $notas[$j + 1]) {
$temporal = $notas[$j];
$notas[$j] = $notas[$j + 1];
$notas[$j + 1] = $temporal;
}
}
}
// Lo imprimimos y a la vez vamos calculando el promedio
$sumatoria = 0;
echo "Notas de manera ascendente: \n";
foreach ($notas as $nota) {
$sumatoria += $nota;
echo $nota . "\n";
}
$promedio = $sumatoria / $cantidadNotas;
echo "Promedio: " . $promedio;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment