c++ - Using a 2D array in an equation -
recently i've been working on engine thrust calculates slope @ given values.
i've gotten lot of code work, cant seem equation function working. person supposed list values based off specific points on graph , newton, , give time thats different , computer find value in between time given , slope calculation.
of course thats not working , i'm lost @ point, i'm 100% sure loop wrong in function, i'm not sure equation wrong.
basically program supposed this
x y .19 14.5 .24 6.0 .40 4.4 enter time: .21 ((.21-.19)/.24-.19))*(6-14.5)+14.5=11.1 thrust @ time .21 11.1
source
#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; // radius/diameter of circle // main program int main( ) { system("cls"); cout << "take home #12 - " << "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 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; } } { cout << "enter time: "<<endl; cin >> time; newton=slope(thrust,time); cout << "the thrust @ time "<<time << " " << newton << " newtons." <<endl: cout << "would 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; } } } }
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; } } } }
i see few problems algo.
1)you divide 0 error here.
((time - thrust[i][j])/(thrust[i][j]-thrust[i][j]))
2) loop never run (always return on first iteration).
return newton;
3) if fix (2) remember may forever trapped in while loop, (value of time , thrust[50][2] never changed).
also ";" @ end of while loop deliberate?
while(time > thrust[50][2]);
you may want change slope method following.
double slope (double x1, double x2, double y1, double y2, double time){ double result = 0; if ((x2-x1) != 0 ){ // google 'double comparison" may want use epsilon instead result = ((time - x1)/(x2-x1)) * (y2-y1) + y1 } return result; }
to use want thing along following.
... assuming trust [row][0] contains x [row][1] contains 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); }
i leave how populate thrust cin exercise you.