2016-04-06 10 views
2

私はちょっとした助けが必要です。 プログラミング割り当てのために、私は平方根を計算するプログラムを書く必要がありますユーザーが入力した番号で、一定の要件があります。3つのユーザー定義関数を使って数値の真の平方根を計算する方法

  1. 番号を要求し、それを表示し、ユーザはそれを

  2. 計算は呼び出された関数で行われなければならないを閉じずにプログラムを繰り返すことができるように、ループ内動作しますSQROOTアルゴリズムを用いメインによって呼び出される。

newValueに= 0.5 *(oldVa LUE +(X/OLDVALUE))

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

    私はすぐ隣、どのように適切にプログラムを書くために何をすべきかをで立ち往生しています。 ヘルプとヒントをありがとう!

    答えて

    2

    あなたは簡単である、あなたはabsVal()を実装方法を示していない:

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

    はあなたが三項演算子を知っていると仮定すると。それ以外の場合はifを使用します。

    あなたが投稿したmain()の実装は、基本的に無限ループであり、ユーザーが入力した最初の数字だけを計算して1.0以上繰り返します。それはあなたが求めているものではありません、私は考えています

    x> = 1の条件が(小さな値はより多くの反復が必要です)、あなたの前提とあなたが初期化するには、パラメータとして変数xを渡すために

    #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()の実装では、あなたが忘れてしまった:負の数は(あなたの代わりにエラーを印刷するABSVALを使用することができます)、しかし、あなたはこのような何かを書くことができます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; 
    } 
    

    はそれが助け願っています。

    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); 
    } 
    
    関連する問題