CS 1160 C/C++ Programming 12 MWF - Spring 2016

March 21st thru March 25th - Week #10


Monday 03/21/2016


  1. Review the 23 birthdays array problem. Functions, Selection Sort, Random numbers, Nested Loops, Arrays, the swap() function code. See Week #9, Friday code.

  2. Pointers: Chapter 9, Section 9.1: The Address Of operator. A variable stores values and has an address.

  3. The output of the 91.cpp C++ program: Address Of and the sizeof() some different variables (short, int, double).
    Note: A, B, C, D, E and F are base 16, hexadecimal digits. A = 10 and F = 15.

  4. Chapter 11: Introduction to the struct concept: ADTs and the struct. Polynomial terms. Coefficients and exponents.
    struct polyTerm
    {
       double coef;
       int exponent;
    }; 
    
  5. On Wednesday, we will create an array of polyTerms, so study today's handout (from page 606-607 of textbook) and chapter 11 pages very carefully.
    struct polyTerm
    {
       double coef;
       int exponent;
    }; 
    
    int main()
    {
        const int MAX_TERMS = 8;
        polyTerm poly[MAX_TERMS];
        int i, numTerms, exp;
        double x, value, cf;
    
        cout << "\nHow many terms in your polynomial? ";
        cin >> numTerms;
        
        for (i = 0; i < numTerms; i++)
        {
           cout << "\nEnter the coefficient for term " << i + 1 << ": ";
           cin >> cf;
    
           cout << "\nEnter exponent for term " << i + 1 << ": ";
           cin >> exp;
           
           poly[i].coef = cf;
           poly[i].exponent = exp;
        }
        
        cout << "What value of x would you like to evaluate? ";
        cin >> x;
        ...
        ...
    }
    
  6. Definition of a polynomial.
    POLYNOMIAL definition:  (Algebra)
    (in one variable) 
       an expression consisting of 
       the sum of two or more terms 
       each of which is the product 
       of a constant and a variable raised to an integral power: 
    
         2
       ax  + bx + c is a polynomial, 
    
       where a, b, and c are constants and x is a variable.
    
       Class example from the C++ code in 321.cpp
    
         2                          2      1         0
       3x  +  1.5x - 121.5    or  3x + 1.5x  - 121.5x
    
       The constant is called a coefficient.
       The integral power is called an exponent.
    
       Every term has both, except the exponent does not need to be shown
       when it is 1 or when it is 0.
    

Wednesday 3/23/2016


  1. Output from the polynomial program - testing the array of structs with its evaluate() function.
    How many terms is your polynomial? 4
    
    Enter the coefficient for term 1: 10
    
    Enter exponent for term 1: 4
    
    Enter the coefficient for term 2: -3.5
    
    Enter exponent for term 2: 2
    
    Enter the coefficient for term 3: 5
    
    Enter exponent for term 3: 1
    
    Enter the coefficient for term 4: 1000
    
    Enter exponent for term 4: 0
    What value of x would you like to evaluate? 2  
    
    The value of the polynomial is: 1156
    jacobson[weblab]:~$ 
    
        4       2            
     10x  - 3.5x  + 5x + 1000
    
        4       2     1       0
     10x  - 3.5x  + 5x + 1000x  == 1,156 when x == 2.
    
    
  2. C++ polynomial program with evaluate() function.
    double evaluate( polyTerm[], int, double);      // Compare to your hand-written in class solution
    
    double evaluate( polyTerm p[], int n, double x)  // You tried developing this in class today.
    {
       double pValue = 0;
    
       for (int i = 0; i < n; i++)
          pValue = pValue + p[i].coef * pow(x, p[i].exponent);
    
       return pValue;
    }
    

Friday 3/25/2016


  1. Polynomial In-Class Exercize: Partner Programming - addTerm(p, n, c, e) - to be finished first thing on Monday 03/28 in class. Uses the array of structs example from earlier this week.

  2. Assignment POLYNOMIALS: Functions to read a polynomial from a file, to add two polynomials together to create a third polynomial, and to output a polynomial. Uses the array of structs example we have been studying.

  3. Output to demonstrate the testing of addTerm(p, n, coefficient, exponent) function.