Winter 2005 CS 31 (Shinnerl)
Programming Assignment 3: Sequence Generator
DUE: 10:00PM, Fri., 1/28
FINAL
Specification posted 12:30pm, Fri., 1/21
See the FAQ file for a description of the
updates to the original BETA spec..
Write a C++ program to generate and sum
user-specified subsequence fragments demonstrating
linear, polynomial, and exponential
rates of growth, as specified below.
Overview
-
On startup, the program offers the following menu.
Please specify the type of sequence you wish to generate.
a --- Arithmetic (linear)
p --- Polynomial (specify degree)
g --- Geometric (exponential)
Enter 'q' to quit or 'h' to display this menu again.
apgqh>
After generating and displaying a sequence fragment and its sum,
the program displays the simple prompt apgqh>.
Iterations continue in this fashion until the user quits.
-
If the user selects a (arithmetic, a.k.a.
linear growth), the program prompts for
- the first element in the sequence
- the difference between successive elements:
d = si+1 - si.
- the number of elements to generate, i.e., the "fragment length"
(Recall that in this type of sequence, d is constant, independent
of i.)
-
For polynomial growth, the program considers only
monomials, i.e., expressions of the form c xn
for integers n > 1 and constants c > 0.
If the user selects p, the program prompts for
- the degree n of the monomial
- the value of the positive constant c
- the initial value of the base x
- the base increment, i.e., the constant difference
d between successive values of the base x
- the number of elements to generate, i.e., the "fragment length"
-
Processing of geometric, a.k.a. exponential
fragments is identical to that for arithmetic
fragments, except that, of course, instead of the
difference between successive elements,
the constant ratio
between successive elements must be provided:
r = si+1 / si.
After reading in the necessary information, the program displays
the terms of the sequence fragment, in order, up to a maximum of
TERMS_DISPLAY_LIMIT terms plus the final term in the fragment.
Consecutive terms in the output are separated by commas and space.
The integer
TERMS_DISPLAY_LIMIT
is a named constant which you may set to 10.
When the requested number of terms exceeds
TERMS_DISPLAY_LIMIT, the last of the leading
TERMS_DISPLAY_LIMIT terms displayed is separated from the
final term in the fragment by the usual ellipis , ...,;
see the examples below.
The sum of the terms in the fragment is calculated directly
(by adding the terms together one by one).
Check the correctness of your sums by comparing your sums to
the results predicted by the following mathematical formulas.
-
linear (arithmetic)
a0 +
a1 + ...
+ an =
a0 (n+1) + d (n+1) n/2,
where d = ai+1 - ai is constant.
for all i.
-
exponential (geometric)
a0 +
a1 + ...
+ an =
a0 ( 1 - rn+1) / (1 -r ),
where r = ai+1 / ai is constant
for all i.
-
polynomial (approximation for unit base increment only)
x0p
+ (x0 + 1)p
+ (x0 + 2)p
+ ...
+ (x0 + n)p
approximately equals
((x0 + n)p+1 - x0p+1 ) / (p+1).
Note that the formula for polynomial sums is approximate and applies only
when the base increment is 1 and p is not equal to -1.
Additional Rules
-
There is no exponent operator in C++.
To raise a number to a power (exponent) in C++,
include the <cmath> library and use the pow(...)
function. E.g., to get b p, use pow(b,p); .
- You may store all numerical input as type double.
- Robust input (correctly handling all possible kinds of
errors in user input) is not required. You may assume the user's input
is free of format errors and that it is within the limits of the smallest
and largest representable values of type double.
- However, you should guard against the terms in fragments
or the sums of fragments becoming larger or smaller
than can be represented
as type double on your machine. E.g., terms larger than
DBL_MAX (#include <cfloat>) cannot
be processed by your program. DBL_MAX is
approximately 1.79769e+308 in IEEE standard double-precision arithmetic.
If the requested sequence generates terms out of the representable range,
print a suitable error message and return to the usual prompt.
- Allow users to study rates of decay as well as rates of
growth by allowing them to provide the following kinds of parameter values.
- negative differences for arithmetic sequence fragments
- negative integer exponents for polynomial sequence fragments
- positive ratios less than one for geometric sequence fragments
-
The format of the numbers you display is not specified, you may just
rely on the default format used by the << operator.
- If you feel it is important to impose any restrictions on user input
beyond the above specification, you may do so, provided that you explain
the reason for the restriction carefully in your report.
Sample I/O
Please specify the type of sequence you wish to examine.
a --- Arithmetic (linear)
p --- Polynomial (specify degree)
g --- Geometric (exponential)
Enter 'q' to quit or 'h' to display this menu again.
apgqh> a
Arithmetic sequence { a0 + i*d } for i = 0,1,2, ..., L-1.
Enter the first element a0: 3
Enter the difference d between successive elements: 5
Enter the fragment length L: 7
Fragment:
3, 8, 13, 18, 23, 28, 33
Fragment Sum: 126
apgqh> a
Arithmetic sequence { a0 + i*d } for i = 0,1,2, ..., L-1.
Enter the first element a0: 3
Enter the difference d between successive elements: 5
Enter the fragment length L: 15
Fragment:
3, 8, 13, 18, 23, 28, 33, 38, 43, 48, ..., 78
Fragment Sum: 603
apgqh> a
Arithmetic sequence { a0 + n*d } for i = 0,1,2, ..., L-1.
Enter the first element a0: 8
Enter the difference d between successive elements: -6
Enter the fragment length L: 4
Fragment:
8, 2, -4, -10
Fragment Sum: -4
apgqh> p
Polynomial sequence { c*(x0+i*d)^n } for i = 0,1,2, ..., L-1.
Enter the degree n: 2
Enter the constant multiplier c: 1
Enter the initial value of the base x0: 3
Enter the base increment d: 2
Enter the fragment length L: 5
Fragment:
9, 25, 49, 81, 121
Fragment Sum: 285
apgqh> g
Geometric sequence { a0 * r^(i-1) } for i = 1,2, ..., L.
Enter the first element a0: 3
Enter the ratio r between successive elements: 5
Enter the fragment length L: 7
Fragment:
3, 15, 75, 375, 1875, 9375, 46875
Fragment Sum: 58593
apgqh> g
Geometric sequence { a0 * r^(i-1) } for i = 1,2, ..., L.
Enter the first element a0: 3
Enter the ratio r between successive elements: 0.5
Enter the fragment length L: 7
Fragment:
3, 1.5, 0.75, 0.375, 0.1875, 0.09375, 0.046875
Fragment Sum: 5.95312
apgqh> q
Submission
For this assignment, your submission need contain only two files:
report.xxx and p3.cpp. (Choose the suffix
for the report file to match its type --- .txt, .doc, .html, etc..)
Your report should contain
your cover page, documentation, and test results. Your source code
should be neatly formatted, with control blocks consistently
indented and commented.
Homework 3 Home Page