Algorithm of ages of N students, obtain average and only show the student who is older than the average obtained

1

I just need to show the student's older age based on the average obtained

#include<stdio.h>
#include<iostream>
using namespace std;

int main()
{
    int n, edad;

    cout<<"Ingresa la cantidad de alumnos:  ";
    cin>>n;

    for(int i = 1; i<=n; i++)
    {
        cout<<"Edad de alumno "<< i <<" :  ";
        cin>>edad;
    }

    return 0;
}
    
asked by Francisco Cardenas 25.06.2017 в 06:31
source

2 answers

1

The problem is poorly formulated, or missing data: are we sure that, at the most, there will only be 1 a student above the average age?

To show all ages above average, we have no choice but to store them somewhere, and check them, a posteriori , one after the other. Otherwise, we can only know if a certain age is above to all those introduced so far . Which can rule out several ages that meet the condition of being above average .

#include <iostream>
using namespace std;

int main( ) {
  int *lista;
  int acu = 0, n, idx, edad;

  cout << "Ingrese la cantidad de alumnos: ";
  cin >> n;

  lista = new int[n];

  for( idx = 0; idx < n; ++idx ) {
    cout << "Edad del alumno "<< idx + 1 <<": ";
    cin >> edad;

    lista[idx] = edad;
    acu += edad;
  }

  acu /= n;

  cout << "Edad promedio: " << acu << "\n";
  cout << "Edades superiores al promedio:\n;

  for( idx = 0; idx < n; ++idx )
    if( lista[idx] > acu ) {
      cout << "alumno " << idx + 1 << ", edad " << lista[idx] << "\n";

  return 0;
}
    
answered by 25.06.2017 / 18:21
source
1

The code can be something like this:

#include<iostream>
using namespace std;

int main()
{
    int n, edad;
    double promedio=0, mayor=0, nAlumno=0;

    cout<<"Ingresa la cantidad de alumnos:  ";
    cin>>n;

    for(int i = 1; i<=n; i++)
    {
        cout<<"Edad de alumno "<< i <<" :  ";
        cin>>edad;

        if (edad >= mayor) {
            mayor = edad;
            nAlumno = i;
        }

        promedio += edad;
    }
    promedio /= n;

    cout<<"El alumno con mayor edad es: "<<nAlumno<<" con una edad de "<<mayor<<endl;
    cout<<"El promedio de edad es: "<<promedio;
    return 0;
}

I hope it helps.

    
answered by 25.06.2017 в 06:39