- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要一些帮助和一些关于去哪里的提示对于编程作业,我必须编写一个程序来计算用户输入的数字的平方根,并且有一定的要求。
main 请求数字并显示它,在一个循环内运行,以便用户可以在不关闭它的情况下重复该程序
计算必须在名为 sqRoot 的函数中完成,该函数将由 main 使用以下算法调用:
新值 = 0.5 * (旧值 + (X/旧值))
我什至不知道从哪里开始这样的程序。但是,这是我到目前为止所拥有的:
#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);
}
我只是纠结于下一步该做什么以及如何正确编写程序。感谢您的帮助和提示!
最佳答案
在您的代码片段中,您没有展示如何实现 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;
}
希望对您有所帮助。
关于c++ - 如何使用 3 个用户定义函数计算数字的真平方根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36455692/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!