hallo, I am new to this forum so I hope this isn't a trivial question. I have written a simple program to calculate the stopping distance of a vehicle and was wondering how I stop the for loop when the function gets to 0 or less. I was thinking maybe I could assign a character to the solution "s" with an if statement. Like s=NEG if s<=0 then using that as the stop condition.
Any help would be appreciated.
Code:
#include <iostream.h>
#include <iomanip.h>
float f(float, float, float); //function prototype
int main(){
float v,u,g,m,h,s[100],T[100];
int i, n;
cout << "values of v,u,m,n,h are (n must be < 100) \n";
cin >> v >> u >> m >> n >> h;
s[0] = 0;
T[0] = 0.5*m*v*v;
for(i = 0; i<n ;i++){//this is the euler loop
T[i+1] = T[i] + h * f(u,m,g);
s[i+1] = s[i] + h;
}
//set headings for table and then print table
cout << setw(0) << "s (Distance)" << setw(20) << "T (Kinetic Energy)" << "\n";
cout.setf(ios :: fixed);
for(i = 0; i <= n ; i++)
cout << setprecision(0) << setw(7) << s[i]
<< setprecision(0) << setw(20) << T[i] << endl;
return 0;
}
//this is the function f of the differential equation
float f(float u, float m, float g){
g = 9.8;
return -g*m*u;
}