Need help with the last part (Number 5) on my C++ lab You are asked to verify th
ID: 3847352 • Letter: N
Question
Need help with the last part (Number 5) on my C++ lab
You are asked to verify the rx _normal () function whether it generates Gaussian random numbers. Call the rx _normal(u, s) for n times where u and s are the desired mean and standard deviation, respectively. Calculate their sum and sum of square as sigma x and sigma x^2 and compare their real mean and standard deviation to the desired mean and standard deviation. E[X] = sigma x/n E[X^2] = sigma x^2/n sigma = squareroot E[X^2] - (E [X])^2 Verify the ratio of samples within mu plusminus sigma, mu plusminus 3 sigma ranges based on the statistics from the wikipedia (https en.Wikipedia.org/wiki/Normal distribution).Explanation / Answer
#include <math.h>
#include <stdlib.h>
double rx_normal (double µ, double )
{
double U1, U2, W, mult;
static double X1, X2;
static int n = 0;
if (n == 1)
{
n = !n;
return (µ + * (double) X2);
}
do
{
U1 = -1 + ((double) rx_normal () / RAND_MAX) * 2;
U2 = -1 + ((double) rx_normal () / RAND_MAX) * 2;
W = pow (U1, 2) + pow (U2, 2);
}
while (W >= 1 || W == 0);
mult = sqrt ((-2 * log (W)) / W);
X1 = U1 * mult;
X2 = U2 * mult;
n = !n;
return (µ + * (double) X1);
}
int calculate(int n)
{
float x [50]; /* max. 50 elements in array x */
int n; /*number of elements in array x */
float sum, µ, ; /* sum, mean and standard deviation.*/
int i;
clrscr();
for(i = 0; i<n; i++)
scanf("%f", &x[i]);
/* calculate mean */
sum= 0.0;
for(i = 0; i < n; i++)
sum+= x[i];
µ = sum / n;
/* calculate standard deviation */
sum= 0.0;
for(i = 0; i < n; i++)
sum+= (x[i] - µ) * (x[i] - µ);
= sqrt(sum / n);
printf("Mean = %6.3f ", µ);
printf("Standard Deviation: %6.3f ", );
getch();
}
Void main()
{
int result = rx_normal(u,s);
int cal = calculate(result);
if(result==calculate)
printf(“verified”);
else
printf(“Not verified”);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.