By the time you have completed this lab, you should be able to
By now you should know the drill: Choose who will be the first pilot and who will start as navigator, and then remember to switch (at least once) during the lab. But you should probably know the long-term goal too:
Each partner should participate in both roles in approximately equal parts over the course of the quarter. We realize it is not possible to equally split time in every lab, but it's worth trying, and it is possible to make up for unequal splits in future labs. We trust you will try to meet this goal. Thanks!
If your assigned partner is more than 5 minutes late, ask the TA to pair you with someone else for this week.
This lab's first pilot should log in, create ~/cs16/lab03/ and make that your current directory.
Start ch, and simplify the ch prompt if you want:
-bash-4.3$ ch Ch (64-bit) Professional edition, version 7.5.3.15401 Copyright (C) SoftIntegration, Inc. 2001-2016 http://www.softintegration.com /cs/class/cs16/lab03> _prompt = "ch> " ch> ch>
Type (or copy/paste) the function below, int f1(int value)
,
all on one line at the ch prompt. In order to spread the definition over more than
one line in ch, it is necessary to type a continuation character (the backslash, '\
')
at the end of all but the last line:
int f1(int value) \ { \ return value * value; \ }In this case, the function is short enough that it could have been written on one line, and then it would not require the continuation characters.By the way, here is the function described in words like the instructions below for functions that you will create yourself:
Write a function namedf1
that takes oneint
argument named value, and has a return type ofint
. It should return the squared value.
Then test it. Here is how it should look:
ch> int f1(int value) \ ch> { \ ch> return value * value; \ ch> } ch> f1(4) 16
If you made a mistake or want to change something, you can't just redefine the function in the same ch session. Instead exit ch, and start over.
Now it's your turn to write one.
Write a function (either all on one line, or using the continuation character) namedf2
that takes oneint
argument named x, and returns adouble
value equal to1.0/x
(the multiplicative inverse of x). Do NOT print anything. Type your function definition at the ch prompt. Then test it a few times:
ch> CENSORED: FUNCTION DEFINITION HERE ch> f2(100) 0.0100 ch> f2(0) inf ch> double answer = f2(25); ch> answer 0.0400 ch>
Notice in the last test above, how the function returns an answer without printing anything - in this test, the answer is stored in a variable and shown later. Also notice the special value ch printed when the function tried to calculate 1.0/0 and keep this behavior in mind.
If you create a file with .chf
as the extension, and the first part
of this file's name exactly matches the name of the function (to be used externally),
and the file is stored in the current directory (or elsewhere in _fpath
),
then you can execute the function at the ch prompt.
Use emacs or another editor to create a file named dfactorial.chf
. [Note
you don't have to exit ch to start the editor.] Type a comment
such as (// calculates factorial of n as double
) on the first line. Then define
a function to find n factorial as a double value.
In case you never heard of it, or you forgot its definition, n factorial is abbreviated
as n! and it is defined as follows:
n! = (n)(n-1)...(2)(1)
where n is a non-negative integer, and 0! = 1. So, for example:
5! = 5*4*3*2*1 = 120
Type the function definition after the comment. Here are the specific instructions:
Write a function nameddfactorial
that takes oneint
argument named n, and returns adouble
value that is equal to n factorial.
Save the file as dfactorial.chf
in the lab03 directory.
Then test it from the ch prompt. If something doesn't work correctly, then fix the problem before going to Step 4. Here are some correct answers to expect:
ch> dfactorial(5) 120.0000 ch> dfactorial(10) 3628800.0000 ch> dfactorial(50) 30414093201813375576366966406747986832057064836514787179557289984.0000
First: switch roles between pilot and navigator if you did not already do that.
Of course you noticed that 50! is a very large number, but apparently it still is
within the range of the double
data type. What value results when you plug
in 200 for n? Now we want you to experiment a bit.
dfactorial(n)
returns an accurate value.Here, e refers to the mathematical constant that is the base of natural logarithms. Like π and other such constants, this number is irrational, and so it can only be approximated (2.71828183 is a usable approximation, but nevermind that for now).
One way to define the true value of e is by the sum of an infinite series of multiplicative inverses of factorials:
Recall that 0! is 1, so this series begins with 1 + 1, its third term is 1/2! (= 1/2), its fourth term is 1/3! (= 1/6), and so on. Of course you can't write a program to calculate an infinite series, and besides that you know your factorial function has a largest value for which it works correctly.
Write a C++ program that contains a copy of your dfactorial
function, and a new
main
function that uses it to approximate and print the value of e. First
exit ch, then accomplish the following steps:
-bash-4.3$ cp dfactorial.chf e.cpp
-bash-4.3$ emacs e.cpp
main
function
as usual. You might as well terminate main now too, and include the following features
inside its brackets (curly braces):
As a reminder, in order to print the value with 8 digits after the decimal, type these lines before using cout to print the number:cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(8);
Make an executable version of the program, and test it before submitting in the next step:
-bash-4.3$ make e g++ e.cpp -o e -bash-4.3$ ./e e is approximately 2.71828183
If you are still logged on, and your current directory is still the same as your program, then the quickest way to submit is by entering the following command (suggest you copy/paste):
~submit/submit -p 947 e.cppOtherwise you can use the submit.cs interface at https://submit.cs.ucsb.edu/ from your web browser. Either way, be sure to wait for the results of the only test. Fix any problems, and resubmit as necessary until your score is 50/50 points.
If you are working with a partner, be sure that both partners' names are in a comment at the top of the source code file, and be sure to properly form a group for this project in the submit.cs system.
Each student must accomplish the following to earn full credit [50 total points] for this lab:
The deadline for submitting your work for credit is tonight by 11:59 pm.
If you finish before the end of your lab period, work on the optional challenges below.
Optional Extra Challenges
- Convert the statements that approximate e (in Step 5) to a function. Then you can greatly simplify the main function to just print the results of this new one.
- In truth, your program does not really have to add terms all the way to the maximum factorial value that can be calculated by your Step 3 function. In fact, a much shorter series will achieve a result that is accurate to 8 digits. Devise a way, and find out the maximum value of n that is necessary to achieve this accuracy.
- Perhaps you experimented to find the answer to the previous challenge. Another way to approach problems like this one is to check how much the approximation changes each iteration, and stop iterating when this change is negligible (very nearly 0). Try that approach.
- The exponential function ex may be defined by a very similar infinite series of terms:
Write a function to calculate it. Then write a program that asks the user to enter a value of x, and prints ex with a nice label.
Prepared by Michael Costanzo.