Write a C program that will read the required values from th
Write a C program that will read the required values from the input file. Using the thrust, time, and increment between timesteps, compute the total thrust and write separate user-defined functions to compute the total impulse, specific impulse, and average thrust. Print the total impulse, specific impulse, and average thrust to the computer screen and to an output file called impulse-thrust.
The total impulse is the integral of the thrust over the operating duration of the motor. This can be approximated by
It = t(F1+F2+F3+…)
where: It = total impulse in lb-sec
 t = time increment in seconds
 F1, F2, F3, … = thrust at timesteps 1, 2, 3, … in lb
The specific impulse is the total impulse divided by the weight of the propellant burned in the test.
Isp = It/m
where: Isp = specific impulse in seconds
 m = mass of propellant burned in pounds
The average thrust is found by dividing the total impulse by the thrust time.
Favg = It/t
where: Favg = average thrust in lb
 t = total test time in seconds
An input data file called time-thrust contains time, increment, and thrust data for a rocket test where themass of propellant burned was 1.54 lb. The first record line of the input file is the control number which defines the number of data points to be read into the program. Each succeeding record line has three columns. The first column is the time in seconds, the second column is the increment between timesteps in seconds, and the third column is the thrust in pounds. The input file is comma delimited.
OUTPUT FORMAT:
********************************************
ROCKET MOTOR TEST RESULTS
Total Impulse: XXX.X lb-sec
Specific Impulse: XXX.X sec
Average Thrust: XXX.X lb
********************************************
Input file data
Solution
Please follow the code and comments for description :
CODE :
#include <stdio.h> // required header files
 #include <stdlib.h>
 #include <string.h>
int main() // driver method
 {
     FILE * fp; // required initialisations of the variables
     FILE * fptr;
     char * line = NULL;
     size_t len = 0;
     ssize_t read;
     char * readData = NULL;
     char * tot_tests = NULL;
     int count = 0;
     char timeInSec[22], incTimeSteps[22], thrust[22];
     double timeInSecVal, incTimeStepsVal, thrustVal, mass = 1.54, deltaT = 1.00, thrustSumVal, totalTestTime, totalImpulse, specificImpulse, avgThrust;
    fp = fopen(\"time-thrust.txt\", \"r\"); // reading the data fromt the file
     if (fp == NULL) { // checking for null values if any
         exit(0); //exiting the code if any nulls
     }
   
     fptr = fopen(\"impulse-thrust.txt\", \"w\"); // opening the file to write the output data
     if(fptr == NULL) // checking the null condition
     {
       printf(\"Error!\"); // print the error if any
       exit(1);
     }
     fprintf(fptr,\"%s\", \"********************************************\ \"); // print data to file
     fprintf(fptr,\"%s\", \"         ROCKET MOTOR TEST RESULTS          \ \"); // print data to file
   
     printf(\"********************************************\ \"); // print data to console
     printf(\"         ROCKET MOTOR TEST RESULTS          \ \"); // print data to console
     while ((read = getline(&line, &len, fp)) != -1) { // reading the data from the file
         if(count == 0) { // getting the total count of tests
             tot_tests = line; // saving it to a variable
         } else { // else read to a line
             readData = line;
           
             strcpy(timeInSec, strtok(line , \",\")); // seperating the data from comma deleimited
             strcpy(incTimeSteps, strtok(NULL, \",\"));
             strcpy(thrust , strtok(NULL, \",\"));
           
             timeInSecVal = atof(timeInSec); // conver the string data to the double data type
             incTimeStepsVal = atof(incTimeSteps);
             thrustVal = atof(thrust);
           
             thrustSumVal = thrustSumVal + thrustVal; // adding up to get the sum of thrusts values
           
             totalTestTime = totalTestTime + timeInSecVal; // adding up to get the total test time of the results
         }
         count++; // incrementing the count
     }
   
     totalImpulse = deltaT * thrustSumVal; // getting the results calulcated
     printf(\"\ The Total Impulse is : %0.2f lb-sec \ \", totalImpulse); // print data to console
     fprintf(fptr,\"%s %0.2f %s\", \"\ The Total Impulse is : \", totalImpulse, \" lb-sec\"); // print data to file
   
     specificImpulse = thrustSumVal / mass; // getting the results calulcated
     printf(\"\ The Specific Impulse is : %0.2f sec \ \", specificImpulse); // print data to console
     fprintf(fptr,\"%s %0.2f %s\", \"\ The Specific Impulse is : \", specificImpulse, \" sec\"); // print data to file
   
     avgThrust = thrustSumVal / totalTestTime; // getting the results calulcated
     printf(\"\ The Average Thrust is : %0.2f lb \ \", avgThrust); // print data to console
     fprintf(fptr,\"%s %0.2f %s\", \"\ The Average Thrust is : \", avgThrust, \" lb\"); // print data to file
   
     printf(\"********************************************\ \"); // print data to console
     fprintf(fptr,\"%s\", \"\ ********************************************\ \"); // print data to file
   
     fclose(fp); //closing the files
     fclose(fptr);
   
     return 0;
 }
time-thrust.txt :
22
 0.0,0.0,0.0
 0.1,0.1,50.0
 0.2,0.1,100.0
 0.3,0.1,140.0
 0.4,0.1,150.0
 0.5,0.1,155.0
 0.6,0.1,155.0
 0.7,0.1,154.0
 0.8,0.1,150.0
 0.9,0.1,149.0
 1.0,0.1,148.0
 1.1,0.1,140.0
 1.2,0.1,138.0
 1.3,0.1,132.0
 1.4,0.1,120.0
 1.5,0.1,100.0
 1.6,0.1,80.0
 1.7,0.1,50.0
 1.8,0.1,30.0
 1.9,0.1,20.0
 2.0,0.1,10.0
 2.1,0.1,0.0
impulse-thrust.txt :
 ********************************************
          ROCKET MOTOR TEST RESULTS        
The Total Impulse is : 2171.00 lb-sec
 The Specific Impulse is : 1409.74 sec
 The Average Thrust is : 93.98 lb
 ********************************************
 Hope this is helpful.



