CS 111 Assignment 2

  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

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

       cp ~nixon/cs111/hw02/* .
    

    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 5 points (out of 10) for bad indentation, depending on how bad it is.

    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 2," and (4) your lab section. If you don't know your lab section, see the schedule on the Instructors' E-mail addresses page.


  3. Practice problems

    Answers to most of 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.


    1. Debugging exercises:  Find and fix the errors in the following programs Test them to make sure they run properly.


    2. Write a program which promts the user to enter a total number of pennies and then outputs a money amount rounded to the nearest dollar. For example, 150 pennies would round up to $2, whereas 149 pennies would round down to $1. Use integers only; do not use floating-point numbers.

      For this program, you should also submit handwritten code traces for some well-chosen boundary cases. (Code traces and boundary cases are discussed in the Tutorial on if, if/else, program design, testing, debugging, and garbage values.) Your program printout, therefore, should be line-numbered. If you are typing up your program on forbin, see the instructions in the tutorial on How to generate line-numbered copies of files.


    3. Write a program which prompts the user to enter four numbers and then outputs the largest of the four numbers. Use only the techniques we have covered so far (if and if/else but not loops).


    4. A quadratic equation, such as:

             2
          a x  +  b x  +  c  =  0
      

      has two solutions (roots) given by the following formula:

                         
                   +    /-------------
                -b -   /  discriminant
                     \/
          x  =  ----------------------
                        2 a
      
                                   2
          where  discriminant  =  b  -  4 a c
      

      (Note: the above is algebra, NOT C++ code!)

      Write a C++ program which prompts the user to enter the three coefficients a, b, and c, and then outputs the roots, as follows shown below. (In the following sample outputs, the output itself is in plain type, whereas the inputs typed by the user are in boldface type.)

      • If the discriminant is positive, the program outputs the two real roots. For example, if a is 1, b is 0, and c is -1, the output should be:

        This program finds roots of a quadratic equation.
        Enter the coefficients a, b, and c, separated by spaces:>1 0 -1
        Two real roots: 1 and -1.
        

      • If the discriminant is zero, the program outputs the two identical real roots as a single number. For example, if a is 1, b is 2, and c is 1, the output should be:

        This program finds roots of a quadratic equation.
        Enter the coefficients a, b, and c, separated by spaces:>1 2 1
        Two identical real roots, both -1.
        

      • If the discriminant is negative, the program outputs the real and imaginary parts of the two complex roots. For example, if a is 1, b is 0, and c is 1, the output should be:

        This program finds roots of a quadratic equation.
        Enter the coefficients a, b, and c, separated by spaces:>1 0 1
        Two complex roots, where the real part is 0
        and the imaginary part is plus or minus 1.
        

      Assume that the coefficients entered by the user will be real numbers (no imaginary part). Use type double for all quantities, and use the sqrt and abs functions declared in <cmath>. Use sqrt to find the square root of the discriminant and use abs to find the absolute value of the imaginary part of a complex root, to avoid outputs like:

      and the imaginary part is plus or minus -2.
      


    5. The following program segment has very poor indentation:

         if ( number1 < 0 )
         if ( number2 < 0 )
         cout << "+++++" << endl;
         else
         cout << "@@@@@" << endl;
         cout << "#####" << endl;
         cout << "$$$$$" << endl;
      

      Re-write it so that it has an acceptable form of indentation with the else indented by the same amount as the if to which it corresponds. Make no other changes to the program segment. Then, underneath your re-written program segment, write its output for each of the following four cases:

      • a)   number1 = -1   and   number2 = -1
      • b)   number1 = -1   and   number2 = 0
      • c)   number1 = 0   and   number2 = -1
      • d)   number1 = 0   and   number2 = 0

      If you do not actually put the program segment in a program and compile and run it to see what its output actually looks like, write its output as you think it should appear. Write the output of the program segment only, not any larger program that might contain it.


    6. Modify the program segment in the previous problem so that it has the outputs given below, in the indicated cases. Make NO changes to the program segment other than (1) adding curly braces and (2) fixing the indentation to correspond with how the program is now organized.

      • a)   number1 = -1   and   number2 = -1

        +++++
        $$$$$
        

      • b)   number1 = -1   and   number2 = 0

        $$$$$
        

      • c)   number1 = 0   and   number2 = -1

        @@@@@
        #####
        $$$$$
        

      • d)   number1 = 0   and   number2 = 0

        @@@@@
        #####
        $$$$$
        


  4. Assigned programming problems

    1. (3 points)   Write a program practiceEscape.cpp which displays EXACTLY the following output:

      Below is a typical Unix pathname: 
      
         /usr/users/myaccount/somedirectory/mytext.txt
      
      Below is a typical DOS pathname: 
      
         C:\Windows\Desktop\somedir\mytext.txt
      
      Both are pathnames of files named "mytext.txt".
      

      You will need to use some escape sequences, as discussed in the Tutorial on characters, strings, and debugging.

      Your program should not do any input.

    2. (7 points)   Write a program order3strings.cpp which prompts the user to enter three words and then prints out (1) a listing of the three words in order by length (shortest word first, longest word third) and (2) a listing of the words in lexicographical order. (For an explanation of "lexicographical order," read the Tutorial on characters, strings, and debugging.) The words may have been entered by the user in any order, and must be rearanged by the program into the above two specified orders.

      In order for the script file hw02.sh to be able to run this program properly, the three words must be entered on seperate lines, in response to separate prompts, NOT all on one line in response to a single prompt; and the three prompts must each end with a '>' character.


  5. Preparing to hand in your homework

    Run the script file hw02.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/hw02/hw02.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_2.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 "2" indicates Assignment 2.

    The tar file must contain the following source code files, and only these files:

    1. practiceEscape.cpp
    2. order3strings.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 bundle of printouts of the above-listed source code files. Your printouts must be stapled together in exactly the order listed above.


Back to: