To start of, I'm using repl.it on a chromebook. I'm coding in C and I have no idea what I'm doing.
I have to generate 5 random numbers, then find the mean average of them. I already did the generating 5 random numbers by using a while loop. Now I need to find out how to find the average number. Here is the code I have so far.
int n = 1;
float mean = 0;
//create 5 random numbers
srand(time(0));
while( n <= 5)
{
dice = rand() % (100 - 1 + 1) + 1.;
printf("\n%d", dice);
n++;
}
I saw some similar questions but they where all for Python. Thank you to anyone that can help out!
This should solve your problem, or get close enough that you can take it from there:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char **argv)
{
int n = 0;
double die;
double sum = 0.0;
double mean;
//create 5 random numbers
srand(time(0));
for(int n = 0 ; n < 5 ; ++n)
{
die = rand() % (100 - 1 + 1) + 1.;
printf("%f\n", die);
sum = sum + die;
}
mean = sum / 5.0;
printf("mean = %f\n", mean);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.