CS 111 Assignment 5

Answers to some of the practice problems are now available.

  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


  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 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 Tutorial 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


    1. Write a program which inputs a character and then displays the character in the four-triangle pattern shown in the sample outputs below. If the entered character is an asterisk ('*'), the following is displayed:

      *       *****      *   *****
      **      ****      **    ****
      ***     ***      ***     ***
      ****    **      ****      **
      *****   *      *****       *
      

      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.


    2. Series approximation of e.   Write a program eSeries.cpp which calculates approximations to e, the base of the natural logarithms, using the following formula:
             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..


    3. Write a program which prompts the user to enter a string and then outputs a message indicating whether the string represents a valid E-mail address. A string is considered to represent a valid E-mail address if it conforms to the following rules:

      • It contains exactly one (at least one, and no more than one) "at" sign ('@'), which is neither the first character nor the last character in the string.

      • It contains at least one dot ('.') in the portion of the string to the right of the "at" sign.

      • All other characters in the string must be letters, digits, underscores ('_'), hyphens ('-'), or dots.

      • No dot may appear next to another dot, or next to the "at" sign, or as the last character in the string.

      For full credit, use no more nesting of loops than necessary.


    4. Write code traces of echoWhile.cpp and echoFor.cpp, as discussed in the last section of the tutorial. Follow the instructions in the tutorial.


    5. Write a program which prompts the user to enter a string and returns the length of the longest sequence of identical consecutive characters within the string. For example, in the string "aaaAAAAAjjB", the longest sequence of identical consecutive characters is "AAAAA".

      Try to develop an algorithm with as little nesting of loops as possible.


    6. Write a program which prompts the user to enter a string and then prints out the longest sequence of identical consecutive characters within the entered string, without printing out any other part of the string. If there is a tie between two longest sequences, the program prints out just one of them.

      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.

    7. Write a program patternTest.cpp containing a function pattern which takes a parameter of type int and return a value of type bool. If the parameter is in the range 1 to 20, inclusive, the function should return true and output, to the terminal window, the exact pattern described in the paragraphs below. If the parameter is outside the range 1 to 20, the function should return false and not output anything at all.

      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.

    8. Make a duplicate copy of patternTest.cpp with the name pattern2.cpp by typing, at the Unix prompt:

         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.


  4. Assigned programming problems

    Write a program transaction.cpp containing the following functions and a main function which tests them:

    • A boolean function which simulates making a deposit into an account with a specified balance. This function takes a reference parameter of type double representing the account balance. The user should be for an amount of money to deposit. In the event of non-numeric input, an error message is printed and the function returns false. If the input is a valid floating-point number (type double), the function then displays the new balance which results from adding the amount entered by the user. The new balance is then stored in the specified location in the array also.

      Note that this function will need to use cin and cout as global variables. It should be commented accordingly.

    • A boolean function which makes a withdrawal from a specified account. This function, too, takes a reference parameter of type double representing the account balance It behaves similarly to the function described above, except that the amount entered by the user is subtracted from rather than added to the old balance to obtain the new balance. Also, this function should be defined so as not to allow the user to overdraw. If the amount that the user wants to withdraw is larger than the account balance, an error message should be printed, and the user should be prompted repeatedly for an appropriate amount, until an appropriate amount is entered. The return value should be the error state of cin.

    • A parameterless void function which outputs the following menu and prompt:

      What do you want to do?
      
         D - make a deposit
         W - make a withdrawal
         Q - quit
      
      Enter your selection (D, W, Q):>
      

    Your main function should prompt the user for (1) an account balance and then (2) a character to indicate the transaction. (To output the latter prompt, use your function which outputs the menu and prompt.) The program should then simulate a deposit if the user selected 'D' (or 'd') and simulate a withdrawal if the user selected 'W' (or 'w'). In either case, the modified account balance should be eisplayed. If the user entered any other character besides 'D', 'd', 'W', or 'w', the program should display the account balance and quit.

    Write the program one function at a time. First, just write a trigial, do-nothing main function and make sure that that compiles. Then write your menu/prompt function plus a call to it in the main function, mand make sure that that compiles and runs correctly. Then write the deposit function and a call to it from the main function (and the necessary lines in the main function that are needed to input the account balance), and run the program to test the deposit function. Then likewise for the withdraw function. Then complete the main function so that it does everything it is supposed to do.


  5. Preparing to hand in your homework

    The usual.


Back to: