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

Assignment 4 is due Monday, March 27.


  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 venus, create a directory named hw04 inside your homework directory. 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, to avoid inadvertantly deleting your hidden files (.login, .cshrc, .profile, etc.).

    Play with the example programs when reading the tutorials. Then, when you are ready to begin work on the homework assignment itself, it is recommended that you delete the example files, or at least most of them, in order to save disk space.

    In both the practice problems and the assigned problems, pay careful attention to readability issues, such as indentation. You may lose up to 15 points (out of 100) for bad indentation, depending on how bad it is.

    In the assigned problems, ANSWER NO MORE THAN ONE PROBLEM PER SHEET OF PAPER. At the top of each sheet, put (1) your for name (as known to the Registrar's office), (2) the last 4 digits of your student ID number, (3) "CS 111 Spring 2002 Assignment 4," and (4) your lab section.


  3. Practice problems

    Answers to most of the practice problems are here. However, so that you will be adequately prepared for the exam, it is strongly recommended that you work on the problems yourself before looking at the answers. It is also recommended that you work on at least some of these practice problems before doing the assigned homework.

    Most of the problems below involve strings. Use C-strings, not string class objects, unless you are specifically told to use string class objects.


    1. Processing a sequence of numbers:   counting odd and even integers. Write a program which prompts the user to enter a sequence of integers and then an X to.signal end of input. The program should then output a statement saying how many of the numbers were odd and how many of the numbers were even.


    2. Write a program which prompts the user to enter a string and outputs the number of vowels in the string. The vowels are the letters A, E, I, O, and U. Your program should count, as vowels, both uppercase and lowercase occurrences of these letters.

      Use as few if statements as possible. To accomplish this, use the appropriate one of these boolean operators:

      operator meaning
      &&
      and
      ||
      or
      !
      not


    3. Write a program which prompts the user to enter a string and checks whether the string represents a natural number. (Note: In the computer science world, the set of "natural numbers" is usually defined as including 0. Thus, your program should check whether the string represents a non-negative integer.

      A string is considered to represent a natural number if it contains at least one character (i.e. it is not an empty string) and all of its characters are digit characters. Note that checking whether all the characters are digit characters can be accomplished by checking whether the string contains any non-digit characters. Thus, you may use an "innocent until proven guilty" technique similar to the one used in checkNonASCII2A.cpp, checkNonASCII3A.cpp, or checkNonASCII4A.cpp, as discussed in the Tutorial on kinds of repetition, text files, input error states, and break statements

      , to check whether a string contains any non-ASCII characters. To ensure that the string is nonempty, use cin's extraction operator for input.

      Note: To gain maximum benefit in terms of exam preparation, it is strongly recommended that you write the program from scratch, rather than by modifying one of the example files. Look at the example files for clues, then write the program without looking at the example files further.

      Then create a line-numbered copy of this program, so that you can write code traces. Write code traces for the following inputs:

         12390
         0
         9
         /
         :
         a2
         2a
      


    4. Write a program which prompts the user to enter a string and prints a message indicating whether the string is a palindrome. A palindrome is a sequence of characters which is unchanged when read backwards. For example, the words "deed," "madam," and "tenet" are palindromes.


    5. Write a program which prompts the user to enter a string and then outputs a version of the string in which all the letters are capitalized, i.e. the lowercase letters are replaced by uppercase letters and all other characters are unchanged.

      Hint: Recall that, in the ASCII chart, the uppercase letters are all grouped togegher in one part of the chart in alphabtical order, and the lowercase letters are grouped together in another part of the chart, also in alphabetical order. Thus, the difference in ASCII values between 'a' and 'A' is the same as the difference in ASCII values between 'b' and 'B', and is the same as the difference in ASCII values between 'c' and 'C', and so on. Thus, a lowercase letter can be capitalized by adding (or subtracting) this constant difference. (Note: In the interests of program readability, it is better to compute this difference by an expression like 'A' - 'a' (or 'a' - 'A') rather than to compute the difference in your head and use a numeric literal in your program. If you use the number directly, its significance will not be obvious to the reader without a comment.)


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

      In this program, you'll need to right-justify a floating-point number and force it to be displayed to two decimal places, See the updated Tutorial on kinds of repetition, text files, input error states, and break statements for information on how to do this.


  4. Assigned programming problems

    1. (40 points)   Write a program emptyLineCount.cpp which prompts the user to enter the name of a text file and then outputs, to the console, the number of empty lines that the file contains. An empty line is a line that is totally blank, containing no characters at all, even whitespace characters, except for the end-of-line marker which terminates the line.

    2. (40 points)   Write a program table.cpp which inputs a sequence of integers from a text file and prints, to another text file table.txt, a table listing the integers themselves and their squares and cubes, Use the setw manipulator, as illustrated in the Assignment 4 example file neatColumns2.cpp, to right-justify the columns. The filename of the input file should be input by the user when prompted. The program should process all the numbers in a text file, including the first number, 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.

      The error state of your ifstream object should be checked after opening the input file and after every input from the file. The error state of your ofstream object should be chedked after opening the file and at the end of the program, after all output is finished.

      Test your program for correctly-formatted input files such as moreNumbers.txt and test it also for nonexistent files and for incorrectly-formatted input files such as table.cpp itself.


    3. (20 points)   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.


  5. Preparing to hand in your homework

    The usual. Printouts should be stapled together in the following order:

    • emptyLineCount.cpp
    • table.cpp


Back to: