错误在于:
estimate1 = leibnizPi (nTerms, estimatedV1);
&安培;
estimate2 = wallisPi (nTerms, estimatedValue2);
我认为它与设置引用函数中的estimatedValue的方式有关,或者它被调用的方式不正确。
非常感谢任何帮助!
注意:仍然无法使用。对不起。
#include <iostream>
#include <cmath>
//
// This program will be used in the second assignment (functions)
//
using namespace std;
const double PI = 3.14159265358979323846;
void leibnizPi (int numberofterms, double &estimatedValue1 )
{
double sign = 1.0;
double sum = 0.0;
for (int i = 0; i < numberofterms; ++i) {
double denominator = 2.0 * i + 1.0;
double term = 4.0 / denominator;
sum = sum + sign * term;
sign = -sign;
}
estimatedValue1 = sum;
}
void wallisPi (int numberofterms, double &estimatedValue2)
{
double product = 1.0;
for (int i = 1; i < numberofterms; ++i) {
double r = 2.0*i;
r = r*r;
double term = r/(r-1.0);
product = product * term;
}
estimatedValue2 = 2.0 * product;
}
double abstractError (double computedValue);
double relativeError (double computedValue);
int main (int argc, char** argv) {
double estimate1 = 0;
double absErr1 = 0;
double relErr1 = 0;
double estimate2 = 0;
double absErr2 = 0;
double relErr2 = 0;
double estimatedV1 = 0;
double estimatedValue2 = 0;
for (int nTerms = 1; nTerms < 100001; nTerms = nTerms * 4) {
// Estimate Pi by two different methods
// Leibniz' sum
estimate1 = leibnizPi (nTerms, estimatedV1);
absErr1 = abstractError (estimate1);
relErr1 = relativeError (estimate1);
// Wallis' product
estimate2 = wallisPi (nTerms, estimatedValue2);
absErr2 = abstractError (estimate2);
relErr2 = relativeError (estimate2);
cout << "After " << nTerms << " terms\n";
cout << "Leibniz' estimate: "<< estimate1 << "\n";
cout << "Absolute error: " << absErr1
<< "\tRelative error: " << relErr1
<< "\n";
cout << "Wallis' estimate: "<< estimate2 << "\n";
cout << "Absolute error: " << absErr2
<< "\tRelative error: " << relErr2
<< "\n";
cout << endl;
}
return 0;
}
double abstractFunction (double computedValue)
{
double abstractError = abs(computedValue - PI);
return abstractError;
}
double relativeFunction (double computedValue){
double relativeError1 = abs(computedValue - PI) / PI;
return relativeError1;
}
答案 0 :(得分:2)
您不能使用返回void
的函数的返回值,因为没有函数。相反,你可能想尝试这样的事情:
double leibnizPi (int numberofterms, double &estimatedValue1 )
{
double sign = 1.0;
double sum = 0.0;
for (int i = 0; i < numberofterms; ++i) {
double denominator = 2.0 * i + 1.0;
double term = 4.0 / denominator;
sum = sum + sign * term;
sign = -sign;
}
estimatedValue1 = sum;
return estimatedValue1;
}
double wallisPi (int numberofterms, double &estimatedValue2)
{
double product = 1.0;
for (int i = 1; i < numberofterms; ++i) {
double r = 2.0*i;
r = r*r;
double term = r/(r-1.0);
product = product * term;
}
estimatedValue2 = 2.0 * product;
return estimatedValue2;
}
如果必须使用void
函数,则应将作为参数传递的变量(estimatedV1
)分配给辅助变量(estimate1
)。像这样:
leibnizPi (nTerms, estimatedV1);
estimate1 = estimatedV1;
答案 1 :(得分:0)
不,问题是你没有定义你的函数来返回任何东西,而是你试图获取它们返回的东西(未定义)并将它分配给变量,这是一个错误。
将其定义为double leibnizPi (int numberofterms, double &estimatedValue1 )
并添加一个return语句。
如果无法更改函数的返回类型,请不要将其视为返回值。只需撰写leibnizPi (nTerms, estimatedV1);
而不是estimate1 = leibnizPi (nTerms, estimatedV1);