Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Manipulating Polynomials Out: 3/22 Due: 4/11 by 11:59 PM Learning Objectives Creating and Using Dynamic Arrays Manipulating Arrays Using Arrays as Formal Parameters Polynomial Definition

Manipulating Polynomials Out: 3/22 Due: 4/11 by 11:59 PM Learning Objectives Creating and Using Dynamic Arrays Manipulating Arrays Using Arrays as Formal Parameters Polynomial Definition 1. A univariate polynomial is a mathematical expression involving a sum of powers in one variable multiplied by coefficients. A polynomial in one variable with constant coefficients is given by the expression p(x) = Pn i=0 cix i , where the ci 0 s are numeric values representing the coeffi- cients. The polynomial is said to be an n th-degree polynomial if its highest power is n. For example, 3x 4 ? 2x 3 + x 2 ? 1 is a fourth-degree polynomial. To evaluate a univariate polynomial, given a numeric value, the value is substituted for the variable and the expression is evaluated. For example, given the polynomial p(x) = 3x 4 ? 2x 3 + x 2 ? 1, p(?2) = 67 since 3 (?2)4 ? 2 (?2)3 + (?2)2 ? 1 = 67. A univariate polynomial can be represented as a degree-coefficients array, that is, with its first element representing the degree of the polynomial and the remaining elements its coefficients in order of descending powers. For example, the polynomials 3x 4 ? 2x 3 + x 2 ? 1 and 3x 2 + 2x ? 5 are represented as [4, 3, ?2, 1, 0, ?1] and [2, 3, 2, ?5], respectively. In this project you will write an interactive program that represents univariate polynomials using arrays in which the coefficients are arranged in order of descending powers. The program will have functions generate a string representation of a polynomial in standard form, compute the indefinite integral of a polynomial, compute the derivative of a polynomial and evaluate a polynomial. Duncan 1 Spring 2018 Manipulating Polynomial CSc 1253: Project #4 In order to effectively manage memory, the array representing the polynomial entered by the user will be dynamically allocated. To dyanically allocate an array, the following syntax may be used: arrayType* arrayName = new arrayType[arraySize]; Example 1. double* salaries = new double[10]; Dynamically allocating an array gives a programmer the flexibility of being able to create an array of any size during run-time and deallocating the array when it is no longer needed. Unlike static arrays that are deallocated when the function in which they are declared is terminated, dynamically allocated arrays are fully within the programmers control and may be deallocated at any point after they are created. The example above creates an array of doubles that has the size of 10. To deallocate a dynamically allocated array, use the following syntax: delete[] nameOfArray; This example shows how the salaries array can be deallocated. Example 2. delete[] salaries; When an array is deallocated, it is no longer accessible. It has to be reallocated if there is a need to re-use it. Proceed with caution when working with dynamically allocated arrays. When a variable associated with a dynamically allocated array is re-used, be sure to deallocate the array before using the variable. Failure to do so leads to memory leak. Memory leak is the allocation of a chunk of memory that contains inaccessible data and cannot be reallocated. There is a lot more to dynamic memory allocation. For now, this is enough to get us through the project. Write a program called PolyManipulator that uses a modular design. This program will involve the implementation of several functions that use arrays as formal parameters. Some function will return dynamically allocated arrays. As was the case with your previous project, use an sstream object when writing the polyToStr function that returns a string representation of a polynomial. Duncan 2 Spring 2018 Manipulating Polynomial CSc 1253: Project #4 In addition to the main, your program should consists of the following functions: /** * Gives a string representation of this polynomial in standard * form where zero terms, coefficients 1 or -1, and exponents 1 * are not displayed. *

* Note: Rules for Representing a Polynomial in Normalized Form: * 1. If the degree of the polynomial is 0, return a string * representing the number. * 2. If the degree of the polynomial is 1, return a string * representing the polynomial in the form ax + b, where when * b is zero it should not be displayed and when a is -1 or 1 * it should not be displayed as a coefficient of x. * 3. If the degree of the polynomial is 2 or more, follow these * steps: * A. Generate the string representation of the highest order * term without using 1, -1 as its coefficient. * B. Generate the string representations of all other, but * the last two, terms beginning from the second highest * order term without the use of 1 and -1 as coefficients * and without including a zero term. Then deal with the * last two terms: * i. If its linear term is non-zero, generate and append * the linear term but without the use of 1 and -1 as * its coefficient and 1 as its exponent. * ii. Finally, append the constant term, the lowest order * term, if it is non-zero. * eg: [6, 3, 0, -1, 0, 1, 1, 0] -> 3x^6 - x^4 + x^2 + x * [5, -1, 0, 3, 0, -1, 1] -> -x^5 + 3x^3 - x + 1 * 

* @param poly a degree-coefficients array representation of a * polynomial. * @return a string representation of this polynomial */ String polyToString(const double poly[]) Duncan 3 Spring 2018 Manipulating Polynomial CSc 1253: Project #4 /** * Evaluates the polynomial represented by the array at the * specified value. * @param poly a degree-coefficients array representation of a * polynomial. * @param x numeric value at which the polynomial is to be * evaluated. * @return the value of the polynomial when evaluated with x */ double polyEval(const double poly[],double x) /** * Generates the array representation of the derivative of a * polynomial *

* Note: The degree of a polynomial of degree 0 is 0. This * function should return an array of size 2 whose * elements are [0, 0] when called using a polynomial * of degree 0. * 

* @param poly a degree-coefficients array representation of a * polynomial. * @return the array representation of the derivative of the * specified polynomial. */ double* differentiate(const double poly[]) /** * Computes an indefinite integral for the specified * polynomial * @param poly a degree-coefficients array representation of a * polynomial. * @param c the constant term * @return the array representation of the integeral of the * specified * polynomial with the specified constant term. */ double* integrate(const double poly[], double c) Duncan 4 Spring 2018 Manipulating Polynomial CSc 1253: Project #4 Integral and Derivative of a Polynomial The integral and derivative of polynomials have applications in many fields including mathematics, the physical sciences and engineering. When given a univariate polynomial f(x) to calculate its derivative, denoted f 0 (x) and read f prime of x, we compute the derivative of each of its terms. The derivative of the term cix i is ciixi?1 . For example, the derivative of ?4x 7 is ?28x 6 . Observe that the derivative of the constant term, a number, is 0 since any constant c can be expressed as cx0 . Also, f 0x) has one fewer term than f(x) if the degree of f(x) is greater than 0. The indefinite integral of f(x), denoted R f(x)dx, is a polynomial obtained by computing the integral of each term of its terms. The integral of the term cix i is ci i+1x i+1. For example, the integral of 3x 4 is 0.6x 5 . When determining the indefinite integral, we add an arbitrary constant so the integral of f(x) has one more term than f(x). Suppose the definite integral of f(x) is denoted F(x), then we can compute the definite integral over a range of values [xmax, xmin] by calculating F(xmax) ? F(xmin) and obtain a numeric value. That is, by evaluating the indefinite integral between two values of the variable x, we obtain the definite integral. The Main function Write the program incrementally. First get the polyToStr to work. Then get polyEval to work. You can then write differentiate followed by integrate. When the functions are all working correctly, you can write the main. When the program begins, it prompts the user for the degree of a polynomial and its coefficients in order of descending powers as shown in the sample run. It then dynamically allocates an array and stores the degree and coefficients in an array in degree-coefficients format as described in this handout. The highest order coefficient of the polynomial must be non-zero. You may assume that the user always enters the correct number of coefficients with the first being non-zero. The program then displays the polynomial using the polyToStr function. It prompts the user for a value at which to evaluate the polynomial. It evaluates the polynomial and displays the result. It then computes and displays the derivative of the polynomial. It also computes the indefinite integral of the polynomial using 1 as the constant. Finally, it prompts the user for the upper and lower limits and then computes the definite integral for the polynomial using those limits. Duncan 5 Spring 2018 Manipulating Polynomial CSc 1253: Project #4 A typically run of the program would be: Listing 1: Code Segment 1 Enter the degree of the polynomial -> 3 2 3 Enter the coefficients -> 4 1 0 1 4 f ( x ) = 4 x ^3 + x ^2 + 1 5 6 Enter a value at which to evaluate this polynomial -> 3 7 f (3) = 118 8 9 f( x ) = 12 x ^2 + 2 x 10 Integrate [ f ( x ) ,x ] = x ^4 + 0.333333 x ^3 + x + 1 11 12 Enter the upper and lower limits of the integral -> 3 0 13 Integrate [ f ( x ) ,x ,3 ,0] = 93 Submitting Your Work 1. The source file that you submit must have header comments that follow this template : /** * Describe what your program does. * @author type your name * @since the date the program was written * Course: csc1253.01 * Programming Project #: 4 * Instructor: Dr. Duncan * @author YOUR NAME * @since DATE THE PROGRAM WAS LAST MODIFIED */ 2. Verify that your code has no syntax error and that it is ISO C++ 11 compliant.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions