Due Monday, April 10, 2006
CS 111 Assignment 5
Below are tutorials and example programs. 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.
So that you can play with the example programs, create a directory named hw05 inside your homework directory on forbin. Change your present working directory to hw05 and then copy into it the example files for this homework assignment, by typing, at the "forbin>" prompt:
cp ~nixon/cs111/hw05/* .
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, and especially to avoid inadvertantly deleting your hidden files (.login, .cshrc, .profile, etc.).
Play with the example programs when reading the tutorials for this assignment. When you are finished reading the tutorial, it is recommended that you then delete the example files, or at least most of them, in order to save disk space.
Answers to most of the practice problems are available.
'e' appeared at these positions: 1 5 6
Note that the position of the first character is to be denoted "1", which is off by one from its position in the actual array.
'e' appeared at 2 other positions besides the end.
The detection of letters should be case-insensitive, e.g. both 'e' and 'E' should count as ths same character.
* ***** * ***** ** **** ** **** *** *** *** *** **** ** **** ** ***** * ***** *
If the entered character is a number sign ('#'), the following is displayed:
# ##### # ##### ## #### ## #### ### ### ### ### #### ## #### ## ##### # ##### #
Although you are not required to compile and run your program, it is strongly recommended that you do so, to see what the output actually looks like.
1 1 1 1 1
e = -- + -- + -- + -- + -- + ...
0! 1! 2! 3! 4!
where the exclamation point (!) denotes the factorial of a number, e.g. 3! is the factorial of 3. (The use of ! to mean factorial is a methematical notation only. It does NOT have such a meaning in C++.)
Your program should display a table of the first 12 approximations (n = 0 through 11), each to 6 decimal places. For accuracy, use type double for the series and its terms.
Note that, for this series, you will NOT need a million terms to get a good approximation. Even the 9th approximation will be very good. Thus, we say that our series for e converges a lot faster than the series for pi discussed in the tutorial.
For full credit, use no more nesting of loops than necessary. You need only ONE for loop. You do NOT need, and should not use, a nested loop to generate the terms in the series, inside a larger loop to sum the terms..
For full credit, use no more nesting of loops than necessary.
Try to develop an algorithm with as little nesting of loops as possible.
Hint: The program must first determine both the length of the longest sequence and, also, the index of the location, within the string, of the first character in the longest sequence. The program can then use these two pieces of information to print out the characters in the relevant part of the string.
The program patternTest.cpp should prompt the user to enter an integer, and then, after the number has been input, it should display the appropriate pattern, if any. If no pattern is displayed, a message should be printed stating that no pattern is available for that number. In the main function, use juat one single call to the function pattern for BOTH the purposes of displaying the pattern and testing whether the pattern was displayed. (The main function should NOT contain a direct test of whether the parameter is in the range 1 to 20. This should be tested only via a call to the pattern function.)
Below, some examples of the pattern will be shown, and then it will be described in words. If, for example, the parameter is equal to 7, then the pattern should be as follows:
* * * * * * * * * * * * * * * * * * * * * * * * *
If the parameter is equal to 8, the pattern should be as follows:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
If the parameter is equal to 1, the pattern should consist of just a single asterisk:
*
If the parameter is equal to 2, the pattern should consist of just two asterisks as follows:
* *
If the parameter is equal to 3, the pattern should be as follows:
* * * * *
If the parameter is equal to 4, the pattern should be as follows:
* * * * * * * *
If the parameter is equal to 5, the pattern should be as follows:
* * * * * * * * * * * * *
If the parameter is equal to 6, the pattern should be as follows:
* * * * * * * * * * * * * * * * * *
and so on, up to the following patterns for parameter values of 19 and 20. For a parameter value of 19:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
and for a parameter value of 20:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The number of rows should be equal to the parameter. Each row should consist of asterisks separated by three spaces. Odd-numbered rows should begin with an asterisk, whereas even-numbered rows should begin with two spaces followed by the first asterisk. In each row, the number of asterisks to be displayed should be as many as possible within the limitation that the total number of characters, including both asterisks and spaces, is no greater than 2n, if n is the number of rows.:
Write the function so that it is as short as possible. (It should contain a nested loop.)
For ease in compiling the program, it is STRONGLY recommended that you write the program in stages, rather than all at once. First, write just a file containing the pattern function and a trivial main function:
int main()
{
return 0;
}
Once you've gotten this to compile, add a single, simple statement inside the main function calling the pattern function. In order for this to compile, your program will need to contain a prototype (declaration) of the pattern function above the main function, in addition to defining the pattern function below the main function.
Once you've gotten this to compile and run, modify the main function so that it does everything it is supposed to do, as described earlier.
cp patternTest.cpp pattern2.cpp
Then modify pattern2.cpp so that it prompts the user repeatedly to enter an integer, displaying the pattern for each integer the user enters, until the user enters a number outside the range 1 to 20, at which time the program does not display a pattern, but, instead, displays a count of the number of patterns that have been displayed. The program should then quit.
Use a single call to the pattern function for BOTH the purposes of (1) displaying the pattern and (2) testing whether the entered number is in the range 1 to 20. Do not test the latter directly within the main function, but only via a call to the pattern function, which should be used as the condition of a while loop.
The final project will simulate an ATM machine. Below is the first part.
Given an input text file containing bank account numbers, bank balances, and names, formatted as follows:
801749724 3901.63 Anwar Abdul 648759757 26876.49 Brown Joel 185029754 596.13 Carducci Giovani 906314873 2150.84 Martinez Maria 857905495 684501.37 Smith Jane Lee 742978169 8495.10 Warren Jerry 108358763 39105.28 Zhu Weng Heng
Write program atm.cpp which takes the name of the input file as a command-line argument and does the following:
Also, if and whenever the program needs to quit for any reason (including an error such as a non-numeric entry for the account number), the program should save the contents of the arrays to bankNew.txt before quitting. (The only exceptions is that if there was an error reading the input file, or if the file bankNew.txt cannot be created, then in those cases the program should not try to save the data to that file, because it can't.)
Your program should work with both of the following input data files:
Please note that this will be the longest program you've written so far. Do NOT try to write the entire program all at once and then complie and run it. If you do that, the program will be a nightmare to debug. Instead, write it piecemeal.
As a first step, write a program which just (1) reads the data from the input file into the three arrays and then (2) saves the contents of the three arrays to bankNew.txt. Make sure this runs properly before you do anything else.
Even this simpler program should be written piecemeal, several lines at a time, compiling after every several lines. So that these intermediat programs will be more likely to compile, pay attention to block structure. Whenever you type an opening curly brace, immediately type the corresponding closing curly brace, then type the lines that go in between.
In the output file, you will need to right-justify a column of floatin-point numbers (representing money amounts) and force them to be printed to two decimal places. Below is information on how to do this:
outputstream << setw(width) << setprecision(2)
<< setiosflags(ios::fixed)
<< variable_name << endl;
where outputstream is either cout or an ofstream object, and variable_name is a floating-point variable.
When you have finished writing a program which just reads the data from a file and then just writes it to bankNew.txt and you have verified that it runs correctly, you should then write the remaining parts.
Submit the following source code file, and only this file:
To recitation/lab on the due date, bring a stapled-together printout of the following source code file, which you wrote:
Back to: