-4
#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
4

1 回答 1

1

你有两个总体问题:

1)你的函数是'定义的inside ofmain()' - 你需要将它们移到外面(函数不能嵌套)

2)您的函数定义在不适当的位置有一个分号,将它们变成仅仅是原型,后跟一个不相关的代码块(这就是为什么问题 #1 不是立即出现语法错误的原因)。例如:

    void intializeArray(int distF[]); // <-- this semi-colon needs to be removed
    {
    for(int j=0;j<8;j++)
    distF[j]=0;
    //return;
    }

哦,我一开始没有注意到的第三个问题:您在尝试定义函数时initializeArray()拼写错误。intializeArray()

于 2012-11-06T01:28:05.903 回答