15,995,230 members
0.00/5 (No votes)
See more:
I'm trying to calculate the average of a set of random numbers (all of them between 0 and 1), considering if other conditions are fulfilled. That is, calculate the average of a list of random numbers for every value of a float "y" while it is less than 1. I also need to consider the number of iterations required to do it.

The following is the code I wrote so far:

What I have tried:

```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define L 2
#define STEPS 10
#define ITERATIONS L*L

FILE *doc;

float mean(float sum, float counts){
double mean = 0.0;
mean = sum/counts;
return mean;
}

int main(){
doc = fopen("document.txt","w");
double y=0.0;
float r;
float sum_r=0.0;
float mean_r=0.0;
srand((unsigned)time(0));

fprintf(doc,"y                   mean of r        iterations \n");
printf("y                   mean of r        iterations \n");

for(int iter = 0; iter < ITERATIONS; iter++){

while(y<1){
sum_r = 0;
for (int t = 0; t < STEPS; t++){
r = rand()/(float)RAND_MAX;
sum_r+=r;
}
mean_r = mean(sum_r,(float)STEPS);
y = y + 1/((double)(STEPS*ITERATIONS));

fprintf(doc," %lf          %f           %d\n",y,mean_r,ITERATIONS);
printf(" %lf          %f           %d\n",y,mean_r,ITERATIONS);
}

}
fclose(doc);
return 0;
}```

The problem is that I get averages above of 1, and it is incorrect. Also, I don't know exactly how to include the "ITERATIONS" since I get incorrect values for the average of those number every time.

Note:

What I need to obtain is something like:

```for y=0.00
calculate the average of:
0.3545
0.5678
0.2346
.
.
.
0.6786

for y=0.01
calculate the average of:
0.7893
0.1234
0.3899
.
.
.
0.8726

for y=0.02
.
.
.
.```

and so on only while "y" is less than 1 and considering the number of iterations.
Posted
Updated 22-Feb-23 12:39pm
v6

## Solution 1

That code doesn't compile:
C
`fprintf(doc," %f          %f          %f    \n\n",y,mean_r,);`
You have specified three "%f" parameters in the format string but provided two, and have an empty parameter at the end - you'll get an error for the ",)" part, and a problem when it tries to execute if you remove the ",".

Try resetting `sum_r` after each `STEPS` iteration.

The way I'd do it is to write a function which generated a set of random numbers and returned the average.
Then call that repeatedly for each value of `y` and print the result.

Comments
CPallini 22-Feb-23 2:13am
5.
yuike 22-Feb-23 5:41am
Sorry, I fix my code. Now it's working
OriginalGriff 22-Feb-23 6:11am
It's still a good idea to move the average calculation into a separate function - it makes code a lot "cleaner" and easier to read.
yuike 22-Feb-23 8:01am
Thank you very much. I edited my code to include a function that calculates the average. Is it ok the way I did it?
OriginalGriff 22-Feb-23 9:16am
If you did, you didn't show us that code ... :D

## Solution 3

Quote:
Auyik 10hrs 10mins ago
I edited my code to include a function that calculates the average. Is it ok the way I did it?

no!

C++
```float mean(float sum, float counts){
double mean = 0.0;
mean = sum/counts;
return mean;
}```

It is almost useless to calculate the mean value over a single value and divide it by count.

Here several values should be passed, as well as the number of values as parameter. After that you would need a loop in which you sum up the values and finally it would make sense to divide.

v2
Comments
yuike 5-Mar-23 20:49pm
I fixed it, thanks! My code is working now :)

## Solution 2

Quote:
r = rand()/(float)RAND_MAX;
That should be, instead
C
`r = rand()/(RAND_MAX + 1.0f);`

Then you forgot to reset the `sum_r` variable inside the `ITERATIONS` loop.

Try
C
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define L 2
#define STEPS 10
#define ITERATIONS L*L

int main()
{
FILE * doc = fopen("document.txt","w");
float y=0.0f;
srand((unsigned)time(0));

while(y < 1.0f)
{
for(int iter = 0; iter < ITERATIONS ; iter++)
{
float sum_r = 0.0f;

for (int t = 0; t < STEPS; t++)
{
float r = rand()/(RAND_MAX+1.0f);
fprintf(doc,"r=%f\n",r );
sum_r += r;
}
float mean_r = sum_r/STEPS;

fprintf(doc," %f    %d      %f\n\n",y, iter, mean_r);
}
y = y + 0.01f;
}
fclose(doc);
return 0;
}```

v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Dave Kreskowiak 45 OriginalGriff 25 RickZeeland 10 Member 11653844 -16
 OriginalGriff 431 Pete O'Hanlon 215 Dave Kreskowiak 115 CPallini 110 CHill60 42

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900