CS2134
Fall 2007
Homework #1
Due Tuesday September 18, 11:59 p.m.
Your first assignment has a programming portion and a written portion. The
programming portion should consist of a single file called hw01.cpp, and the written
portion should be a single file called hw01written, in a format commonly understood as
indicated with its extension (for instance, .txt,
doc, .html, or .pdf). Be sure to include
your name and section at the beginning of each file!
You must hand both files via the digital drop box on MyPoly.
Programming Part:
In this program, you’ll measure the actual running time of three algorithms, to help you
understand the relationship between code structure, running time in big-Oh notation,
and clock running time.
Given a vector a[] of integers, suppose we wish
to find the largest distance between
two elements, i.e., the largest value of a[i] – a[j], for any array indices
i and j.
Here are three algorithms for the problem:
1.
For each ordered pair (i,j), compute a[i] – a[j], keeping track of the maximum
difference found so far.
2.
For each ordered pair (i,j) with j >i , compute |a[i] – a[j]|, keeping track of the
maximum difference found so far.
3.
Note that the maximum will be achieved when a[i] is the largest element in the
array and a[j] is the smallest. Scan the vector once from left to right, keeping
track of the largest element seen so far and the smallest seen so far. After
completing the scan, subtract the smallest from the largest.