2015-04-23 101 views
0

最近,我一直在研究一個發動機推力計算給定值的斜率。在方程中使用二維數組

我已經得到了很多的代碼工作,但我似乎無法得到方程功能的工作。該人應該根據圖表和牛頓上的特定點列出數值,然後給出不同的時間,計算機將在給定的時間和斜率計算之間找到一個值。

當然,這不工作,我真的迷失在這一點上,我100%肯定我的循環在函數中是錯誤的,但我不知道我的方程是錯誤的。

基本程序是應該這樣做

x y 
.19 14.5 
.24 6.0 
.40 4.4 

Enter a time: .21 

((.21-.19)/.24-.19))*(6-14.5)+14.5=11.1 

the thrust at time .21 is 11.1 

來源

#include <iostream> 
#include <cstdlib> 
#include <cmath> 
#include <string> 
#include <iomanip> 
#include <fstream> 
using namespace std; 

const int grid_rows=50; 
const int grid_cols=2; 

double slope(double thrust[grid_rows][grid_cols],double time); 

// Constant Declarations 
const double PI = 3.1415926535; // the radius/diameter of a circle 

// Main Program 
int main() 
{ 
system("CLS"); 
cout << "Take Home #12 by - " 
    << "CETUA\n\n"; 

double thrust[grid_rows][grid_cols]; 
double time; 
double newton; 
char ans; 
int i=0; 
int j=0; 

cout << "Enter thrust curve data (0 for thrust end list): "<<endl; 
for(i=0;i < grid_rows; i++) 
{ 
    for(j=0; j< grid_cols;j++) 
    { 
     cin >> thrust[i][j]; 
     if(thrust[i][j]==0) 
     { 
      break; 
     } 
    } 
    if(thrust[i][j]==0) 
    { 
     break; 
    } 
    } 


    do 
    { 
    cout << "Enter a time: "<<endl; 
    cin >> time; 

    newton=slope(thrust,time); 

    cout << "The thrust at time "<<time << " is " << newton << " newtons."     <<endl: 
    cout << "Would you like another thrust value? (Y or N): " <<endl; 
    cin >> ans; 
    }while(ans=='Y'||ans=='y'); 
} 

double slope(double thrust[50][2],double time) 
{ 
    double newton; 

    while(time > thrust[50][2]); 
    { 
    for(int i=0;i < grid_rows; i++) 
    { 
     for(int j=0; j< grid_cols;j++) 
     { 
     newton=((time - thrust[i][j])/(thrust[i][j]-thrust[i][j])) 
      *(thrust[i][j]-thrust[i][j])+thrust[i][j]; 
     return newton; 
     } 
    } 
    } 
} 
+0

嘗試在發佈之前縮進代碼,它使代碼更具可讀性。 – ANjaNA

+0

以數組表示給我你的例子,它是3X2嗎? – ANjaNA

+1

它的一個50 * 2,50行和2列 – Notkrokidal

回答

1
double slope(double thrust[50][2],double time) 
{ 
    double newton; 

    while(time > thrust[50][2]); 
    { 
    for(int i=0;i < grid_rows; i++) 
    { 
     for(int j=0; j< grid_cols;j++) 
     { 
     newton=((time - thrust[i][j])/(thrust[i][j]-thrust[i][j])) 
      *(thrust[i][j]-thrust[i][j])+thrust[i][j]; 
     return newton; 
     } 
    } 
    } 
} 

我看到你的算法中的一些問題。

1)你在這裏得到除零誤差。

((time - thrust[i][j])/(thrust[i][j]-thrust[i][j])) 

2)你的循環永遠不會運行(總是在第一次迭代時返回)。

return newton; 

3)如果固定(2)請注意,你可能會永遠被困在while循環中,(時間和推力[50]的值[2]被從未改變)。

也是「;」在while循環的末尾故意?

while(time > thrust[50][2]); 

您可能需要將斜率方法更改爲以下內容。

double slope (double x1, double x2, double y1, double y2, double time){ 

    double result = 0; 
    if ((x2-x1) != 0){ // google 'double comparison" you may want to use EPSILON instead 
     result = ((time - x1)/(x2-x1)) * (y2-y1) + y1 
    } 
    return result; 
} 

要使用它,你可能想沿着下面的方法做一些事情。

... assuming trust 
    [row][0] contains all the x 
    [row][1] contains all the y 
    double [lastEntry-1] results; 
for(int i=0; i< lastEntry-1; i++){ 

    results[i] = slope (thrust[i][0], //x1 
         thrust[i+1][0],//x2 
         thrust[i][1], //y1 
         thrust[i+1][1],//y2 
         time); 
} 

我離開如何從cin作爲鍛鍊來填充推力。

+0

嗯,那麼我怎麼會告訴程序該數組的哪個部分是什麼部分的功能? – Notkrokidal