The problem says: Find the mean age of your classmates. The age is randomly generated from 18 to 23 years old within the function, which performs the sum and returns the sum; the mean is calculated in another function that receives the sum and returns the result.
I have done everything well, the problem is in the second function where I have to take the average, I can only use the value returned from the first function, which is the sum of the ages. The average gives if I leave a value assigned to me, but in reality the value depends on how much the value entered by the user is. I explain below...
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
float generadorEstudiantes (float alum);
float media (float prom );
int main (){
float prom, alum;
printf ( "\nEL PROMEDIO DE LA EDADES ES DE: %.2f", media (prom));
}
float generadorEstudiantes (float alum){
int i, can;
int rango, n, cd;
srand (time (NULL));
rango = 24-18;
printf ("Ingrese la cantidad de alumnos:\t");
scanf ("%d", &can );
for (i=0; i<can; i++){
n = rand () % rango + 18;
alum += n;
cd = cd + 1;
printf ("\nEstudiante %i: %d", i+1, n);
}
printf ("\n\nLA SUMA DE LA EDADES ES: %.2f", alum);
return alum;
}
In this function I assigned the value of 5 to be able to get the average of the sum of the ages, I only put it to see if it printed the average correctly. Now, of course, that would not be the correct average since the value that goes where 5 is is the counter that is in the function generatorEstudiantes (float alum) , the variable cd that counts, how many ages are going to be executed, that variable is the one that will replace 5, so that is the problem, how do I pass that variable if only in that function it returns only one value that is alum= is the sum of the ages. I already tried to make the function return 2 values that would be the sum and the counter, but how do I make it divide and be able to take the average in the second function.
float media (float prom ){
float alum, n;
n = generadorEstudiantes (alum);
prom = n / 5;
return prom;
}