Tuesday, August 17, 2004

This program inspired from chapter 3 of Ross, Sheldon. Simulation 2d (San Diego: Academic, 1997) will calculate the average value of X^2 between x=1-10 for a discrete version of x squared.

//
// A C++ program For Integrating a random variable (X^2) and finding its expected value or mean.//
#include
int Kvalue;
int Svalue;
int Seed;
int U;
int countK;
float Expected;



int main()
{


std::cout << "Enter integer for random seed: ";
std::cin >> Seed;
void srand(unsigned int Seed);
std::cout << "Enter integer for length of Integral: ";
std::cin >> Kvalue;
countK=0;
Svalue = 0;
while (Kvalue >= countK)

{

U= 1 + (int) ( 10.0 * rand() / (RAND_MAX + 1.0) );
Svalue= Svalue + U * U;


countK++;
}

Expected= Svalue/Kvalue;
std::cout << "Expected Value = ";
std::cout << Expected;
std::cout << "Svalue = ";
std::cout << Svalue;
std::cout << "Kvalue = ";
std::cout << Kvalue;
return 0;
}


No comments: