CS 111 Assignment 8

REVISED 1:10 PM 5/18/2004. Removed an outdated reference to a "remove" option. The assigned programming problem is NOT required to have a "remove account" capability.

Answers to the practice problems are here.

  1. Files to print out and bring to lecture and recitation
  2. Preparations before you begin your homework
  3. Practice problems
  4. Assigned programming problems
  5. 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.


  1. 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.


  2. Preparations before you begin your homework

    On forbin, create a directory named hw08 inside your homework directory. Change your present working directory to hw08 and then copy into it the example files for this homework assignment, by typing, at the "forbin>" prompt:

       cp ~nixon/cs111/hw08/* .
    

    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.).


  3. Practice problems


    1. 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.)


    2. 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:

      1. 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.

      2. 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.)

      3. 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).

      4. 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.


    3. 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.


    4. Output problems involving recursion. Compare the implementations of all the functions of the last two of the following listed filesL

      Test the functions in both functionDemoRecursive.cpp and functionDemoRecursiveTrace.cpp using each of the following test programs:

      Compile them as follows, for example:

      g++ -s functionDemoRecursive.cpp testFactorial.cpp
      g++ -s functionDemoRecursiveTrace.cpp testFactorial.cpp
      g++ -s functionDemoRecursiveTrace.cpp testPowerOfTwo.cpp
      

      And run them using small numbers (no greater than 5).

      After you've gotten a feel for what both the programs and their outputs do, try to predict their outputs. Write the predicted output on a piece of paper before you run the program, and then run the program to see whether your prediction was correct.


  4. Assigned programming problem

    Write a program atm4.cpp, which will be an enhanced version of your assignment 7 program (atm3.cpp) with additional features as follows:

    1. After completing a transaction (as in Assignment 7), the program should NOT quit. Instead, it should ask the user: "Do you want to enter another account number? (Enter Y or N)> ". If the user answers N (No), the program should save the current data (i.e. the contents of the array of account structs) to the output file and quit. If the user answers Y (Yes), the program should prompt for an account number.

    2. If the account number entered does not exist in the array of structs, the program should ask if you want to add this as a new account.

      If the user's answer is N (No), then the program should go back to step 1 above (i.e. the program should ask whether to process another account number, and so on).

      If the user's answer is Y (Yes), the program should create the new account by inputting data into the next available struct in the array,

      After calling the above function, the program should go back to step 1.

      Note: In your program, the declared maximum array size should be much bigger than the program is likely to need, to allow for repeated additions of new accounts. You may need to increase it from what it was previous versions.

    3. If a valid account number was entered in step 1, the program should ask the user to enter D for Deposit, W for Withdrawal, Q to quit. In other words, the same menu as in Assignment 7.

      For W or D, do as in previous assignments, then repeat step 3 (i.e. display the D/W/Q menu again) for another transaction to this account.

      For Q, the program should go back to step 1 above. Note, in this version, Q should no longer result in the program quitting entirely. Instead, it quits action for THIS account only, (The user is then asked whether to process another account, etc.)

    Use switch statements where appropriate, instead of nested if/else. Use whatever kind of loop (for, while, or do/while) is most appropriate for each situation.


  5. Preparing to hand in your homework

    As usual. The script file will have the name hw08.sh


Back to: