Algorithms

Assignments

The accompanying table indicates assignments and due dates for Computer Science 301 and are subject to the following notes.


Due Date Chapter Collaboration? Problems
Wed., Sept. 2 Sup. Prob.
No
1, 2
Fri., Sept. 4 Sup. Prob.
No
3, 4
Mon., Sept. 7 Sup. Prob.
No
5
Fri., Sept. 11Chapter 1
Yes
2, 3, 7, 9
Wed., Sept. 30Chapter 2
Yes
5bc, 7
Chapter 4
Yes
15
Fri., Oct. 2 Sup. Prob.
No
6
Wed., Oct. 28 Sup. Prob.
No
7
Mon., Nov. 23 Chapter 8
No
4, 5, 6, 7, 11, 12, 13, 14, 17, 29
Wed., Dec. 2 Take-Home Test
No
Part A: written problems
Fri., Dec. 4 Take-Home Test
No
Part B: C++ program


Submitting Programs For This Course: In turning in any programs for the course, please follow these directions:

  1. The first three lines of any C++ program should be comments containing your name, your mailbox number, and an identification of assignment being solved. For example:
    
        // Henry M. Walker
        // Box Y-06
        // Assignment for Wednesday, September 2
    
    Also, a comment is needed for every definition of a C++ function, method, or class, stating in English what that program unit is supposed to do.

  2. Obtain a listing of your program and a record of relevant test runs using the submit command:
    • Within a dtterm window (before running C++), begin recording session with the statement

      submit filename

      where filename is the name of the file in which you want the session stored.
    • Within the submit file,
      • Print a copy of your program with the command

        cat C++file.cpp

        where C++file.cpp is the name of the file containing your C++ program.
      • If your work involves several files, list the main program first with the cat command; then for each separate class/implementation file, list the header file immediately before its implementation file.
      • Compile and run the program with appropriate test cases to check the correctness of your program.
    • When your runs are complete, stop the submit session by typing <Ctrl/D>.
    • Print the record of your session by typing

      print filename

  3. Either write on your printout or include a separate statement that argues why your program is correct, based upon the evidence from your test runs.
Some Grading Notes:

Supplemental Problems

  1. Making Change: Write a program that reads the amount of change (less than a dollar) to be given to a customer, and then prints how many coins of each type (pennies, nickels, dimes, and quarters) should be given as change. Your computation should use the fewest number of coins. For example, if 99 cents were required, the program should specify 3 quarters, 2 dimes, 0 nickels, and 4 pennies.

  2. Type of Triangle: Write a program that reads and analyzes three real numbers as potential sides of a triangle. The output should identify the type of triangle formed from these sides, as follows:
    
      A triangle cannot be formed from sides of these lengths.
      The triangle is equilateral.
      The triangle is isosceles (but not equilateral).
      The triangle is scalene (no two sides are equal).
    

  3. Bank Interest: If an amount of money A is deposited into a savings account which earns interest at the rate of R% per year compounded monthly, then after N months, the bank account will contain the amount A(1+R/1200)N.

    Write a program that reads an initial amount A and the annual interest rate R, and then computes and prints the monthly bank balance until the balance has doubled. The number of the month and the monthly balances should be printed in a table. For example, if $100.00 were deposited at a 12% annual interest, the start of the output might look as follows:

    
    Month    Balance
      0      100.00
      1      101.00
      2      102.01
     ...
    

  4. The Euclidean Algorithm: Write a program that uses the Euclidean Algorithm to find the greatest common divisor (gcd) of two positive numbers.

    To compute the greatest common divisor of two positive integers M and N, the Euclidean algorithms may be described as follows:

    • Divide N by M to get a remainder R1.
    • Divide M by R1 to get a remainder R2.
    • Continue in this fashion, dividing Ri by Ri-1 to get a remainder Ri+1, until obtaining a remainder Ri+1 = 0.
    The value Ri then is the desired gcd(M, N).

    Example: To find gcd(66, 24):

    • Divide 66 by 24 to get the remainder 18.
    • Divide 24 by 18 to get the remainder 6.
    • Divide 18 by 6 to get the remainder 0.
    Since the remainder is now 0, the gcd(66, 24) is 6, the previously computed remainder.

    Programming Note: Arrays are not needed in this problem. Rather, maintain current values for M, N, and the remainder R, and update each value from one iteration to the next.

  5. Counting Letters in a File: Write a program that reads the name of a file and then counts the number of times each letter occurs, ignoring case differences. Non-letters should be ignored. Include results for file "/home/walker/301/sched" in your testing.

  6. An Expanded List Class: Consider class List and class OrderedList as found defined and implemented in ~walker/c++/examples/lists/list-class.h . These classes are used and tested with program list-app1.cc in directory ~walker/c++/examples/.

    Expand class List to include the following operations:

    Expand class OrderedList to include the following operations:

    Check that both print_last and largest work for both class List and class OrderedList. Of course, delete_duplicates will be defined only for class OrderedList.

    Note: While you will need to add the above methods to these classes, the current implementation should not be changed in any way. Rather, you only will need to perform additions to the class definitions and corresponding new implementation. The current methods may not be altered in this assignment.

  7. External Sorting in a File: File ~walker/301/integer.file contains approximately 20,000 integers in random order. This problem asks you to sort data from this file into ascendng order, creating a new file sorted-integer.file in the process.

    For comparison, this assignment will assume that main memory is large enough to store only 1000 integers at a time. Thus, it will be necessary to generate initial runs of the data and to merge successive runs to form a final sorted file. In order to compare various methods, this assignment involves the following steps:

    1. Generate initial runs as follows:
      1. using a merge sort (of 1000 items at a time),
      2. using an insertion sort (of 1000 items at a time), and
      3. using a priority queue (of up to 1000 items).

    2. After generating the initial runs, implement the merge step with 2 files per bank (4 files total).

    3. Use a clock (with a second hand) or the time functions of C++ to get a rough timing of the total sorting process with each of run-generation approaches. Can you make any conclusions about the best ways to generate runs for random data?

    4. Extra Credit: Implement the merge step with 4 and 6 files per bank (8 or 12 files total), and perform similar timings. Can you make any conclusions about timings for different numbers of files per bank?

    5. Extra Credit: Rerun the above timings with files ~walker/301/integer.file-ascending and ~walker/301/integer.file-descending, which contain the integers from ~walker/301/integer.file in ascending and descending order, respectively. What conclusions about the best approach(es) for run generation can you make in these cases?

    During the formal process for submitting programs, for this problem you should run the Unix utility diff to compare your output file with ~walker/301/integer.file-ascending. Since diff will report any differences between your file and the correctly sorted data, this will provide an efficient way to check the output of your program. You should not print out a copy of the sorted data; a check with diff provides an effective mechanism for saving a few trees.


This document is available on the World Wide Web as

http://www.math.grin.edu/~walker/courses/301.fa98/assignments.html

created August 19, 1998
last revised October 7, 1998