#include <stdio.h>
#include <math.h>

// constants for the program
#define DATA_FILE "rocket.txt"
#define G -9.8
#define THRESHOLD 5.0e-1

int main() {

	// variables for data read in from the file.
	int t;
	double d, v, a;
	int conversions;  // used to detect blank lines.

	// variables to note and record new stages.
	double last_a;
	int stage_index;
	
	// the data file.
	FILE * f = fopen(DATA_FILE, "r");
	
	// initialize variables so we correctly detect and display the first stage.
	last_a = G;
	stage_index = 0;

	// for each data line, see if the last acceleration was G while the current
	// one was greater than G.  If so, display the new stage information (index,
	// timestamp and acceleration).
	while (!feof(f)) {
		conversions =fscanf(f, "%i%lf%lf%lf", &t, &d, &v, &a);
		if ( (fabs(last_a - G) <= THRESHOLD) && (fabs(a - G) > THRESHOLD)) {
			stage_index++;
			printf("%3i.%6i%8.3lf\n", stage_index, t, a);
		}
		last_a = a;
	}
	
	// we're done with the file.
	fclose(f);
	
	// end the program.
	return 0;
}

