10:58 PM 2/27/2004 CS 111 Assignment 3

CS 111 Assignment 3

  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

    If you have not done so already, please read Skansholm, § 3.1 to 3.3, in addition to reading the tutorals for this assignment.

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

       cp ~nixon/cs111/hw03/* .
    

    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 first delete the example files, in order to save disk space.


  3. Practice problems

    Answers to the practice problems are provided here. However, so that you will be adequately prepared for the next quiz, 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.

    In response to the following problems, write complete programs unless you are told to do otherwise. For some problems, you will also be asked to write code traces.

    It is strongly recommended that you plan your algorithms informally in paper before writing the programs themselves.


    1. Write a program which computes the factorial of a non-negative integer entered by the user. A user-friendly error message should be printed if the number is negative.

      The factorial of a number is equal to 1 if the number is 0. If the number is positive, the factorial is equal to the product of all the integers from 1 up to the number.

      Create a line-numbered copy of this program, so that you can more conveniently write code traces.

      Make code traces for several different cases, including boundary cases. Include both valid cases and at least one error case.


    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 checkNonASCII.cpp, as discussed in the Tutorial on for loops, and more about characters and strings, 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 quiz preparation, it is strongly recommended that you write the program from scratch, rather than by modifying checkNonASCII.cpp. Look at checkNonASCII.cpp for a clue, then write the program without looking at checkNonASCII.cpp further.

      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 "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. Write a program which inputs an integer as a command-line argument and then outputs, to the terminal window, the factorial of the number. You may write this program by modifying the program you wrote in exercise 1 so that it takes its input via a command-line argument rather than via cin. (Note that command-line arguments are covered ONLY in the relevant tutorial and not in the textbook.)

      First, write the program without any error handling. Then, after you test the program for a variety of inputs, add error handling as explained below.

      In your first draft of the program, use variables of type int for both the input number and the factorial. Your program should have the following output for an input of 0:

      The factorial of 0 is 1.
      

      For an input of 1:

      The factorial of 1 is 1.
      

      For an input of 11:

      The factorial of 11 is 39916800.
      

      And for an input of 12:

      The factorial of 12 is 479001600.
      

      But, for an input of 13, your program will have the following incorrect output:

      The factorial of 13 is 1932053504.
      

      You can tell that the output for 13 is incorrect because it does not end in two zeroes, as the.output for 12 does. If a number's base 10 representation ends with two zeroes, then multiplying it by any nonzero integer, such as 13, should yield another number whose base 10 representation ends with at least two zeroes.

      Why the incorrect result? Because the correct result, 6227020800, exceeds the highest possible number in the int range, which is 2147483647.

      You can make the program output a correct result for 13 by declaring as type long, rather than type int, the variable which holds the cumulative product you use to compute the factorial. (However, the input number should still be of type int, for compatibility with the parseForbinInt function which you use to interpret the command-line argument as an int value.)

      Change the data type of the factorial to long. Then determine, experimentally, how much higher the input can go and still yield a correct result, without exceeding the highest possible value in the long range. To determine this, check the program with every possible input starting at 13, increasing the input by 1 each time you run the program, until you encounter an output which is obviously wrong, e.g. an output number which does not end in 00. You will not need to check very many inputs; you will arrive at the limit very soon.

      Once you've determined the maximum input that will work correctly, add error handling. Your program should display appropriate user-friendly messages if (1) there are not enough command-line arguments, (2) the command-line argument represents a negative integer, or (3) the command-line argument represents a number which is too large for the computed factorial to be accurate, as you've determined experimentally above.


  4. Assigned programming problems

    1. (5 points)   Write a program powerSum.cpp which inputs a positive integer n and a floating-point (double) number x as command-line arguments and computes the following sum:

          x     x     x            x
         1  +  2  +  3  +  ...  + n
      

      For example, if the program is run as follows:

         a.out 5 2
      

      it should, in this case, print out the number 55, the sum of the squares of 1, 2, 3, 4, and 5. Or, if the program is run as follows:

         a.out 2 0.5
      

      it should, in this case, print out the sum of the square roots of the numbers 1 and 2, which is approximately 2.41421.

      If the integer n is negative or zero, the program should print an error message and quit. Otherwise, it should compute the above sum and print it out.

      To compute powers, you may use the pow function in the library. Its prototypes are in the <cmath> header file. Use the version of the pow function which has the following prototype:

         double pow(double x, double y);
      

      Given the arguments specified in the above prototype, the pow function returns the value of x raised to the y power.

      Note that an int value will automatically convert to a double with no problem, if passed as an argument to a function which expects a double value.

    2. (5 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 3 example file neatColumns1.cpp, to right-justify the columns. The filename of the input file should be input as a command-line argument. The first number in the input file should be the number of numbers in the sequence. (This first number, itself, should not be considered part of the sequence of numbers to be squared and cubed and listed in the the table.)

      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 numbers2.txt and FileWithAVeryLongName.txt, and test it also for nonexistent files and for incorrectly-formatted input files such as table.cpp itself.


  5. Preparing to hand in your homework

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

       cp ~nixon/cs111/hw03/hw03.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_3.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 files, and only these files:

    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 bundle of printouts of the following source code files, which you wrote:

    1. powerSum.cpp
    2. table.cpp

    Your printouts must be stapled together in exactly the order listed above.


Back to: