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;
}
Initial notes:
tabulate the code . Untabulated code is terribly difficult to read, and errors will start reproducing like cute bunnies. You're warned.
For more than 30 years it is not necessary to declare all the variables at the beginning of the function. This practice is not advisable at all because the life of the variables is unnecessarily lengthened and that is when we can end up reusing a variable... this is usually the origin of numerous programming errors. Try that the life of the variables is the essential minimum.
Let's go with your code.
You have a problem in the two functions you have defined, and it is that both receive a variable as a parameter whose value they do not use at any time:
If the function receives a parameter, it is because it needs a value that must be supplied from the outside so that the function can work correctly. If the function does not need such a value, then it should not receive any parameters.
Now speaking of the function
generadorEstudiantes
:You shouldn't call
srand
in a function that does more things... the reason is that every time you call yousrand
modify the seed of the number generator. If this function is called two or more times in the same second (it is the precision oftime(NULL)
) then you will get the same sequence of random numbers several timesalum
should not be afloat
but an integer. In this function you have no need to resort to using decimals. In fact it should have a more suitable name, likesuma_edades
. An easy to read code will give you less problems.int StudentGenerator () { int range = 24 - 18;
}
Going ahead a little to the next problem, this function should not ask for the number of students, since this data is going to be necessary to be able to calculate the average. This is data that we can either request in a separate function or directly in the
main
:Now it would make sense for it to
numeroAlumnos
receive a parameter. Notice that the function now has a parameter, and in return loses the code that initialized the variable.Now we go for the function
media
. This function shouldn't callgeneradorEstudiantes
directly, instead it should pass the sum and the number of values so that it can calculate the average:And we would only have to link all these functions in the
main
: