Tuesday, August 24, 2004

This following program written in C++ is for exercise #4 in chapter 3 of Ross's Simulation 2d. By the way I looked up how to write into this blog, the less than and greater than signs in the program where the include statement is. These weren't showing up as the blog thought they were an html tag.
//
// A C++ program for integrating a continuous random variable (1-x^2)^(3/2) between 0-1 and finding its expected value or mean.//

#include <iostream>
int Kvalue;
float Svalue;
int Seed;
float x;
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 Simulation: ";
std::cin >> Kvalue;
countK=0;
Svalue = 0;
while (Kvalue >= countK)
{

x= (float) ( rand() / (RAND_MAX + 1 ) );
Svalue= Svalue + pow((1-x*x),(3/2));


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: