I need to write a program that reads the thrust data for a model rocket engine from a file and prints the data to the screen.The first line is information about the motor and can be skipped. The remainder of the file is paired of floating point numbers. The first number is time in seconds and the second number is the thrust in newtons.
Example output:
Enter the name of the file: A8.txt
The thrust data for that engine is:
Time Thrust
0.041 0.512
0.084 2.115
0.127 4.358
0.166 6.794
....
End program.
so far this is what i have but I'm confused on how to output the data??
After you have opened the file for input you would need to read the data line by line and put the data into their respective variables.
Here's some code that reads an entire line from the file and places it into a string:
1 2 3 4 5
while (!infile.eof()) {
string line;
getline(infile, line); // get the next line from the file
cout << line << endl; // outputs the entire line of the file
}
You could read the first line of data into a 'dummy' variable that you aren't going to use just before you enter the loop that reads all the lines from the file.
Like this:
1 2 3 4 5 6
string dummy;
getline(infile, dummy);
while (!infile.eof()) {
...
}
I started working on another program and while going through the instructions I was confused.
Hope you are able to give me some ideas on how to design the program..Thanks!!
So we got a curve, the curve represents the thrust of a engine. I have to divide the curve into 10 points(5 segments). So the program ask for the segments, then ask for a time for which thrust will be calculated. The thrust is calculated by
((time_entered - firstpoint) /(second point - first point)) *(thrust of second point - thrust of first point) + thrust of first point).
Example output:
Enter thrust data (0 for thrust to end list):
0.19 14.5
0.24 6.0 (user enters the point and thrust at that point)
0.40 4.4
1.80 4.2
Enter a time: 1.0 (user enters time for which thrust is calculated)
The thrust at time 1.0 is 4.3 newtons
The Instructions say that the data should be read into a pair of arrays or one two dimensional array. This is where I'm confused, how to read the data into array and call them to calculate the thrust at a given time??
Sorry, but I can't quite understand what numbers you are using to get 4.3 Newtons for 1 second. Don't get me wrong, I trust your answer far more than my answer. I must have been using the wrong numbers because I managed to get -123.2 Newtons which isn't quite possible in this situation.
Tell me if I'm using the wrong numbers, but here's exactly what I did:
((1 - 0.19) / (0.24 - 0.19)) * (6 - 14.5) + 14.5
I'm clearly having a bad day because I'm also not sure whether the thrust data is meant to be entered into the program by the user or that it is meant to be read from file.
I need just a little bit more context to be more helpful.
I apologize for not being clear enough. Yes you are using the wrong time segment. It should be the segment of (0.40-1.80)
here is an example:
We make the time 0.19 to 0.24 a segment and the thrust at 0.19 is 14.5 newtons and the thrust at 0.24 is 6.0 newtons. then if I call the function with a time of 0.21 it will return the value of:
((0.21 - 0.19)/(0.24 - 0.19)) * (6 - 14.5) + 14.5 = 11.1 newtons
And yes, the thrust data is entered by the user for example:
Enter thrust data:
(time/points) (thrust at point)
0.19 14.5
0.24 6.0
0.40 4.4
Thanks, that's much better now. First of all, you obviously need to actually get input from the user for the data you require for the calculations. You could either get input with both pieces of data entered at once or you could ask for them separately.
I'm not going to spoil what I've done just yet, I'm going to give you the opportunity to try and figure this one out yourself; all I'm going to say is that you need to use cin >> to take input. And then once you've got the input you just simply need to put the data into the formula and calculate the thrust.
I made a program that calculates thrust for any given time on the curve. The program ask the user for a time then a function calculates the thrust using if else statements to check the segments. There are still major things to add like the array for the thrust data and a for loop to allow the user enter up to 50 curve points.
Creating the array to store the data and how to call the data to calculate the thrust at given time is where im confused??
Well you could either input the thrust data from the file, as you did from your previous program, or you could ask the user to input the data. I think inputting from file would be easier considering you already wrote the code to do it in the previous program. All you need to do with the data is split it up into the necessary numbers and then put those values into an array.
The below example splits off a line of the file and then places the numbers into 2 arrays.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
constint ITEMS = 6;
int time[ITEMS];
int thrust[ITEMS];
string line = "0.19 14.5"; // an example line of data from the file
// search for the space in the line that splits the numbers
int sp = line.find(" ");
if (sp != string::npos) {
// a space exists
double ti; // the time
double th; // the thrust
ti = stod(line.substr(0, sp)); // splits the first number and converts it into a double
th = stod(line.substr(sp + 1, line.length() - sp)); // does the same with the second
time[0] = ti;
thrust[0] = th;
}
You just have to loop through all of the lines continuing to split the numbers up and placing them into the respective arrays.
So I added a for loop that ask user for thrust data (time, thrust at time) and stores it in a multidimensional array. But now im confused on how to pass the array to the function to calculate the thrust for given time. What do I put in for the function parameters in the function call, definition and prototype??? I added question marks for the areas where im confused.
#include <iostream>
#include <cstdlib>
#include <iomanip>
usingnamespace std;
double thr(???????????????);
// Main Program
int main( )
{
double timethrust[10][10];
char answer; // answer enter by user if program should runs again
double enginethrust, // engine thrust during time of interest
entered_time_by_user; // time for which thrust is calculated
// Output Identification
system("CLS");
cout << " - "
<< "Compute Engine Thrust Using Arrays\n\n";
do
{
cout << "Enter thrust curve data (0 for thrust to end list):\n";
for(int i=0; i<10; i++)
{
cin >> timethrust[i][0] >> setw(5) >> timethrust[0][i];
}
cout << "Enter a time: ";
cin >> entered_time_by_user;
// function call for engine thrust
enginethrust = thr(??????????????);
// outputs the engine thrust for time of interest
cout << "The thrust at time " << entered_time_by_user << " is " << enginethrust << " newtons.\n";
// ask user if program should run again
cout << "Would you like to run the program again? ";
cin >> answer;
}while(answer == 'Y' || answer == 'y');
return 0;
}
// function definition
double thrust(????????????????)
{
}
If you are wanting to pass the entire array at once to the function, then the function would look something like this: double thrust(double data[10][10])
And your prototype would look very similar: double thrust(double[10][10]);
Then when you want to pass the array to the function you just type the name of the array as you would for a normal variable, like so: enginethrust = thrust(timethrust);