#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
void countx(double, int[]);
void initializeArray(int[]);
void printResult(double,double,int,int[]);
void main()
{
int i, count,distF[8];
double x,sum1,sum2;
/* Calculation for X~ (x,0,1) */
srand(time(NULL));
count=0;
sum1=sum2=0.0;
initializeArray(distF);
for(i=1;i<10000;i++)
{
x=(double)(rand())/RAND_MAX;
if(x>.9 && x<= 1.8)
count++;
countx(x,distF);
sum1 += x;
sum2 += x*x;
}
cout<<"Results for uniform Distribution:\n";
printResult(sum1,sum2,count,distF);
srand(time(NULL));
count=0;
sum1=sum2=0.0;
initializeArray(distF);
for(i=1;i<=10000;i++)
{
x= -(log(1-(double) (rand())/RAND_MAX))/2;
if(x>.9 && x<=1.8)
countx(x,distF);
sum1+=x;
sum2+=x*x;
}
cout<<"Reuslts for exponential Distribution 2:\n";
printResult(sum1,sum2,count,distF);
/*** Calculations for P(x=1/2)=1 ***/
srand(time(NULL));
count=0;
sum1=sum2=0.0;
initializeArray(distF);
for(i=1;i<=1000;i++)
{
x=.5;
count++;
countx(x,distF);
sum1+=x;
sum2+=x*x;
}
cout<<"Reuslts for exponential Distribution 3:\n";
printResult(sum1,sum2,count,distF);
srand(time(NULL));
count=0;
sum1=sum2=0.0;
initializeArray(distF);
double temp;
for(i=1;i<=10000;i++)
{
temp=(double)(rand())/RAND_MAX;
if(temp<=.5)
x=.25;
else
x=1.50;
if(x>.9 && x<=1.8)
count++;
countx(x,distF);
sum1 += x;
sum2 +=x*x;
}
cout<<"Reuslts for Distribution #4:\n";
printResult(sum1,sum2,count,distF);
/*Function*/
void countx(double x, int distF[]);
{
int j=0;
double t= -0.25;
do
{
if(x<=t)
{
for(int k=j;k<8;k++)
distF[k]++;
break;
}
t += .25;
j++;
}
while(t<=1.50);
//return;
}
void intializeArray(int distF[]);
{
for(int j=0;j<8;j++)
distF[j]=0;
//return;
}
void printResult(double sum1, double sum2, int count, int distF[]);
{
double ev_x,var_x,pr_x;
ev_x=sum1/10000;
var_x=sum2/10000-(ev_x * ev_x);
pr_x=(double)(count)/10000;
cout<<"Expected value: " <<ev_x<<endl;
cout<<"Variance: "<<var_x<<endl;
cout<<"P(0.9<x<=1.8):" <<pr_x<<endl;
cout<<"t=";
for(int k=-1; k<7;k++)
cout<<.25 *k<<"\t";
cout<<"lnFx(t)=";
for(int j=0;j<8;j++)
cout<<(double)(distF[j])/10000<<"\t";
cout<<endl;
//return;
}
}
助けが必要!Microsoft Visual Studio 2012 の使用
>kikii.obj : error LNK2019: unresolved external symbol "void __cdecl printResult(double,double,int,int * const)" (?printResult@@YAXNNHQAH@Z) referenced in function _main
1>kikii.obj : error LNK2019: unresolved external symbol "void __cdecl countx(double,int * const)" (?countx@@YAXNQAH@Z) referenced in function _main
1>kikii.obj : error LNK2019: unresolved external symbol "void __cdecl initializeArray(int * const)" (?initializeArray@@YAXQAH@Z) referenced in function _main