help me...loop problem

My professor hand out the assignment and I am getting stuck with it. Any help will be appreciated.

The number of stage in the rocket can be determined by the number of times that the velocity increases to some peaks and then begins decreasing. Write the program that reads these data and determines the number of stages on the rocket.

I have the data files already but I have no idea how to make the loop. Please help me
post the code and data file you have.
I imagine your file has a bunch of numbers, they go back and forth between increasing and decreasing.

Your job is to count the amount of times the numbers start to increase.

Is the initial increase in speed a stage? I would ask my teacher this.

I would read a number, and store it. Then start a while loop. The loop continues until a number fails to be read. I check the number against the previous one, and if it's less, I set a flag that the speed is decreasing. Othewise, if the flag is set, I increase a counter and then unset the flag. In all cases, I store the number I just read into the variable I just compared it to. If the first increase in speed is a stage, then the flag should be set before entering the loop.

I would have to see code that you wrote to help you more.
Last edited on
thank you very much for reply

My job is count the time that the velocity start to decrease or get the peaks of velocity. I can write the loop to pop up the max value. This is the code

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
// Declare the variables

double time, altitude, velocity, acceleration;

double max, count(0);

// create the file

ifstream rocket;

//open the file

rocket.open("C:\\rocket2.txt");

// check

if ( rocket.fail())
{
cout<< " Error opening file \n"<<endl;
system("pause");
return(0);
}

// read the first line

rocket>>time>>altitude>>velocity>>acceleration;

max = velocity;

do
{

if (velocity > max)
{
max = velocity;

}

rocket>>time>>altitude>>velocity>>acceleration;

}while(velocity >0);


cout<<max<<endl;
rocket.close();
system("pause");
return(0);
}

I read the data and it has two peaks totally. I have no idea how to get the other one.
Sorry. I forgot the data file.

time>>altitude>>velocity>>acceleration

1.0000000e+01 2.9265380e+03 5.0821200e+02 4.3231640e+01
2.0000000e+01 1.0170240e+04 9.2798610e+02 4.0723180e+01
3.0000000e+01 2.1486260e+04 1.1832420e+03 1.0328000e+01
4.0000000e+01 3.3835080e+04 1.1882285e+03 -9.3307000e+00
5.0000000e+01 4.5250830e+04 1.0899705e+03 -1.0320900e+01
6.0000000e+01 5.5634490e+04 9.8935650e+02 -9.8019000e+00
7.0000000e+01 6.5037960e+04 8.9134700e+02 -9.8000000e+00
8.0000000e+01 7.3461430e+04 7.9334750e+02 -9.7999000e+00
9.0000000e+01 8.0904910e+04 6.9534750e+02 -9.8001000e+00
1.0000000e+02 8.7368380e+04 5.9734700e+02 -9.8000000e+00
1.1000000e+02 9.2851850e+04 4.9934700e+02 -9.8000000e+00
1.2000000e+02 9.7355320e+04 4.0134750e+02 -9.7999000e+00
1.3000000e+02 1.0087880e+05 3.0334400e+02 -9.8008000e+00
1.4000000e+02 1.0342220e+05 2.0534500e+02 -9.7990000e+00
1.5000000e+02 1.0498570e+05 1.3402000e+02 -4.4660000e+00
1.6000000e+02 1.0610260e+05 2.6304000e+02 3.0270000e+01
1.7000000e+02 1.1024650e+05 6.7618500e+02 5.2359000e+01
1.8000000e+02 1.1962630e+05 1.2929950e+03 7.1003000e+01
1.9000000e+02 1.3610640e+05 2.1234700e+03 9.5092000e+01
2.0000000e+02 1.6209570e+05 3.1700000e+03 1.1421400e+02
2.1000000e+02 1.9950640e+05 3.8340050e+03 1.8587000e+01
2.2000000e+02 2.3877580e+05 3.8779450e+03 -9.7990000e+00
2.3000000e+02 2.7706530e+05 3.7799500e+03 -9.8000000e+00
2.4000000e+02 3.1437480e+05 3.6819500e+03 -9.8000000e+00
-99 -99 -99 -99
If you could use code tags and indentation, that would help my eyes out. Code tags are formed by clicking the <> button to the right of the text box.

About your loop, the first thing that pops out is that your code has real trouble if the rocket doesn't end at zero velocity. Better is to put the actual reading of the variables in the while condition:
while (rocket >> time >> altitude >> velocity >> acceleration)

( >> is an operator that returns "rocket", "rocket" will not be true if it can't read those variables).

You need to keep track of two variables:
1
2
3
4
5
6
7
8
9
10
// Keeping it simple
double v1, v2;
rocket >> v1;
while (rocket >> v2)
{
  if (v1  < v2) /* the rocket is accelerating */;
  if (v1  > v2) /* the rocket is decelerating */;

  v1 = v2;
}
Last edited on
You mean I have to create 2 variable v1 and v2 and assign the value of velocity for them, then compare them together ? I still can not figure out how to get the peaks. Every time the velocity increase to the max value then decrease, we have a peak. In the data file, we have 2 peaks. How can I get them?
Every time the velocity increase to the max value then decrease, we have a peak


Not nessesarily, the booster might not bring the shuttle to a max speed. The only thing you know is that if all of the sudden the velocity increases, a booster has gone off.
Topic archived. No new replies allowed.