TDT4102, Spring 2009
Exercise 2
Deadline: 06.02.2009
The objective of this exercise:
- learn the basics of functions
- learn the difference between pass-by-value and pass-by-reference
- try overloading functions
- Learn to use the assert macro
- Learn to add source code from other files using headers
General requirements:
- use Visual Studio or another IDE of your choice
- use exact name and specification when given in the exercise
- all parts should be done in the same file, except when explicitly promted otherwise ( part 7 )
Recommended reading:
- Chapter 3 & 4, Absolute C++ (Walter Savitch)
Part 1: Function headers (10 pt.)
Give the function headers (only) for each of the following functions:
a) Function hypotenuse that takes two double-precision, floating-point arguments, side1 and side2, and returns a double-precision, floating-point result.
b) Function sum that takes three integers, x, y and z, and returns an integer.
c) Function instructions that does not receive any arguments and does not return a value.
b) Function intToDouble that takes an integer argument, number, and returns a double-precision, floating-point result.
Part 2: Functions and Procedural abstraction (10 pt.)
a) Create a function printTime that takes as input three integers, hour, minute and second, and prints them to screen.
The input of 3, 15 and 7 should produce output looking something like this:
3 hours, 15 minutes and 7 seconds.
a) Create a function getTime that takes as input the number of seconds and prints the same as the function in a), returning nothing.
You can use the code from the first exercise (øving1) to make things easier. Use the function from a to print the computed numbers.
Part 3: Use of standard functions (15 pt.)
In this exercise you will create functions that use a variety of the available functions in the standard libraries of c++. You will have to decide what functions to use
yourself, a good hint in finding these is appendix 4 of the course textbook.
a) Create the function randomOneToFifty that creates a random integer number between and including ten (10) and fifty(50).
b) Create the function checkTrigonometry that takes a single floating-point number as input and checks whether the mathematical rule tan(x)=(sin(X) / cos(X))
holds for this number. Return a number indicating the difference between the tan(x) and ( sin(x) / cos(x) ) values, indicating if this rule holds for the given number.
Comment on the answer using your knowledge of how floating point numbers work.
You can assume the number is a well-behaving one ( this means you can assume that x will not cause division by zero, and that the tangent can be calculated ). No input checking is neccesary.
c) Create the function changeCase that takes as input a single character, and changes the case. That is, if the character is a lowercase letter, the function
should return an uppercase letter. If the input character is an uppercase letter, the function should return a lowercase letter. A char in c++ can be declared like this:
char mychar = 'a';
Part 4: Various functions and the assert macro (15 pt.)
The National Aeronautics and Space Administration (NASA) has, after a number of disasters lately, decided that they should try doing some
calculations before their next launch. Deciding not to go overboard with this radical new thinking however, they decide an underpaid Norwegian
student should be more than capable of providing the needed calculations. You are, incidentally, that Norwegian student.
Because of the intricate bureaucracy of NASA, and because the author of this exercise is becoming somewhat lazy, you have been given a number of
functions to implement, some of them seemingly unfitting to the problem area. You are free to give the functions fitting names.
a) Create a function that computes the kinetic energy of an object in motion. The function should take as input the object's mass in kilograms
and the object's velocity in meters per second. The function should return the object's kinetic energy.
The (non-relativistic) function for kinetic energy is:
KE = (1 / 2) mv^2
b) Create a function that computes the potential energy of an object in a gravitational field. The function should take as input the
object's mass, the objects height ( above an arbitrarily assigned reference point ) and the gravitational force and return the object's
potential energy.
The (non-relativistic) function for potential energy is:
PE = mgh
c) Create a function that tests the two functions using the assert macro. The function should call both functions several times to test them thoroughly.
Solve the equations by hand to obtain the necessary test data, and call this function from the main function to assure that everything is working smoothly.
Part 5: Call-by-value / call-by-reference(20 pt.)
In this part you will create the same function using two different approaches, call-by-value and call-by-reference. You should read up on these
two techniques before doing the exercise, so you are certain you are doing things correctly.
The function to implement is simply:
((A^2 + 10) * 2)
Where A is the input integer.
a)Create the function using call-by-value. You should take an integer as input and return an integer.
b)Create the function using call-by-reference. You should take an integer reference as input and return nothing. The result of the function
should be written to the integer referenced by the input.
Part 6: Function overloading (15 pt.)
In this exercise you will create a lot of functions with the same names, but thanks to the magic of computers and a fair bit of divine intervention,
everything will work out in the end anyways. Create all the functions in the same program, and test them all to ensure that they are working
properly.
a)Create the function add that takes as input two integers and returns the sum as an integer.
Example: Input: 4 , 5 - Output: 9
b)Create the function add that takes as input two double-precision, floating-point numbers and returns the sum as a double-precision,
floating-point number.
Example: Input: 3.5 + 6.25 - Output: 9.75
c)Create the function add that takes as four integer references, and returns nothing, but changes the integers referenced. After beeing
called the first integer should be the same. The second integer should contain the sum of the two first integers. The third should contain the sum of
the first three, and the forth the sum of all the integers.
Example: Before function: a=1 , b=2 , c=5 , d=1 - After function: a=1 , b=3 , c=8 , d=9