CPE 101
Laboratory 3b


Due Date

Objectives

Prerequisites

Resources

Part 1: Projectile Motion Equations

In this activity we are going to write a program that will perform some simple calculations using the formulas for projectile flight that you learned in physics.  For example, given a projectile's launch characteristics we would like to determine how far it will fly.

The launch characteristics are the initial angle, Ø,  and the initial velocity, v, the relevant formulas are:

1) Given a time in flight, t,  the distance traveled, d is computed as:
dv · cos(Ø) · t

2) Given a time in flight, t,  the height attained, h is computed as:
hv · sin(Ø) · t  -  ½ · G · t 2
where G is the acceleration due to gravity,  32.17 feet per second per second.


3) Sometimes we may know the desired distance, and want to determine the flight time to reach it. 
td / (v · cos(Ø))

Part 2 - The header file

The instructor has created the required header file for this activity: flightlib.h. This file provides the function prototypes you are to follow.

Part 3 - The implementation file


Create an implementation for each of the functions specified in the header file.  Put the implementations in a separate file named flightlib.c.  Note that none of the functions do any input or output to the user.  Your implementation should take advantage of the trigonometry functions defined in math.h library.  Note these trigonometry functions require the units be in radians. Do not include a main function in this file.

Because there is no main function, you will want to compile the implementation file without linking, using the "-c" flag.
gcc -c -lm -Wall -ansi -pedantic flightlib.c

Alternatively,  if you are using jGRASP, click the icon of the single green plus.

The result of compiling will be a new object file named flightlib.o.   This file can't be executed by itself;  the executable will be created below.

Part 4 - Testing your functions with a test driver
Complete the following test plan in your notebook.  Calculate by hand (or with a calculator) the results that should be expected for the following inputs.
Input
Expected Output
Angle:  30 degrees
Angle (radians) :
Angle:  30 degrees
Velocity: 800 ft/sec
Time: 12 sec
Distance (feet):
Angle:  50 degrees
Velocity: 900 ft/sec
Time: 10 sec
Height (feet):
Angle:  40 degrees
Distance: 11000 ft
Velocity: 800 ft/sec
Time (sec):

Now copy-and-paste this skeleton program into a separate file named flightlib_test.c.  This file contains only a main function and unit tests.  A sample unit test has been provided for you.  Add additional calls to checkit to invoke each of the functions you implemented and pass as input parameters the values from the test plan above. 

Next compile the unit tests without linking.
gcc -c -lm -Wall -ansi -pedantic flightlib_test.c

The result of compiling will be a new object file named flightlib_test.o.  

Finally, create an executable by linking both the implementation (flightlib.o) and the test driver (flightlib_test.o) object files together by issuing the following command:
gcc -lm flight*.o 
Issue the command in the directory where you compiled your source files.

To run the executable from the command line just issue the command:  ./a.out

If all your tests are successful, you will see only "Test passed" messages.  If any "Test FAILED" messages are output then you have to debug your program to correct the mistake.  When all your tests are passing, proceed to the next step.



Part 5 - Testing your functions with the instructor's test driver on unix1

YOU MUST LOGIN TO UNIX1 TO COMPLETE THIS STEP!

The instructor has prepared a test driver for testing your functions.  You can obtain this file from the instructor's unix1 account.  On unix1 the file is named flightlib_tester.o.  Copy this file into your account with this unix command:
cp ~graderjd/Public/flightlib_tester.o  .
(Don't overlook the period at the end of the command; it indicates the destination should be the current directory).

Recompile your implementation file: gcc -c -lm -Wall -ansi -pedantic flightlib.c
Link your object file together with the instructor's driver into an executable file:  gcc -lm flightlib.o flightlib_tester.o
To run the executable just issue the command:  ./a.out

You should see the following output:
degrees_to_radians PASSED
calculate_time PASSED
calculate_height PASSED
calculate_distance PASSED


If any of the tests fail, you must isolate the defect and correct your program.

Getting this error? File format not recognized collect2: ld returned 1 exit status
Your .c file needs to be recompiled.

Part 6 - Writing an interactive client program.

Now create a client program in a separate file named flightlib_client.c.  The client should contain only a main function.  The main function should interact with the user to obtain input values and display the corresponding results.

Sample Execution:
Enter initial velocity (ft/sec): 800.0
Enter initial angle (degrees): 17.0
Enter time in flight (sec): 14.0
The distance traveled was: 10710.613267

Part 7: Submit your completed program

Submit your completed source files electronically, using the handin procedure as in previous labs.
  1. Verify that your completed source files flightlib.c, flightlib_test.c, flightlib_client.c compile and execute on unix1.
  2. Put both student names in the header comment of each file.
  3. Use the handin command below,

  4. handin graderjd Lab03 flight*.c