如何使用 3 个用户定义函数计算数字的真平方根
How to calculate the true square root of a number with 3 user-defined functions
我需要一些帮助和一些关于去哪里的提示
对于编程作业,我必须编写一个程序来计算用户输入的数字的平方根,并且有一定的要求。
main 请求数字并显示它,在循环内运行,以便用户可以在不关闭程序的情况下重复程序
计算必须在名为 sqRoot 的函数中完成,该函数将由 main 使用算法调用:
newValue = 0.5 * (oldValue + (X / oldValue))
- sqRoot 将需要使用名为 absVal 的函数找到数字的绝对值,然后 [=23] 将调用该函数=]sqRoot
我什至不知道从哪里开始使用这样的程序。但是,这是我目前所拥有的:
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
double sqRoot();
double absVal();
int i = 0;
double X;
int main()
{
sqRoot = sqrt(X);
double X;
// Calculations
cout << "Please enter a number: ";
cin >> X;
while (X <= 0)
{
cout << "*** Error: Invalid Number! *** " << endl;
cout << "Please enter a number: ";
cin >> X;
}
while (X >= 1)
{
cout << "The Square Root is: " << sqRoot << endl;
}
}
double sqRoot ()
{
double newValue;
double oldValue ;
while (abs(newValue - oldValue) > 0.0001)
{
newValue = 0.5 * (oldValue + ( X / oldValue));
oldValue = newValue;
cout << " The square root is: " << newValue << endl;
}
return (newValue);
}
我只是不知道下一步该做什么以及如何正确编写程序。
感谢您的帮助和提示!
只是一些更正
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
double sqRoot(double X);
int main()
{
double X;
// Calculations
cout << "Please enter a number: ";
cin >> X;
while (X <= 0)
{
cout << "*** Error: Invalid Number! *** " << endl;
cout << "Please enter a number: ";
cin >> X;
}
while (X >= 1)
{
cout << "The Square Root is: " << sqRoot(X) << endl;
}
}
double sqRoot(double X)
{
double newValue = 0;
double oldValue = X;
while (true)
{
newValue = 0.5 * (oldValue + (X / oldValue));
if (abs(newValue - oldValue) < 0.0001)
break;
oldValue = newValue;
//cout << " The square root is: " << newValue << endl;
}
return (newValue);
}
在您的代码片段中,您没有展示如何实现 absVal()
,这很简单:
double absVal( double x )
{
return x < 0.0 ? -x : x;
}
假设您知道三元运算符。否则使用 if
.
您发布的 main()
的实现基本上是一个无限循环,它只重复计算和打印用户输入的第一个等于或大于 1.0 的数字。我想这不是你要的
我不确定 x >= 1 条件是否是强制性的(微小的值需要更多的迭代)或者你的假设以及在负数的情况下你应该做什么(你可以使用 absVal 代替打印错误),但你可以这样写:
#include <iostream>
// #include <cmath> <-- you are not supposed to use that
// #include <cstdlib> <-- why do you want that?
// using namespace std; <-- bad idea
using std::cin;
using std::cout;
double absVal( double x );
double sqRoot( double x );
int main()
{
double num;
cout << "This program calculate the square root of the numbers you enter.\n"
<< "Please, enter a number or something else to quit the program:\n";
// this will loop till std::cin fails
while ( cin >> num )
{
if ( num < 0.0 ) {
cout << "*** Error: Invalid input! ***\n"
<< "Please enter a positive number: ";
continue;
}
cout << "The square root of " << num << " is: " << sqRoot(num);
cout << "\nPlease enter a number: ";
}
return 0; // you missed this
}
然后,在 sqRoot()
的实现中,您忘记将变量 x 作为参数传递,以初始化 oldValue 和 newValue 并且如果执行流程恰好进入 while 循环,它将在第一个循环之后退出循环,因为 oldValue = newValue;
在条件之前计算。尝试这样的事情(我使用相对误差而不是绝对差来获得更好的精度 x 的小值以更多的迭代为代价):
double sqRoot(double x)
{
const double eps = 0.0001;
double newValue = 0;
double oldValue = x;
while ( oldValue != 0.0 )
{
newValue = 0.5 * (oldValue + (x / oldValue));
// check if the relative error is small enough
if ( absVal(newValue - oldValue) / oldValue < eps )
break;
oldValue = newValue;
}
return newValue;
}
希望对您有所帮助。
我需要一些帮助和一些关于去哪里的提示 对于编程作业,我必须编写一个程序来计算用户输入的数字的平方根,并且有一定的要求。
main 请求数字并显示它,在循环内运行,以便用户可以在不关闭程序的情况下重复程序
计算必须在名为 sqRoot 的函数中完成,该函数将由 main 使用算法调用:
newValue = 0.5 * (oldValue + (X / oldValue))
- sqRoot 将需要使用名为 absVal 的函数找到数字的绝对值,然后 [=23] 将调用该函数=]sqRoot
我什至不知道从哪里开始使用这样的程序。但是,这是我目前所拥有的:
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
double sqRoot();
double absVal();
int i = 0;
double X;
int main()
{
sqRoot = sqrt(X);
double X;
// Calculations
cout << "Please enter a number: ";
cin >> X;
while (X <= 0)
{
cout << "*** Error: Invalid Number! *** " << endl;
cout << "Please enter a number: ";
cin >> X;
}
while (X >= 1)
{
cout << "The Square Root is: " << sqRoot << endl;
}
}
double sqRoot ()
{
double newValue;
double oldValue ;
while (abs(newValue - oldValue) > 0.0001)
{
newValue = 0.5 * (oldValue + ( X / oldValue));
oldValue = newValue;
cout << " The square root is: " << newValue << endl;
}
return (newValue);
}
我只是不知道下一步该做什么以及如何正确编写程序。 感谢您的帮助和提示!
只是一些更正
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
double sqRoot(double X);
int main()
{
double X;
// Calculations
cout << "Please enter a number: ";
cin >> X;
while (X <= 0)
{
cout << "*** Error: Invalid Number! *** " << endl;
cout << "Please enter a number: ";
cin >> X;
}
while (X >= 1)
{
cout << "The Square Root is: " << sqRoot(X) << endl;
}
}
double sqRoot(double X)
{
double newValue = 0;
double oldValue = X;
while (true)
{
newValue = 0.5 * (oldValue + (X / oldValue));
if (abs(newValue - oldValue) < 0.0001)
break;
oldValue = newValue;
//cout << " The square root is: " << newValue << endl;
}
return (newValue);
}
在您的代码片段中,您没有展示如何实现 absVal()
,这很简单:
double absVal( double x )
{
return x < 0.0 ? -x : x;
}
假设您知道三元运算符。否则使用 if
.
您发布的 main()
的实现基本上是一个无限循环,它只重复计算和打印用户输入的第一个等于或大于 1.0 的数字。我想这不是你要的
我不确定 x >= 1 条件是否是强制性的(微小的值需要更多的迭代)或者你的假设以及在负数的情况下你应该做什么(你可以使用 absVal 代替打印错误),但你可以这样写:
#include <iostream>
// #include <cmath> <-- you are not supposed to use that
// #include <cstdlib> <-- why do you want that?
// using namespace std; <-- bad idea
using std::cin;
using std::cout;
double absVal( double x );
double sqRoot( double x );
int main()
{
double num;
cout << "This program calculate the square root of the numbers you enter.\n"
<< "Please, enter a number or something else to quit the program:\n";
// this will loop till std::cin fails
while ( cin >> num )
{
if ( num < 0.0 ) {
cout << "*** Error: Invalid input! ***\n"
<< "Please enter a positive number: ";
continue;
}
cout << "The square root of " << num << " is: " << sqRoot(num);
cout << "\nPlease enter a number: ";
}
return 0; // you missed this
}
然后,在 sqRoot()
的实现中,您忘记将变量 x 作为参数传递,以初始化 oldValue 和 newValue 并且如果执行流程恰好进入 while 循环,它将在第一个循环之后退出循环,因为 oldValue = newValue;
在条件之前计算。尝试这样的事情(我使用相对误差而不是绝对差来获得更好的精度 x 的小值以更多的迭代为代价):
double sqRoot(double x)
{
const double eps = 0.0001;
double newValue = 0;
double oldValue = x;
while ( oldValue != 0.0 )
{
newValue = 0.5 * (oldValue + (x / oldValue));
// check if the relative error is small enough
if ( absVal(newValue - oldValue) / oldValue < eps )
break;
oldValue = newValue;
}
return newValue;
}
希望对您有所帮助。