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

  1. 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.

  2. If the user selects a (arithmetic, a.k.a. linear growth), the program prompts for
    1. the first element in the sequence
    2. the difference between successive elements: d = si+1 - si.
    3. the number of elements to generate, i.e., the "fragment length" (Recall that in this type of sequence, d is constant, independent of i.)

  3. 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
    1. the degree n of the monomial
    2. the value of the positive constant c
    3. the initial value of the base x
    4. the base increment, i.e., the constant difference d between successive values of the base x
    5. the number of elements to generate, i.e., the "fragment length"

  4. 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.

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

  1. 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); .

  2. You may store all numerical input as type double.

  3. 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.

  4. 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.

  5. Allow users to study rates of decay as well as rates of growth by allowing them to provide the following kinds of parameter values.
    1. negative differences for arithmetic sequence fragments
    2. negative integer exponents for polynomial sequence fragments
    3. positive ratios less than one for geometric sequence fragments

    4. The format of the numbers you display is not specified, you may just rely on the default format used by the << operator.

    5. 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