CSC 344
Homework 1

Assigned Feb. 4, due Feb. 21

Some of these problems can be done in varying levels of detail. The more work you do, and the more aspects of the problem you explore, the more impressed I'll be and the more credit you'll get.

Problems from the textbook

A sorting algorithm: (1 point)

Here's a silly way to sort a bunch of numbers: generate each of the possible permutations of the numbers, and for each one, test whether it's in order. Analyze this algorithm: tell how long it takes, as a function of how many numbers there are, using big-O notation.

Another sorting algorithm: (1 point)

A slightly less-silly way to sort: for each number, count how many of the other numbers are less than it, and use this count to put it into the right location in an array. Then read off the array from beginning to end. Analyze this algorithm: tell how long it takes, as a function of how many numbers there are, using big-O notation. Do you see any significant limitations of this algorithm? Can you fix them?

A problem from another textbook:

You are given a bunch of floating-point numbers, and you want to find which two of them are closest together. Describe an algorithm (in pseudocode) that solves this problem. Analyze the algorithm: tell how long it will take, as a function of how many numbers there are. You'll get one point for a correct but inefficient algorithm, two for a more efficient one.

Computing square roots:

The square root of a number tends to be either quite simple (e.g. sqrt(9) = 3) or irrational, a non-repeating infinitely long decimal. So most algorithms for computing square roots involve successive approximations: they start with a guess at the right answer, then use it to compute a closer guess, then a still closer guess, etc. until the approximation is "close enough" for the purpose at hand. (The "close enough" criterion usually determines how long the algorithm takes, acting like a size parameter.)

One simple algorithm for computing square roots is binary search. Given a positive number c, you know that the square root is somewhere between 0 and c (technically this is only true if c>1; assume that for now and we'll come back to it later). So pick a guess in the middle of that range, c/2. If (c/2)2 is larger than c, that means your guess was too high, and the actual answer is between 0 and c/2; if smaller, your guess was too low and the actual answer is between c/2 and c. In either case, you now have a range of possible answers only half as large as you had before. Repeat the process until the range is "small enough": for example, if you want an answer correct to two decimal places, repeat until the range is less than 0.01.

Another algorithm, developed by the ancient Babylonians at least 3500 years ago, is as follows. Suppose you want a square of area c. Start with a rectangle of dimensions 1 x c; this has the right area, but it's not square. So take the average of these two dimensions, yielding (1+c)/2, and use this as one dimension to get a "more nearly square" rectangle. If one dimension is (1+c)/2 and the area is c, then the other dimension must be c/((1+c)/2), or 2c/(1+c). This now has the right area, but it's not quite square either, so take the average of these two dimensions.... If you want an answer correct to two decimal places, repeat until your two dimensions are within .01 of one another.

Trace the action of each of these algorithms to find the square root of 5, correct to three decimal places. Then do the same to find the square root of 29, correct to three decimal places. (You may want to write a little program to help with this, but it's not required.)

For each of the two algorithms and each of the two numbers (5 and 29), how many iterations did it take to reach an accuracy of .1? An accuracy of .01? An accuracy of .001?

Programming:

Write a program in your favorite programming language -- C, C++, Java, Scheme, Prolog, perl, Pascal, etc. -- to manipulate polynomials. A polynomial is (usually) represented as a list of coefficients: for example, the list 3, -4, 2 could represent the polynomial

3x2 -4x + 2

You need to write the following functions/methods:

For each function/method, be sure to provide well-chosen test cases with correct answers (preferably written before you write the function/method itself), so it's easy to test the program.

I'm not really interested in your ability to write I/O and driver code, so I recommend doing this problem in a system that allows you to call functions directly and interactively, such as BlueJ, SWI-Prolog, or DrScheme.


Last modified: Mon Feb 14 09:45:11 EST 2011
Stephen Bloch / sbloch@adelphi.edu