如何在for循环中添加结果?

时间:2015-11-10 00:11:28

标签: c++ algorithm probability

我创建了一个可以计算概率的程序:

() = ((!)/(x!)((N-x)!))*(^x)*(1-p)^(−)

当您输入Nxp的值时,该程序运行正常。

但是,我还需要计算给定x范围内的概率之和。所以我在程序中添加了x的低端和x的高端。我现在遇到方程式问题,因为我无法找到概率的总和。我正在尝试进行for循环,但我继续收到以下消息:"代码永远不会被执行"。

这是我的代码:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;


double factorial (double N, double x, double p);
double tgamma (double N, double x, double p);

int main ()
{
    double N;
    double xlow;
    double xhigh;
    double p;
    double Probability;
    double result;

    {
        cout << "Input N value" << endl;
        cin >> N;
        {
            cout << "Input low end of x Value" << endl;
            cin >> xlow;
            while(xlow<0 || xlow>N){
                cout << "x value is NOT between 0 and N." << endl;
                cout << "Input x Value" << endl;
                cin >> xlow;
            }
            {
                cout << "Input high endx Value" << endl;
                cin >> xhigh;
                while(xhigh<0 || xhigh>N){
                    cout << "x value is NOT between 0 and N." << endl;
                    cout << "Input x Value" << endl;
                    cin >> xhigh;
                }
            }
            cout << "Input p value" << endl;
            cin >> p;
            while(p<0 || p>1){
                cout << "p value is NOT a real number between 0 and 1." << endl;
                cout << "Input p value" << endl;
                cin >> p;
            }
            double Probability;
            Probability=result;
        }
        return 0;

    }

    double factorial (double N, double xlow, double xhigh, double p, double       Probability);

    for (xlow=0; xlow <= xhigh; ++xlow){

        result =((tgamma((N+1)))/((tgamma(xlow+1)) * ((tgamma(((N-xlow+1))))))) * (pow(p,xlow)) * (pow((1-p),(N-xlow)));
        Probability += result;
    }

}

有谁知道这里的问题是什么?

以下是根据您的建议更新的代码!

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;


double factorial (double N, double x, double p);
double tgamma (double N, double x, double p);

int main ()
{
double N;
double xlow;
double xhigh;
double p;
double Probability;
double result;

{
cout << "Input N value" << endl;
cin >> N;

cout << "Input low end of x Value" << endl;
cin >> xlow;
while(xlow<0 || xlow>N){
    cout << "x value is NOT between 0 and N." << endl;
    cout << "Input x Value" << endl;
    cin >> xlow;
}

cout << "Input high endx Value" << endl;
cin >> xhigh;
while(xhigh<0 || xhigh>N){
        cout << "x value is NOT between 0 and N." << endl;
        cout << "Input x Value" << endl;
        cin >> xhigh;
}

cout << "Input p value" << endl;
cin >> p;
while(p<0 || p>1){
    cout << "p value is NOT a real number between 0 and 1." << endl;
    cout << "Input p value" << endl;
    cin >> p;
}
    double Probability;
    Probability=result;
}




double factorial (double N, double xlow, double xhigh, double p);

for (xlow=0; xlow <= xhigh; ++xlow){

result =((tgamma((N+1)))/((tgamma(xlow+1)) * ((tgamma(((N-xlow+1))))))) * (pow(p,xlow)) * (pow((1-p),(N-xlow)));
Probability += result;
}

}

2 个答案:

答案 0 :(得分:4)

这一行

    return 0;

将阻止main函数的其余部分运行。删除它。

答案 1 :(得分:0)

我做了几处改动。使用“while”条件使此代码正常工作。以下代码计算概率总和:

select t.*
from (select t.*,
             row_number() over (partition by name, course order by lastupdate desc) as seqnum
      from t
     ) t
where seqnum = 1;