CS 111 Assignment 7
Answers to practice problems will be here soon.
- Files to print out and bring to lecture and recitation
- Preparations before you begin your homework
- Practice problems
- Assigned programming problems
- Preparing to hand in your homework
For information about the due dates, including late dates and extra-credit early due dates, please see Homework policies and due dates.
- Files to print out and bring to lecture and recitation
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.
- Examples to accompany the tutorial on One-dimensional arrays (including C-strings) as parameters to functions
- Examples to accompany the Tutorial on two-dimensional arrays
- arrayOfPointersDemo1.cpp
- arrayOfPointersDemo2.cpp
- matricesToAdd.txt
- matricesToMultiply.txt
- matrixAddition.cpp
- matrixAddition2.cpp
- oneBigArrayDemo1.cpp
- oneBigArrayDemo2.cpp
- quizScores.txt
- quizScores1.cpp
- quizScores2.cpp
- quizScores3.cpp
- ticTacToe.cpp
- textUtility2.cpp
- textUtility2.h
- square3.txt
- square5.txt
- square8.txt
- square10.txt
- Preparations before you begin your homework
On forbin, create a directory named hw07 inside your homework directory. Change your present working directory to hw07 and then copy into it the example files for this homework assignment, by typing, at the "forbin>" prompt:
cp ~nixon/cs111/hw07/* .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.).
- Practice problems
- In textUtility2.cpp, there is a stub for a C-string version of the isNaturalNumber function, of which you wrote a string class object version in Assignment 7. Replace the stub with a function body which actually does the specified job.
Before you begin, make sure you have the latest versions of textUtility2.cpp and textUtility2.h, which contain the newly-added isInteger function.
Also before you begin, read the Tutorial on one-dimensional arrays, especially the last section, on "C-strings as parameters to functions." It will contain some important clues.
When you are finished writing the function, test it via the driver program testIsNaturalNumberC.cpp, which you can compile by typing:
g++ testIsNaturalNumberC.cpp textUtility2.cppOnce you have gotten the isNaturalNumber function to work correctly, the isInteger function -- which calls isNaturalNumber -- should also work correctly. Test the isInteger function via testIsIntegerC.cpp.
- In textUtility2.cpp, write the function isForbinInt with the following comment and heading:
/* * bool isForbinInt(const char text[]) * * Tests whether the specified string represents * a value of type int on forbin. A string represents * such a value if, and only if, it consists of a * string representing an integer and that integer is * within the int range on forbin, i.e. within the * range -2147483648 to 2147483647, inclusive. * * Parameter: * text - the string to be tested. * * Returns: * true if text represents an int on forbin. * false otherwise. */ bool isForbinInt(const char text[])and put a suitable declaration (prototype) in header file textUtility2.h. In textUtility2.cpp, define the function so that it behaves as advertised in the comment.
It is recommended that begin by calling the isInteger function to determine whether the string represents an integer. If the string does not represent an integer, then it obviously does not represent an integer within the int range on forbin, either. So, if your call to isInteger returns false, then isForbinInt can go ahead and return false too. Only if the string represents an integer do you need to concern yourself with whether the represented integer is within the appropriate range.
Then, as in Assignment 7, you will need to (1) obtain a substring which does not contain the leading minus sign, if any, and which also does not contain any leading zeroes, (2) test the length of that substring, and (3) perform lexicographical comparison of the substring to C-string literals representing the extreme values of the range.
To obtain the substring, use pointer arithmetic, as discussed in the Tutorial on one-dimensional arrays. See especially the last two sections, "Still more about arrays as parameters" and "C-strings as parameters to functions."
To do the lexicographical comparison of strings, do NOT use relational operators (<, >, <=, >=, ==, and !=), which perform a lexicographical comparison only if at least one of the strings is a C++ string class object. To compare two C-strings, call the compare function defined in textUtility2.cpp.
When finished writing your isForbinInt function, test it via the driver program testIsForbinIntC.cpp.
- In textUtility2.cpp, write the function parseForbinInt with the following comment and heading:
/* * bool parseForbinInt(const char text[], * int& number) * * Converts a string to an int value, if the * string represents an integer within forbin's * int range, i.e. within the range * -2147483648 to 2147483647, inclusive. * * Parameters: * text - the string to be converted * number - the intended int value. * Precondition: none * Postcondition: number is the intended * integer value represented by the string, * if the string represents a valid forbin * int value. Otherwise, number is * unchanged. * * Returns: * true if a non-negative integer was successfully * read from text, false otherwise. */ bool parseForbinInt(const char text[], int& number)and put a suitable declaration (prototype) in header file textUtility2.h. In textUtility2.cpp, define the function so that it behaves as advertised in the comment.
As in Assignment 7, it is recommended that you use calls to the functions isForbinInt and parseNaturalNumber.
When finished writing your parseForbinInt function, test it via the driver program testParseForbinIntC.cpp.
- Matrix multiplication. Read the Tutorial on two-dimensional arrays, especially the sections on "Matrices" and "Two-dimensional arrays (the one-big-array kind) as parameters to functions." Then write programs matrixMultiplication.cpp and matrixMultiplication2.cpp, which both perform matrix multiplication, differing from each other only in that matrixMultiplication.cpp should use two-dimensional arrays of float directly, whereas matrixMultiplication2.cpp should use typedef statements to define special data types for three different sizes two-dimensional of arrays of float, and then, throughout the program, should use those data types instead of declaring two-dimensional arrays of float directly.
Both programs should read the two factor matrices from file matricesToMultiply.txt and output the product matrix to file matrixProduct.txt.
The sizes of the matrices should be hard-coded into your program, based on your own reading of the sizes of the matrices in matricesToMultiply.txt. With two-dimensional arrays of the one-big-array kind, it is not, alas, possible to write a program which DECIDES the size of the matrices based on what the program itself discovers in the data file.
Both programs should define a main function and three other functions:
- A readMatrices function which reads two matrices from a file whose filename is specified as a const C-string parameter. The matrices are read into two parameter arrays, of appropriate sizes. This function will be similar to the readMatrices in matrixAddition.cpp and matrixAddition2.cpp, except that the arrays will be of two different sizes.
- A multiply function which multiplies two matrices. The factor matrices are represented by const two-dimensional arrays whose sizes match the contents of matricesToMultiply.txt. The product matrix is put into another (NON-const) 2-dimensional array parameter. (Be careful about the array sizes.)
- A printMatrix function which prints the contents of a const two-dimensional array into a file whose filename is specified as a const C-string parameter. Like the similar function in matrixAddition.cpp, it may call one of the printRightJustified functions which are declared in textUtility2.h and defined in textUtility2.cpp.
It is recommended that you write one version of the program one function at a time, compiling after each function. You'll also need a skeletal main function, right from the start, in order to compile anything, but you should write the body of the main function last.
Having written one version of the program, you then can easily convert it to the other version. Just remember to change ALL your array declarations. (The program will work correctly even if you don't change them all, but you are, nevertheless, asked to change them all.)
- Partially-filled two-dimensional arrays. Given the following data files, each containing a square pattern of numbers, plus an extra number at the top indicating both the number of rows and the number of columns in the square:
Write a program which contains the following:
- A global typedef for a square two dimensional array with MAX_SIZE rows and MAX_SIZE columns, where MAX_SIZE is a previously-declared global constant whose value should be 10, at least for now. The elements of the array should be of type int.
- A function which inputs the contents of the specified text file into a two-dimensional array of your typedefed type. (The first number read from the file does not get stored in the array, but indicates the actual number of rows and columns to be used. The remaining numbers should be stored in a square pattern with the given number of rows and columns, in most cases not occupying the entire two-dimensional array.) The function's parameters should be: (1) the two-dimensional array, declared using your typedefed type, (2) the input filename (passed as a string), and (3) a reference parameter for the actual number of rows and columns used within the array (as distinct from the number of rows and columns of the array itself). The function should return a boolean value indicating whether input was successful. (It should return false if (1) the specified number of rows and columns is either less than 1 or greater than MAX_SIZE, (2) the specified input file does not exist or is not readable, or (3) a non-numeric input -- or end-of-file -- was encountered before all the expected numbers have been read.)
- A function which outputs the contents of the specified two-dimensional array to the screen, with the numbers in neat columns. The function's parameters should be (1) the square two-dimensional array, declared using your typedefed type, and (2) the number of rows and columns used within the array (declared as a value parameter this time).
- A main function which takes a text file as a command-line argument, and then, by calling the two other functions, it should first read the text file into a two-dimensional array and then output the contents of the array to the screen.
- Magic squares. To the above program, add a function which tests whether a partially-filled square array (of your typedefed type) represents a magic square. A square matrix is a magic square if, and only if, the sums of the numbers in each of its rows, columns, and diagonals are equal. The function should take, as parameters, both the array itself and the number of rows and columns used within the array. The function should return a boolean value indicating whether the array represents a magic square.
- Assigned programming problems
- (10 points) You will now be asked to write a complete ATM simulator program atm2.cpp, which should behave as follows:
First, the user should be prompted to enter the name of the input text file. Then, 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 HengWrite a program which then does the following:
- Reads, into memory, the contents of the input text file. (Use three parallel arrays to hold the data in memory. Use an array of int to hold the account numbers, an array of double to hold the bank balances, and an array of strings (C++ string class objects) to hold the names. Use a while loop to read from the file into the arrays.)
- Prompts the user to enter an account number. If the account number is invalid, an error message is printed and the user is prompted again for the account number. This occurs repeatedly, if necessary, until the user enters a valid account number. (Of course, if a valid account number is entered the first time, the user is not prompted for it again. Also, if the user enters a non-numeric character, the program should quit rather than prompt again for input.) Hint: To determine whether the entered number is valid, your program should search the array of account numbers.
- Once the user enters a valid account number, the account balance is displayed.
- The user is then prompted to do one of the following: (1) make a deposit, (2) make a withdrawal, or (3) quit. The program should display a menu of choices, prompting the user to enter 'D' (for deposit), 'W' (for withdrawal), or 'Q' (for quit).
- The user's selection should be processed in a case-insensitive manner, e.g. either 'D' or 'd' should count as selecting the "deposit" option. If the user selects "quit" ('Q' or 'q'), the program quits. Otherwise, the user is prompted for an amount of money to deposit or an amount of money to withdraw, depending on the user's selection. (IF the user chooses to withdraw money, the program should ensure that the user does not overdraw, prompting the user repeatedly if necessary.) The program then displays the new balance which results from either adding or subtracting the amount entered by the user.
- After a deposit or withdrawal, the program again prompts the user for an account number, and then displays the menu again (deposit, withdraw, quit) once a valid account is found. This occurs repeatedly until the user selects 'Q' or 'q' at the menu prompt.
- Before quitting, the program stores the entire customer database, including the user's updated balance, to an output file bankNew.txt. (Use an ofstream object as explained at the end of Assignment 3's Tutorial on text files, command-line arguments, and I/O stream error states.) The file bankNew.txt should exactly resemble the original file except that one account has been updated.
If and whenever the program needs to quit for any reason (including the user entering 'Q' at the menu prompt, or 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:
Your program should define the following functions, in addition to the main function:
- All the functions that you developed in your Assignment 5 program (except for that program's main function.
- A boolean function which reads the contents of an input text file into memory and returns true if successful, false in the event of input error. The function should take parameters as follows: (1) the name of the input file (as a string class object), (2) the array of account numbers, (3) the array of account balances, (4) the array of account names, and (5) a reference parameter for the number of elements that the function has actually input into each of the three arrays.
- A void function which outputs the contents of the three arrays into an output text file. This function should take parameters as follows: (1) the name of the input file (as a string class object), (2) the array of account numbers, (3) the array of account balances, (4) the array of account names, and (5) the length of the meaningfully defined portion of each of the three arrays.
- A function which asks the user for an account number and, once a valid account number is obtained from the user, returns the index of that account number in the array of account numbers. This function should take as parameters (1) the array of account numbers and (2) the length of the meaningfully defined portion of the array.
This function should prompt the user to enter an account number. If the account number is invalid, an error message is printed and the user is prompted again for the account number. This occurs repeatedly, if necessary, until the user enters a valid account number. (Of course, if a valid account number is entered the first time, the user is not prompted for it again. Also, if the user enters a non-numeric character, the program should print an error message and then quit rather than prompt again for input.) Hint: To determine whether the entered number is valid, your program should search the array of account numbers.
To make the program quit (rather than just make the function quit), use the following line of code:
exit(1);To call the exit function, you will need to include the standard library header file <cstdlib> as follows:
#include <cstdlib>Note that this function will need to use cin and cout as global variables. It should be commented accordingly.
All functions should be accompanied by comments as specified in the Assignment 6 tutorial on Tutorial on programmer-defined functions. All primitive data type parameters should be passed by value when it is not necessary to pass them by reference. On the other hand, all object parameters (e.g. string class objects such as the filenames) should be passed by reference, not by value. In cases where an object or array is not changed by a function, the object or array parameter should be declared const.
Do not use any global variables except for cin and cout (which you do not need to declare, because they are declared for you in header file <iostream>). But do feel free to use any global constants (declared using the const keyword) that you may deem appropriate, if any. It is recommended that the size of the three arrays be declared as a global constant.
- Preparing to hand in your homework
Run the script file hw07.sh to make sure your programs work correctly and that they have the correct filenames. The script file hw07.sh can be copied into your present working directory as follows:
cp ~nixon/cs111/hw07/hw07.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_6.tarreplacing lastname with your actual last name (spelled as the Registrar's Office spells it, as on your tuition bill or bursar's recepet) in lower-case letters only (not capital letters), and replacing accountname with your actual forbin account name. The "6" indicates Assignment 7.
The tar file must contain the following source code file, and only this file:
- atm2.cpp
The tar file must be submitted to your instructor at the appropriate E-mail address, as listed on the instructor E-mail address page.
Be sure to hand in a printout, as usual.
Back to: