2014-05-14 62 views
0

我正在編寫一個程序,以查找用戶給定的時間間隔上的方程的最大值。當我編譯的代碼,而不是輸出最大的它給了我這個嵌套循環找到最大

請輸入要檢查的間隔的第一個數字: 請輸入時間間隔的最後一個號碼進行檢查: 請輸入所需的初始步驟大小: sh:暫停:命令未找到

我想問題與我的循環,但我不知道如何糾正這種情況。 這裏是我的代碼

#include <iostream> 
#include <cmath> 

using namespace std; 

int main() 
{ 
    int a, b, delta, x, y; 
    int max = 0; 


    cout <<"Please enter the first number of the interval to be checked: " << endl; 
    cin >> a; 
    cout << "Please enter the last number of the interval to be checked: " << endl; 
    cin >> b; 
    cout << "Please enter the desired initial step size: " << endl; 
    cin >> delta; 

    for(x = a; x <= b; x = x+delta) 
    { 
     y = pow(x, 2)-7*x-18; 
     if (y > max) 
     { 
      max = y; 
      cout <<"The maximum over the interval from " << a <<" to " << b <<" is " << max; 
     } 
     else 
     { 
      delta= delta/2; 
     } 
     if (delta < pow(10, -6)) 
     { 
      break; 
     } 
    }  

    return 0; 
} 
+0

爲什麼你需要使用系統暫停? –

+0

我想這不是真的有必要。我會擺脫它。儘管如此,這對我的循環中的任何錯誤都沒有幫助。 –

+0

你能澄清你的問題是什麼嗎? [它不會做_nothing_](http://ideone.com/Xw7ffq)。只需移動循環外部的最大值... – David

回答

0

看你的代碼好像是增量的值應該是在浮法因爲你是遞歸除以2.您POW(10,-2)不會做任何有用的比較這件事。即使在所有事情都沒有讓我停下來的時候出現錯誤。我在VS 12上運行它(C++ 11)。