Winter 2004 CS 31 (Shinnerl)
Programming Assignment 3: Growth Rates
Official
Specification posted 12:00pm, Wed., 1/21
DUE: 10:00PM Wed. 1/28
Write a modular C++ program to generate
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 examine.
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 (linear growth rate), 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, it is sufficient and customary to consider 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 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 sequence fragment, separating successive elements by commas and space.
The sum of the terms in the fragment
is then displayed, on a separate line of output,
preceded by the label fragment sum:. See the examples
below.
Additional Rules
- The program automatically determines whether each sequence
is an integer sequence.
To decide whether a given number provided by the user is an integer,
you may first read and store it in floating-point and then compare
it to its static_cast<int> truncation, as follows.
double x; int i;
bool isInteger = false;
cin >> x;
if ( abs(x) < INT_MAX ){
i = static_cast<int>(x);
isInteger = (x == i); // ...same as ( x == double(i) ).
}
The constant INT_MAX is the largest integer representable
on the given machine and is available from the standard library
<climits>.
- Integer sequences should be processed with integer arithmetic
and displayed simply as integers in the usual way, as long as overflow
does not occur. Similarly, floating-point sequences should be processed
and displayed in floating-point.
- Your program must automatically detect the case where
the elements in an integer sequence fragment become too large to store in
the usual int type variable. When this happens, your program
should switch to a floating-point representation, both for internal
calculation and output.
- 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.
Any other numerical restrictions
on the input should be carefully enforced.
- 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
-
Sequences longer than 10 elements should be displayed in lines
of 10 elements each, except possibly for the last line.
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: 2000000000
Enter the difference d between successive elements: 100000000
Enter the fragment length L: 4
Fragment:
2000000000, 2100000000, 2.2e+09, 2.3e+09
Fragment Sum: 8.6e+09
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
Homework 3 Home Page