10:58 PM 2/27/2004
CS 111 Assignment 3
For information about the due dates, including late dates and extra-credit early due dates, please see Homework policies and due dates.
Below are tutorials and example programs and data files. Please make printouts of these and bring them with you to both lecture and recitation. However, please do NOT print them out in an on-campus lab. (On-campus printers are to be used only for your homework, i.e. for files YOU wrote.) If you do not have a computer at home, with a printer, ask a friend or classmate to print out copies of the following files for you.
If you have not done so already, please read Skansholm, § 3.1 to 3.3, in addition to reading the tutorals for this assignment.
On forbin, create a directory named hw03 inside your homework directory. Change your present working directory to hw03 and then copy into it the example files for this homework assignment, by typing, at the "forbin>" prompt:
cp ~nixon/cs111/hw03/* .
Please do NOT copy these files into your home directory, to avoid cluttering your home directory. If you inadvertantly copied them into your home directory, move them out using the mv command. Be very careful about deleting anything in your home directory, to avoid inadvertantly deleting your hidden files (.login, .cshrc, .profile, etc.).
Play with the example programs when reading the tutorials. Then, when you are ready to begin work on the homework assignment itself, it is recommended that you first delete the example files, in order to save disk space.
Answers to the practice problems are provided here. However, so that you will be adequately prepared for the next quiz, it is strongly recommended that you work on the problems yourself before looking at the answers. It is also recommended that you work on at least some of these practice problems before doing the assigned homework.
In response to the following problems, write complete programs unless you are told to do otherwise. For some problems, you will also be asked to write code traces.
It is strongly recommended that you plan your algorithms informally in paper before writing the programs themselves.
The factorial of a number is equal to 1 if the number is 0. If the number is positive, the factorial is equal to the product of all the integers from 1 up to the number.
Create a line-numbered copy of this program, so that you can more conveniently write code traces.
Make code traces for several different cases, including boundary cases. Include both valid cases and at least one error case.
Use as few if statements as possible. To accomplish this, use the appropriate one of these boolean operators:
operator meaning && and || or ! not
A string is considered to represent a natural number if it contains at least one character (i.e. it is not an empty string) and all of its characters are digit characters. Note that checking whether all the characters are digit characters can be accomplished by checking whether the string contains any non-digit characters. Thus, you may use an "innocent until proven guilty" technique similar to the one used in checkNonASCII.cpp, as discussed in the Tutorial on for loops, and more about characters and strings, to check whether a string contains any non-ASCII characters. To ensure that the string is nonempty, use cin's extraction operator for input.
Note: To gain maximum benefit in terms of quiz preparation, it is strongly recommended that you write the program from scratch, rather than by modifying checkNonASCII.cpp. Look at checkNonASCII.cpp for a clue, then write the program without looking at checkNonASCII.cpp further.
Create a line-numbered copy of this program, so that you can write code traces. Write code traces for the following inputs:
12390 0 9 / : a2 2a
Hint: Recall that, in the ASCII chart, the uppercase letters are all grouped togegher in one part of the chart in alphabtical order, and the lowercase letters are grouped together in another part of the chart, also in alphabetical order. Thus, the difference in ASCII values between 'a' and 'A' is the same as the difference in ASCII values between 'b' and 'B', and is the same as the difference in ASCII values between 'c' and 'C', and so on. Thus, a lowercase letter can be capitalized by adding (or subtracting) this constant difference. (Note: In the interests of program readability, it is better to compute this difference by an expression like 'A' - 'a' (or 'a' - 'A') rather than to compute the difference in your head and use a numeric literal in your program. If you use the number directly, its significance will not be obvious to the reader without a comment.)
First, write the program without any error handling. Then, after you test the program for a variety of inputs, add error handling as explained below.
In your first draft of the program, use variables of type int for both the input number and the factorial. Your program should have the following output for an input of 0:
The factorial of 0 is 1.
For an input of 1:
The factorial of 1 is 1.
For an input of 11:
The factorial of 11 is 39916800.
And for an input of 12:
The factorial of 12 is 479001600.
But, for an input of 13, your program will have the following incorrect output:
The factorial of 13 is 1932053504.
You can tell that the output for 13 is incorrect because it does not end in two zeroes, as the.output for 12 does. If a number's base 10 representation ends with two zeroes, then multiplying it by any nonzero integer, such as 13, should yield another number whose base 10 representation ends with at least two zeroes.
Why the incorrect result? Because the correct result, 6227020800, exceeds the highest possible number in the int range, which is 2147483647.
You can make the program output a correct result for 13 by declaring as type long, rather than type int, the variable which holds the cumulative product you use to compute the factorial. (However, the input number should still be of type int, for compatibility with the parseForbinInt function which you use to interpret the command-line argument as an int value.)
Change the data type of the factorial to long. Then determine, experimentally, how much higher the input can go and still yield a correct result, without exceeding the highest possible value in the long range. To determine this, check the program with every possible input starting at 13, increasing the input by 1 each time you run the program, until you encounter an output which is obviously wrong, e.g. an output number which does not end in 00. You will not need to check very many inputs; you will arrive at the limit very soon.
Once you've determined the maximum input that will work correctly, add error handling. Your program should display appropriate user-friendly messages if (1) there are not enough command-line arguments, (2) the command-line argument represents a negative integer, or (3) the command-line argument represents a number which is too large for the computed factorial to be accurate, as you've determined experimentally above.
x x x x
1 + 2 + 3 + ... + n
For example, if the program is run as follows:
a.out 5 2
it should, in this case, print out the number 55, the sum of the squares of 1, 2, 3, 4, and 5. Or, if the program is run as follows:
a.out 2 0.5
it should, in this case, print out the sum of the square roots of the numbers 1 and 2, which is approximately 2.41421.
If the integer n is negative or zero, the program should print an error message and quit. Otherwise, it should compute the above sum and print it out.
To compute powers, you may use the pow function in the library. Its prototypes are in the <cmath> header file. Use the version of the pow function which has the following prototype:
double pow(double x, double y);
Given the arguments specified in the above prototype, the pow function returns the value of x raised to the y power.
Note that an int value will automatically convert to a double with no problem, if passed as an argument to a function which expects a double value.
The error state of your ifstream object should be checked after opening the input file and after every input from the file. The error state of your ofstream object should be chedked after opening the file and at the end of the program, after all output is finished.
Test your program for correctly-formatted input files such as numbers2.txt and FileWithAVeryLongName.txt, and test it also for nonexistent files and for incorrectly-formatted input files such as table.cpp itself.
Run the script file hw03.sh to make sure your programs work correctly and that they have the correct filenames. The script file hw03.sh can be copied into your present working directory as follows:
cp ~nixon/cs111/hw03/hw03.sh .
Then, create a tar file as you did for earlier assignments. The tar file must have a filename in exactly the following format:
lastname_accountname_3.tar
replacing lastname with your actual last name (spelled as the Registrar's Office spells it, as on your tuition bill or bursar's receipt) in lower-case letters only (not capital letters), and replacing accountname with your actual forbin account name. The "3" indicates Assignment 3.
The tar file must contain the following source code files, and only these files:
The tar file must be submitted to your instructor at the appropriate E-mail address, as listed on the instructor E-mail address page.
To recitation/lab on the due date, bring a stapled-together bundle of printouts of the following source code files, which you wrote:
Your printouts must be stapled together in exactly the order listed above.
Back to: