I am trying to create a program that calculates velocity and acceleration, the following information is given:
After each of the position measurements has been entered, calculate and display the following:
1. the average velocity for this time interval
2. the average acceleration for this time interval
Calculations
Use the following formulas for your calculations:
change of position = current position - previous position
change of velocity = current velocity - previous velocity
average velocity = change of position / time interval
average acceleration = change of velocity / time interval
I am having trouble with the average velocity and acceleration now showing the second time around the loop. I have a sample output too.
This is the code I have created so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int choice,
count=0;
cout << "Motion Analysis!";
double position = 0,
timeInt,
chngPos,
prevPos,
prevVel,
chngVel,
avgVel,
avgAccel,
crntVel = 0;
cout << fixed <<setprecision(2);
cout << "\nEnter the uniform time interval in seconds: ";
cin >> timeInt;
while (1)
{
cout << "\nEnter a position in feet (negative value to quit): ";
cin >> prevPos;
if (prevPos <= 0)
break;
else avgVel = prevPos / timeInt;
avgAccel = avgVel / timeInt;
cout << "\nAverage velocity (this interval): "<<avgVel<<" feet/second";
cout <<"\nAverage acceleration: " <<avgAccel<<" feet/second";
}
return (0);
}
|
I am really new with C++ and I am having a little bit of a hard time.
This is the sample output:
Motion Analysis
Enter the uniform time interval in seconds: 2.5
Enter a position in feet (negative value to quit): 2
Average velocity (this interval): 0.80 feet/second
Average acceleration: 0.32 feet/second/second
Enter a position in feet (negative value to quit): 5
Average velocity (this interval): 1.20 feet/second
Average acceleration: 0.16 feet/second/second
Enter a position in feet (negative value to quit): 4
Invalid value: 4 is less than the current position of 5. Try again.
Enter a position in feet (negative value to quit): 12
Average velocity (this interval): 2.80 feet/second
Average acceleration: 0.64 feet/second/second
Enter a position in feet (negative value to quit): -1
I am getting this as my output
Motion Analysis
Enter the uniform time interval in seconds: 2.5
Enter a position in feet (negative value to quit): 2
Average velocity (this interval): 0.80 feet/second
Average acceleration: 0.32 feet/second/second
Enter a position in feet (negative value to quit): 5
Average velocity (this interval): 2.00 feet/second
Average acceleration: 0.80 feet/second/second
Enter a position in feet (negative value to quit):