Thread: Help with Programming Assignment

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    16

    Help with Programming Assignment

    Ok I have a program due on Monday. Just seeing if yall can help me out. I have tried different code but cant get the program to do everything its supposed to do.

    Here's the assignment:

    INTRODUCTION
    Wind-tunnels are test chambers built to generate precise wind speeds. Accurate scale models of new aircraft can be mounted on a force-measuring supports in the test chamber, and measurements of the forces on the model can be made at different wind speeds and angles. Some wind tunnels can operate at hypersonic velocities, generating wind speeds of thousands of miles per hour. The sizes of the wind-tunnel test chamber vary from a few inches across to sizes large enough to accommodate a jet fighter. At the completion of a wind-tunnel test series, many sets of data have been collected that can be used to determine lift, drag, and other aerodynamic performance characteristics of a new aircraft at its various operating speeds and positions.

    The collected data points can be used to estimate the performance of the aircraft at speeds and positions that are within the range of the experimental measurements but not equal to the original data. This is called interpolation. There are various ways of doing interpolation, the easiest of which is probably linear interpolation. For example, suppose we have data points (a, f(a)) and (c, f(c)). If we want to estimate the value of f(b), where a < b < c, we could assume that a straight line joined f(a) and f(c) and then use linear interpolation to estimate the value of f(b). The formula to do this is [f(a)-f(b)]/[b-a] = [f(a)-f(c)]/[c-a]. A nice illustrated explanation of linear interpolation can be found on pages 52 and 53 of your textbook.

    SPECIFICATIONS
    The input file tunnel.txt contains 17 wind-tunnel measurement pairs which consist of a flight-path angle (in degrees) and its corresponding coefficient of lift on each line in the file. The flight-path angles are in ascending order. Write a program that reads the wind-tunnel data into an appropriate array(s), prints a message showing the range of angles that are covered in the data file, and then allows the user to enter a flight-path angle. If the angle is within the bounds of the data set, the program should then use linear interpolation to compute the corresponding coefficient of lift and display it on the screen. If the user’s input is outside the range, the program should print an error message indicating so. The program should keep asking the user to enter flight-path values as long as the user desires. A sample program run using the input file tunnel.txt is shown below.

    Your program should be contained in a single source code file called lastname_prog6.cpp (where lastname is your last name) and must follow the code format guidelines for this class (click here for these guidelines).


    Here is the sample run:

    SAMPLE RUN
    The following shows a sample program run using the input file tunnel.txt (user input in bold).

    Range of flight-path angles: -4 to 21 degrees

    Please enter a flight-path angle in degrees: -3
    Lift coefficient for -3 degrees: -0.119

    Would you like to enter another value (Y/N)? y
    Please enter a flight-path angle in degrees: -5
    ****Sorry, -5 is not within data range****

    Would you like to enter another value (Y/N)? Y
    Please enter a flight-path angle in degrees: 5.5
    Lift coefficient for 5.5 degrees: 0.59575

    Would you like to enter another value (Y/N)? N


    Here is the data file "tunnel.txt":


    -4 -0.182
    -2 -0.056
    0 0.097
    2 0.238
    4 0.421
    6 0.479
    8 0.654
    10 0.792
    12 0.924
    14 1.035
    15 1.076
    16 1.103
    17 1.120
    18 1.121
    19 1.121
    20 1.099
    21 1.059


    And here is my code:
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    //Define constants and declare function prototypes
    const int T=100;
    double maxval(const double x[], int n);
    double minval(const double x[], int n);
    
    int main()
    {
    	// Declare objects
    	int tpts(0);
    	double y[T], flight_range, coef_lift;
    	string filename;
    	ifstream tunnel;
    
    	// Prompt user for file name and open date file.
    	cout << "Enter the name of the data file";
    	cin >> filename;
    	tunnel.open(filename.c_str());
    	if (tunnel.fail())
    	{
    		cout << "Error opeing input file\n";
    	}
    	else
    	{
    		// Read a data value from the file
    		tunnel >> flight_range >> coef_lift;
    
    		// While there is room in the array and 
    		// end of file was not encountered,
    		// assign the value to the array and
    		// input the next value.
    		while (tpts <= T-1 && !tunnel.eof())
    		{
    			y[tpts] = flight_range;
    			tpts++;
    			tunnel >> flight_range;
    		}
    
    		// Find and print the maximum value
    		cout << "Range of flight-path angles: " << minval(y,tpts) << " to " << maxval(y,tpts) << " degrees"<< endl;
    			
    		
    
    		// Close file and exit program
    		tunnel.close();
    	}
    return 0;
    }
    
    /* ------------------------------------------------------*/
    /*   This function returns the maximum					 */
    /*   value in the array x with n elements				 */
    
    double maxval(const double x[], int n)
    {
    	// Declare local objects
    	double max_x;
    
    	// Determine maximum value in the array
    	max_x = x[0];
    	for (int k=1; k<=n-1; k++)
    	{
    		if (x[k] > max_x)
    			max_x = x[k];
    	}
    
    	// Return maximum value
    	return max_x;
    }
    
    /* ------------------------------------------------------*/
    /*   This function returns the minimum					 */
    /*   value in an array x with n elements				 */
    
    double minval(const double x[], int n)
    {
    	// Declare objects
    		double min_x;
    	// Determine minimum value in the array
    		min_x = x[0]; 
    		for (int k=1; k<=n-1; k++)
    		{
    			if (x[k] < min_x)
    				min_x = x[k];
    		}
    
    	// Return minimum value
    		return min_x;
    }
    I found out how to do the range, but nothing else. I know this is long but I can really use the help.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I don't know what you learned in class, but the basic rule when you are reading from a file is: the read statement should be your while conditional, e.g.
    Code:
    while(tunnel>>myVar)
    {
    
    }
    tunnel>>myVar calls a function that reads data into myVar and then returns the object tunnel, leaving you with:

    while(tunnel)

    The tunnel object will evaluate to false in the while conditional if any errors have occured that will prevent you from reading anymore data from the file. eof is considered an error, so the while loop will terminate correctly when you reach the end of the data without having to count the data.

    However, there are other possible errors that can occur while reading from a file. If one of those errors occurs, your current while loop will try to keep reading data because it hasn't encountered eof yet(and the counter hasn't reached 17)--but the error will prevent you from reading any data, and you won't know that you aren't reading in data. After the loop terminates due to the counter reaching its max, you'll think you successfully read in 17 lines of data. If you put the read statement in the while conditional instead, the loop will end immediately if there's any error, so you can check a counter to see if you read in 17 pieces of data and therefore you are good to go.

    By the way, the instructions say the data is in ascending order by flight angle, so while your maxval() and minval() functions would apply to data that isn't ordered, the minimum angle is actually just the first angle in the data file, and the max angle is the last angle in the data file. Your instructor apparently wanted to make the assignment easier and eliminated the need to write those functions. If I were you, I would read the data into a two dimensional array. Row 0 will have the angle in column 0 and the lift in column 1, row 1 will have the next angle in column 0, and the next lift in column 1, and so forth.

    Then prompt the user to enter an angle. After that, check that the angle is within the range: if it isn't display an error message, and prompt them again. If it is within the range, then start at row 0 in your array and check to see if the angle in column 0 is larger than the given angle. Check the rows until you find the first row whose angle is larger than the given angle. Record that row number. The angle that is lower than the given angle will be the recorded row number minus 1. That will give you the two rows whose angles surround the given angle. The lifts are in the second column of each of those rows. Then you do the math to find the lift and display it.

    Work on that for awhile.
    Last edited by 7stud; 11-06-2005 at 02:05 PM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Ok I'm really having trouble. I don't know what to do. It keeps returning -2 for my range instead of -4.
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    
    
    int main()
    {
    	double range[17][2] = {0}, nrows, ncols, sum;
    	ifstream tunnel;
    
    	tunnel.open("tunnel.txt");
    
    	tunnel >> nrows >> ncols;
    	for (int i=0; i <= 16; i++)
    	{
    		for (int j=0; j<= 1;j++)
    		{
    		tunnel >> range[i][j];
    		
    		}
    	}
    	cout << range[0][0] << range[16][0];
    	
    	
    return 0;
    }
    Explain a little more what to do with the while(tunnel) conditional. I appreciate your help.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    This code returns my range of points.
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
    	double range[17][2], myVar;
    	ifstream tunnel;
    
    	tunnel.open("tunnel.txt");
    	if (!tunnel.fail())
    	{
    		for (int i=0; i<=16; i++)
    		{
    			for (int j=0; j<=1; j++)
    			{
    				tunnel >> range[i][j];
    			}
    		}
    	}
    	tunnel.close();
    	cout << range[0][0] << range[16][0];
    	
    return 0;
    }

  5. #5
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Personally I use while(in_file.good()) in_file >> var;

    I'm not that sure about while(in_file >> var), would it still return false on errors other than EOF and all?

    It keeps returning -2 for my range instead of -4.
    So what it looks like you're trying to do is return the upper and lower value for your values? How can you get a negative range?

    Nothing immediately jumps out at me about your code that should keep it from reading in the correct data.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Explain a little more what to do with the while(tunnel) conditional. I appreciate your help.
    It's pretty simple: you make your read statement the while conditional.

    1) Write your read-from-file statement on any line you want.
    2) Cut and paste it into the while conditional. Voila!. You've just made your read statement the while conditional.

    If you know what a function is, then you should know that a function call like:

    double avg = average(myIntArray);

    causes the function call(what's on the right side of the equals sign) to be replaced by the return value of the function. So, when the function average() is called in that line, the function executes and returns a value which replaces the function call, so you end up with something like:

    double avg = 32.45;

    Well, you may not realize it but when you write:

    myFile>>myVar;

    that actually calls a function that reads data from the file in a certain way and puts what it reads into myVar; and after the function executes, that whole statement is replaced by the return value of the function, which is defined to be whatever object is on the left side of '>>'. So, you could write:

    ifstream someOtherIFstreamObject = myFile>>myVar;

    and that would execute a function and read from the file and then return myFile, which in turn would replace the function call (i.e. myFile>>myVar) with myFile, leaving you with the statement:

    ifstream someOtherIFstreamObject = myFile;

    ----
    I'm not that sure about while(in_file >> var), would it still return false on errors other than EOF and all?
    tunnel>>myVar calls a function that reads data into myVar and then returns the object tunnel, leaving you with:

    while(tunnel)

    The tunnel object will evaluate to false in the while conditional if any errors have occured that will prevent you from reading anymore data from the file. eof is considered an error, so the while loop will terminate correctly when you reach the end of the data without having to count the data.

    However, there are other possible errors that can occur while reading from a file.
    ----
    [edited out some incorrect stuff here]
    Last edited by 7stud; 11-06-2005 at 08:50 PM.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    I get what you are saying but I can't figure out how to read the data file into an array with out using the for loops. Aren't the for loops required for arrays? How would I store it a different way?

    So I will have a while(tunnel >> angle >> coef) and then some junk in storing the data which I don't understand. Then I have to enter a flight-path angle and check to see if its within the range which I understand how to do.

    Then if that number is in range I have to check it with the angles and display lift coef. I don't understand this part.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I get what you are saying but I can't figure out how to read the data file into an array with out using the for loops. Aren't the for loops required for arrays? How would I store it a different way?
    I forgot you were reading into an array. Sorry. Ignore what I posted about that.

    So I will have a while(tunnel >> angle >> coef) and then some junk in storing the data which I don't understand.
    What do you mean there?

    Then if that number is in range I have to check it with the angles and display lift coef. I don't understand this part.
    First you have to get the two angles that surround the user's angle(=the entered angle). I explained how to do that in my earlier post. You just use a for loop to check the first column of each row in your array. Remember, the angles are in ascending order. You can 'break' out of the loop when you find the first angle that is greater than the user's angle. Use a counter to count the rows, so then you'll know the row number that contains the higher angle. The row before that row will contain the lower angle. Those two angles will surround the user's angle. For instance if these are the angles:

    5
    10
    20
    35
    ...

    and the user enters 15. Then, you need to search the array until you find the first angle higher than 15, which is 20. The row before that will contain the angle 10. Those are the two angles that surround the user's angle.

    Then, you need to calculate the lift. Notice the user's angle is half way between the surrounding angles. That means the corresponding lift will be half way betwen the two lift values. If the lift values corresponding to the surrounding angles are 1.3 and 1.4, then the user's estimated lift will be 1.35. Half way is the easy case. What if the user's angle was 12, and the surrounding angles were 10 and 20? The user's angle is equivalent to the lower angle plus 20% of the distance between 10 and 20. If the corresponding lifts are 1.3 and 1.4, then the user's lift will be the lower lift plus the same 20% times the difference between the two lifts or 1.3 + .2(1.4 - 1.3) = 1.3 + .02 = 1.32. The calculated lift is 20% along the range created by the two lifts. The user's angle was 20% along the range created by the surrounding angles, so you calculated a lift that was 20% along the range created by the two lifts.

    The general case is: take the user's lift, subtract the lower surrounding angle and divide by the difference of the two angles. That gives you where the user's angle falls in the range created by the surrounding angles--in percentage terms. In the example, the user's angle was located 20% of the range "distant" from the lower angle. You need to find that same location in the range created by the corresponding lifts. So, take the percentage you calculated and multiply it by the difference in the lifts, and then add that result to the lift corresponding to the lower surrounding angle.
    Last edited by 7stud; 11-06-2005 at 09:51 PM.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    Ok sorry to keep bothering you, but I'm not sure how to store the data file in an array. It is supposed to be a two-dimensional array right? I worked on it and had range[0][-1] and range [16][-1] for the range of degrees. Somehow the data files arent being stored right.

    For comparing a user entered degree to the rest, how do you run through all of the array to compare the file? I understand what you arent saying, but I don't understand the code. Is there anyway that you will post some example code of how its done.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Ok sorry to keep bothering you, but I'm not sure how to store the data file in an array. It is supposed to be a two-dimensional array right?
    Yes.

    I worked on it and had range[0][-1] and range [16][-1] for the range of degrees. Somehow the data files arent being stored right.
    Huh? Your array has rows numbered 0 to 16, and each row has elements at index 0 and index 1. What is range[0][-1]?

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Also, have you ever heard anyone tell you: ALWAYS initialize your variables?

    I ran your code on a data file containing the data you posted and it displayed -4 and 21 as the two angles for the range. I only altered your output statement to this:
    Code:
    cout << range[0][0]<<endl
            << range[16][0]<<endl;

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    There is an easier way to read in the data though. Start with this:
    So I will have a while(tunnel >> angle >> coef)
    After that, you have to figure out how to get angle and coef into your 2d array. The while loop is going to continually loop and read two values. You need to put those two values in your array for every loop. Attempt to write a statement that will do that. What index value is going to vary every time through the loop and what index values are going to remain identical? Write down a couple of generic statements that assign angle and coef to the array using three variables for the different index values: x, y, z. Next, imagine the while loop executing those assignment statements over and over again. In the assignment statements you wrote down, does x need to change? y? z?

    You don't need a for-loop to vary an index value. You can do this:

    Code:
    int i = 0;
    
    while(...)
    {
        i++;
        array[i] = 30;
    
    }
    Last edited by 7stud; 11-07-2005 at 12:45 AM.

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    16
    I've tried different ........ and can't seem to get anything working. I'm really frustrated and I don't know what to do. Thanks for trying to help.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Show me what you did starting your while loop like this:

    Code:
    double data[50][2] = {0};
    double angle = 0;
    double coef = 0;
    
    while(tunnel >> angle >> coef)
    {
           //assign angle and coef to the array
    }
    If you have to, pretend there is only one set of data in the data file, and the while loop will only execute once. Then, write the code to assign angle and coef to the array.

    Another thing: are you aware you have to give the complete path name to your data file when you enter it? For example:

    "C:\\MyDocuments\\data.txt"

    (Note the double slashes.)
    Last edited by 7stud; 11-07-2005 at 02:12 AM.

  15. #15
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    If you can't read data from a file, then try starting with the data already in the array. Write the program and get everything else working. Later, you can worry about reading from the file to fill the array.

    Have you ever written a program that successfully read data from a file? If so, you might want to take a look at it and compare it to your current program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM