ENSF 614 Lab 1


Category: You will Instantly receive a download link for .zip solution file upon Payment


5/5 - (1 vote)

Exercise A: Creating a C++ source file, building and running an executable
Read This First:
If this is your first attempt to develop a C++ program, please do it so that you start to become
comfortable with program development in C++ and particularly using your favorite development
environment such as Cygwin, Xcode, or simple editor such as Notepad++.

What to Do:
• Startup favorite editor or IDE.
• Into the editing area, type exactly in all the following C++ code:
• Now save your file as: lab1exe_A.cpp
• If you are using a text editor and Cygwin on your Windows OS, within Cygwin terminal navigate
to your working directory (the same directory that you saved your lab1exe_A.cpp,then on
the Cygwin command line enter:
g++ -Wall lab1exe_A.cpp

an executable file called a.exe will have been created. If the command fails — which will be
indicated by one or more error Messages–go back to your editor and fix the code, save the file
again, try g++ again.

• If you are using Mac OS, and Xcode IDE, press the run button to compile and run your program. Also,
Mac users can use the Mac terminal and follow exactly the same steps mentioned above, for Cygwin, to
use the g++ command and compile the program. However, instead of executable file a.exe file will be
called a.out.

• Once you have an executable, run it a few times by using the command
./a.exe (or ./a.out on the terminal of Mac machine)
over and over.

Try different inputs each time; see what happens if you enter letters or punctuation instead of

Hint: You don’t need to type ‘./a.exe or ./a.out over and over! You can use the up arrow on
your keyboard to retrieve previously entered commands.
You don’t need to submit anything for exercise A.

Exercise B – A C++ Program with User-Defined Functions (8 marks)

Read This First
In physics, assuming a flat Earth and no air resistance, a projectile launched with specific initial
conditions will have a predictable range (maximum distance), and a predictable travel time.

The range or maximum horizontal distance traveled by the projectile can be approximately calculated
g is gravitation acceleration (9.81 m/s2
θ: the angle at which the projectile is launched in degrees
v: the velocity at which the projectile is launched
d: the total horizontal distance travelled by the projectile

To calulate the projectile travel time (t), when reaches the maximum horizontal distace the followig
formaula can be used :
In this exercise you will complete a given C++ source file called lab1exe_B.cpp that prompt the
user to enter a projectile’s initial launch velocity (), and displays the table of maximum horizontal
distance and travel time for the trajectory angles of 0 to 90 degrees.

What to Do:

First, download file lab1exe_B.cpp from D2L. In this file the definition of function main and the
function prototypes for four other functions are given.

Your job is to complete the definition of missing
functions as follows:
Function create_table: which is called by the main function, receives the projectile initial velocity and
displays a table of projectile’s maximum travel distance (d) and time (t), for trajectory angles of 0 to 90
(degrees), with increments of 5 degrees.

Here is the sample of the required table:
Angle t d
(deg) (sec) (m)
0.000000 0.000000 0.000000
5.000000 1.778689 177.192018
10.000000 3.543840 349.000146

You don’t have to worry about the format or the number of digits after the decimal point. The default
format is acceptable.
Function projectile_travel_time: receives two double arguments, the trajectory angle (), and the
initial velocity () and returns projectile travel time (t).

Function projectile_travel_distance: receives two double arguments, the trajectory angle (), and
the initial velocity () and returns projectile maximum horizontal distance (d).
Function degree_to_radian: receives an angle in degrees and converts to radian. This function is
needed, because C++ library function sin needs its argument value to be in radian.

• To use library function sin, you need to include header file cmath.
• Please pay attention to constant values of , and gravitation acceleration, g, the following lines
are already included in the given file:
const double PI 3.141592654
const double G 9.8

• While running your program, try a few times to enter a negative value or invalid input (for
example, instead of a number enter xyz), for velocity, and observe how the program reacts.

What to Submit:
Submit the copy of your program (your code and the program output) as part of your lab report in PDF
format. You don’t need to upload your actual source code. Only it must be copied and pasted into your
lab report along with the program’s output.

Exercise C – Introduction to Pointers

This exercise will not be marked, and you shouldn’t submit anything.
Read This First
This is an important exercise in ENSF 614. If you don’t become comfortable with pointers, you will not be able to
work with C++. Spend as much time on this exercise as is necessary to understand exactly what is happening at
every step in the given program.

What to do
Download the file lab1exe_C.cpp . Trace through the execution of the program to determine what the output
will be. Now assume the following addresses for variables:

sam 9880
fred 9884
bar 9888
foo 9892

Draw a set of AR diagrams for points two through five and use the given address numbers as values of the
pointers (don’t use arrow notation in this exercise). To understand how to draw these diagrams the solution
for point one is given in the following figure:

After you completed the exercise, compile lab1exe_C.cpp and check your predicted output against the actual
program output.

Exercise D: Pointers as function arguments

What to do – Part I
First copy the file lab1exe_D1.cpp from D2L. Carefully make diagrams for point one in this program, using
“Arrow Notation” as we discussed during lectures (you don’t need to use made-up addresses, the way that we
did it in the previous exercise). Then compare your solution with the posted solution on the D2L.
There is nothing to submit for this part

What to do – Part II
Now download the file lab1exe_D2.cpp from D2L and draw AR diagram for point one in this file.
Submit the AR diagram for part II as part of your lab report.

Exercise E: Using pointers to get a function to change variables

Read This First
Here is an important note about terminology:
• Don’t say, “Pointers can be used to make a function return more than one value.” A function
can never have more than one return value. A return value is transmitted by a return
statement, not by a pointer.

• Do say, “Pointer arguments can be used to simulate call by reference,” or, “Functions with
pointer arguments can have the side effect of modifying the variables that the pointer
arguments point to.”

What to do
Make a copy of the file lab1exe_E.cpp from D2L. If you try to compile and run this program it will
give you some warning and displays meaningless output because the definition of function
time_convert is missing.

Write the function definition for time_convert and add code to the main function to call
time_convert before it prints answers.

Submit the copy of your source code and the screenshots of the program output, as part your lab