The second tutorial is now complete, as of 12:07 AM Sun 3/14/2004. The promised two more example programs have been added.

CS 111 Assignment 4

  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

Warning: This assignment must be handed in to your lab instructor on or before its on-time due date and will NOT be accepted late, at all, because it will be gone over in the following recitation. It is a paper-only assignment, not requiring you to compile or run your programs (though you are encouraged to compile and run them if you have enough time). For information about the due dates and the different kinds of homework assignments, please see Homework policies and due dates.


  1. Files to print out and bring to lecture and recitation

    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.


  2. Preparations before you begin your homework

    So that you can play with the example programs, create a directory named hw04 inside your homework directory on forbin. Change your present working directory to hw04 and then copy into it the example files for this homework assignment, by typing, at the "forbin>" prompt:

       cp ~nixon/cs111/hw04/* .
    

    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.


  3. Practice problems

    Here are Answers to most of the practice problems.


    1. Text file input. In Assignment 3, the second of the assigned problems (not practice problems) required you to take input from a text file. The first number in the input text file indicated the number of numbers in the rest of the file.

      Write another version of this same program which can process all the numbers in a text file without needing the first number to indicate the number of numbers in the rest of the file. Use a while loop. Create several input text files to test it.

      Write two versions of this program, one of which takes the name of the input text file as a command-line argument, and the other of which prompts the user to enter the input filename.


    2. Code trace. After having written the above program, write a code trace of the version which prompts the user for an input text file. To keep your code trace reasonably short, use an input text file with just two numbers in it.


    3. Compound interest. Write a program which prompts the user for (1) an initial savings account balance, (2) the year in which the account began, and (3) an annual percentage interest rate. The program then displays a table showing the account balance in consecutive years, beginning with the initial balance. The table should have two columns, the left column displaying the year number, and the right column displaying the account balance for that year, assuming no deposits or withdrawals, Assume that the interest is compounded annually. The last line of the table should represent the first year in which the account balance is at least two times the initial balance.

      Later, some information will be posted here on how to right-justify a floating-point number and force it to be displayed to two decimal places, You are urged to begin work on other aspects of this problem earlier and return to it later to fix the appearance of the output.


    4. String processing with C-strings. Write a program which prompts the user to enter a word, and then does the following:

      1. Prints the position, in the word, of any characters identical to the last character, including the last character itself. For example, if the user entered the word "entree", the output should include the following:

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

      2. Prints the total number of characters identical to the last character, not including the last character itself. For example, if the user entered the word "entree", the output should include the following:

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


    5. More string processing with C-strings. Write a program which prompts the user to enter a word, and then outputs a count of the number of pairs of identical adjacent characters. (For example, the word "Halloween" has two paris of identical adjacent characters, "ll" and "ee".)


  4. Assigned programming problems


    • (10 points) 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 does the following:

      1. Reads, into memory, the contents of an input text file whose name has been specified as a command-line argument. (It is suggested that you 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.)

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

      3. Once the user enters a valid account number, the account balance is displayed.

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

      5. 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. The program then displays the new balance which results from either adding or subtracting the amount entered by the user.

      6. Then, 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.

        Also, if and whenever the program needs to quit for any reason (including the user entering 'Q' at the menu prompt, or the user finishing a 'D' or 'W' transaction, 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:

      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.

      For an example, see the Answer to practice problem 3.

      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 try to figure out a reasonable way to break down the remainder of the assignment into parts, so that you can write, compile, and run the program one new part at a time.


  5. Preparing to hand in your homework

    Note: The script file is not available yet. It will be available later.

    Run the script file hw04.sh to make sure your programs work correctly and that they have the correct filenames. The script file hw04.sh can be copied into your present working directory as follows:

       cp ~nixon/cs111/hw04/hw04.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_4.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 file, and only this file:

    • atm.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.

    To recitation/lab on the due date, bring a stapled-together printout of the following source code file, which you wrote:

    • atm.cpp


Back to: