Little Function Lab
October 1997
Overview
In this lab, you will write a variety of C++ functions. Some will receive input by
value, others by reference. Some will return void, others will return int, double,
or bool types. A driver is supplied to test the functions; you must supply their
declarations and definitions.
Activities
Run the application
LittleFunctionsSolution to see what your final program should do. You can scroll up and down the text window to read the output.
You must do three things to make each function work:
a Write the function declaration
b Remove a comment line in the main program
This will allow the part of main that calls that function to run.
c Write the function definition
Note:
If you do not implement the functions in the order described, you will have to add
comment line to main to block calls to undeclared or unimplemented functions.
The Functions
// (1) the distance function
The distance function takes 4 arguments x1, y1, x2, y2 of type double and returns the distance between (x1, y1) and (x2, y2).
Use the Pythagorean Theorem: c = sqrt(a2 + b2) where c is the hypotenuse and a and b are legs of a right triangle.
// (2) the factorial function
The factorial function takes one argument N of type int and returns the N!.
N! = N*(N-1)*(N-2)*...*2*1 5! = 5*4*3*2*1 = 120
Use a for or while loop to compute the factorial function.
// (3) the min function
The min function takes two arguments x and y of type int. It returns the value of the smaller input.
// (4) the Money function
The Money function takes three arguments:
Y of type int // Y is for years
D of type double // D is the initial deposit
P of type double // P is the percent interest rate
The Money function returns how much Money you have after Y years if your initial deposit
is D and interest is compounded yearly at P percent. Each year, the amount of money
you have is multiplied by 1 + P/100.
// (5) the IsSquare function
The IsSquare function takes one argument N of type int and returns true if N is a perfect square, false otherwise.
// (6) the IsLetter function
The IsLetter function takes one argument ch of type char and returns true if ch is an uppercase or lowercase letter, false otherwise. Letters with accents marks are not counted as letters, e.g. é, å.
// (7) the Roll function
The Roll function takes two arguments D1 and D2 of type int, passed by reference. D1 and D2 are each set to a random integer between 1 and 6.
//Extra Credit 1 point
// (8a) the SIN function
The SIN function takes one argument x of type double and returns sin(x), accurate to 4 decimal places, as calculated with the Maclauran
series
sin(x) = x - x^3/3! + x^5/5! - x^7/7! + . . .
Use a loop to add the terms of the series. Stop when the next term has smaller absolute
value than 10-5. ( fabs(term) < 0.00001 )
Turn in:
Printouts of the source code and of the text window after a sample run.
Diskette with project, source code, and working application.
Output of a Sample Run
distance test - fixed points
the distance between (50, 175) and (-30, 95) is 113.137
the distance between (42.7, -73.7) and (-195.4, 67.4) is 276.769
distance test - random points
the distance between (178.084, -134.404) and (-133.980, 197.783) is 455.777
the distance between (144.153, -17.2072) and (-1.79224, -122.235) is 179.808
the distance between (-1.61178, 110.824) and (-173.659, 117.567) is 172.179
factorial test
N N!
0 1
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
min test
x = 28 y = 12 min = 12
x = 69 y = 9 min = 9
x = -62 y = -21 min = -62
x = 56 y = 42 min = 42
x = 62 y = -77 min = -77
Money test
years amount
0 $ 1000.00
5 $ 1435.63
10 $ 2061.03
15 $ 2958.88
20 $ 4247.85
25 $ 6098.34
30 $ 8754.96
35 $ 12568.87
40 $ 18044.24
45 $ 25904.84
50 $ 37189.75
Perfect squares < 100
1
4
9
16
25
36
49
64
81
100
These chars are letters
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z
Roll test
Roll 1 3 4
Roll 2 5 2
Roll 3 2 4
Roll 4 5 6
Roll 5 4 4
SIN test
angle sin(angle)
0.0000 0.0000
0.5236 0.5000
1.0472 0.8660
1.5708 1.0000
2.0944 0.8660
2.6180 0.5000
3.1416 0.0000
3.6652 -0.5000
4.1888 -0.8660
4.7124 -1.0000
5.2360 -0.8660
5.7596 -0.5000
Last Updated: October 13, 1997 5:40 pm by
Harriet Fell
College of Computer Science, Northeastern University
360 Huntington Avenue #161CN,
Boston, MA 02115
Internet: fell@ccs.neu.edu
Phone: (617) 373-2198 / Fax: (617) 373-5121
The URL for this document is:
http://www.ccs.neu.edu/home/fell/COM1100/PROG/LittleFunctionLab.html