2

どこに行くべきかについて少し助けとヒントが必要です プログラミングの割り当てのために、ユーザーが入力した数値の平方根を計算するプログラムを作成する必要があり、特定の要件があります。

  1. メインは番号を要求して表示し、ループ内で動作するため、ユーザーはプログラムを閉じずに繰り返すことができます

  2. 計算は、次のアルゴリズムを使用してmainによって呼び出されるsqRootという関数で実行する必要があります。

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

  1. sqRootは、 sqRootによって呼び出されるabsValという名前の関数を使用して、数値の絶対値を見つける必要があります。

このようなプログラムをどこから始めればよいかさえわかりません。しかし、これは私がこれまでに持っているものです:

#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);
}

私は次に何をすべきか、そしてプログラムを適切に書く方法に固執しています。助けとヒントをありがとう!

4

2 に答える 2

2

スニペットでは、実装方法を示していませんが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;
 }

それが役に立ったことを願っています。

于 2016-04-06T22:58:05.740 に答える
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);
 }
于 2016-04-06T16:00:54.827 に答える