Due Monday, April 10, 2006

CS 111 Assignment 5

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

    Answers to most of the practice problems are available.


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


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


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


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


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


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


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


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

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

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


  5. Preparing to hand in your homework

    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: