Can anyone help me with this question? it uses C++ and the header file below is
ID: 3847572 • Letter: C
Question
Can anyone help me with this question? it uses C++ and the header file below is used to call rx_normal() function.
Explanation / Answer
Answer: You have only specified the header(.h) file. But to use function actually, .cpp file containg definition of function is required. So I have defined my own function to generate random normal numbers to show the working of program. In place of this, you can use your own function i.e. rx_normal().
See the code below:
------------------------------------------
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
using namespace std;
double random_normal(double mean, double sd)
{
double min, max, sqrd, mult;
static double temp1, temp2;
static int seed = 0;
if (seed == 1)
{
seed = !seed;
return (mean + sd * (double) temp2);
}
do
{
min = -1 + ((double) rand () / RAND_MAX) * 2;
max = -1 + ((double) rand () / RAND_MAX) * 2;
sqrd = pow (min, 2) + pow (max, 2);
}while (sqrd >= 1 || sqrd == 0);
mult = sqrt ((-2 * log (sqrd)) / sqrd);
temp1 = min * mult;
temp2 = max * mult;
seed = !seed;
return (mean + sd * (double) temp1);
}
int main()
{
int N = 10;
double *elems = new double[N];
double mean=5.0,sd=2.0;
cout<<"Generated numbers are:"<<endl;
for(int i=0;i<N;i++)
{
elems[i]=floor(random_normal(mean,sd)*100)/100;
cout<<elems[i]<<endl;
}
double sum=0.0,sum_sqrd=0.0;
for(int i=0;i<N;i++)
{
sum+=elems[i];
sum_sqrd+=pow(elems[i],2);
}
double mean_N=sum/N;
double mean_N_sqrd=sum_sqrd/N;
double sigma = sqrt(mean_N_sqrd - pow(mean_N,2));
cout<<"desired mean:"<<mean<<", desired standard deviation:"<<sd<<endl;
cout<<"real mean:"<<mean_N<<", real standard deviation:"<<sigma<<endl;
int> for(int i=0;i<N;i++)
{
double number=elems[i];
if (number >= (mean_N-sigma) || number <= (mean_N+sigma))
{
one_sigma_range++;
}
else if (number >= (mean_N-2.0*sigma) || number <= (mean_N+2.0*sigma))
{
two_sigma_range++;
}
else if (number >= (mean_N-3.0*sigma) || number <= (mean_N+3.0*sigma))
{
three_sigma_range++;
}
}
cout<<"Proportion of numbers in one sd range:"<<(one_sigma_range/N)*100<<endl;
cout<<"Proportion of numbers in two sd range:"<<(two_sigma_range/N)*100<<endl;
cout<<"Proportion of numbers in three sd range:"<<(three_sigma_range/N)*100<<endl;
return 0;
}
---------------------------------
Output:
-----------------------
Generated numbers are:
3.94
6.05
10.14
3.78
4.42
6.52
6.44
6.1
4.52
5.52
desired mean:5, desired standard deviation:2
real mean:5.743, real standard deviation:1.76195
Proportion of numbers in one sd range:100
Proportion of numbers in two sd range:0
Proportion of numbers in three sd range:0
-----------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.