Monday, October 1, 2018

Day 9 9/25/18

1. Complete the random number problem but with all four parts;

a. Write a program that uses the rand_float function developed previously to generate sequences of random floating-point values between 4 and 10. Then compare the computed mean and variance with the computed theoretical values. As you use more and more random numbers, the computed values and the theoretical values should become closer.


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define seed 26
#define number 10
#define npts 10
int a=4, b=10, list, sum, sumlist, average, variance, xna, xna2;
float random_float[number];
float rand_float(int a,int b);
int main(void)
{
srand(seed);
printf("list of %d random numbers:\n", number);
for (int i=1; i<=number; i++){
random_float[i] = rand_float(a,b);
list = random_float[i];
printf("%d\n", list);
sum +=list;}
average = sum / number;
for(int i=1; i<=number; i++){
xna = random_float[i] - average;
xna2 = pow(xna, 2);
sumlist += xna2;
}
variance = sumlist / (number-1);
printf("Average is:\n%d\nVariance is:\n%d\n", average, variance);
/* Exit program. */
return 0;
}
float rand_float(int a,int b)
{
return rand()%(b-a+1) + a;
}

No comments:

Post a Comment