如何计算具有3个用户定义函数的数字的真实平方根

时间:2016-04-06 15:30:44

标签: c++ user-defined-functions sqrt absolute-value

我需要一些帮助以及一些关于去哪里的提示 对于编程分配,我必须编写一个程序来计算用户输入的数字的平方根,并且有一定的要求。

  1. 请求数字并显示它,在循环内操作,以便用户可以在不关闭的情况下重复该程序

  2. 计算必须在名为 sqRoot 的函数中完成,该函数将由 main 使用该算法调用:

  3. newValue = 0.5 *(oldValue +(X / oldValue))

    1. sqRoot 需要使用名为 absVal 的函数查找数字的绝对值,然后由 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);
      }
      

      我只是坚持下一步该做什么以及如何正确编写程序。 感谢您的帮助和提示!

2 个答案:

答案 0 :(得分:2)

在您的代码段中,您无法展示如何实施absVal(),这是微不足道的:

double absVal( double x )
{
    return  x < 0.0 ? -x : x;
}

假设您了解三元运算符。否则使用if

您发布的main()的实现基本上是一个无限循环,它仅重复计算和打印用户输入的等于或大于1.0的第一个数字。那不是你要求的,我想

我不确定x&gt; = 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;
 }

希望它有所帮助。

答案 1 :(得分:0)

只需进行一些更正

#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);
 }
相关问题