的錯誤是:C++:不被忽略,因爲它空值應該是
estimate1 = leibnizPi (nTerms, estimatedV1);
&
estimate2 = wallisPi (nTerms, estimatedValue2);
我想它有它設置的方式做引用函數中的estimatedValue,或者它被調用的方式不正確。
任何幫助非常感謝!
注意:必須保持VOID。對於那個很抱歉。
#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;
}
提高警告等級。 –
你說這些函數是「賦值」,並且必須保持爲「空」。謹慎澄清?你想要做什麼? –
在變量估計值1分配一個空值後,你期望的是什麼? –