# Polynomial representation Using Array

Polynomials are fundamental mathematical expressions used extensively in various fields. Representing them efficiently in programming is crucial for calculations and manipulations. This section introduces polynomial representation using arrays, a method that stores each term’s coefficient and exponent in a sequential manner, suitable for beginners in computer programming or mathematics.

## Array-Based Representation of Polynomials

• Concept of Polynomial Representation:
• Explaining polynomials as a sum of terms with coefficients and exponents.
• The role of arrays in storing these terms efficiently.
• Structuring an Array for Polynomials:
• Describing how coefficients and exponents are mapped to array indices.
• Examples to illustrate the representation.

## Implementing Polynomial Operations Using Arrays

• Algorithms for adding and subtracting polynomials using arrays.
• Code snippets in C to demonstrate these operations.
• Multiplication and Division:
• Discussing more complex operations like multiplication and division.
• Challenges in array-based representation for these operations.

• Efficiency in Storage and Access:
• The benefit of using arrays in terms of memory efficiency and quick access.
• Limitations:
• Discussing the fixed size of arrays and issues with sparse polynomials.
• Comparing with other representations like linked lists.

To demonstrate polynomial representation using an array, let’s consider an example of a polynomial and how it can be represented using this data structure.

## Example Polynomial

Consider the polynomial:

P(x)=7x^3+4x^2−6x+7

### Representing the Polynomial with an Array

In an array-based representation, each element of the array corresponds to a coefficient of the polynomial, with the array index representing the exponent. For the given polynomial, we can use an array of size 4 (since the highest exponent is 3).

1. Array Structure:
• The array index represents the exponent of `x`.
• The value at each index represents the coefficient of the term.
2. Representation in Array:
• Array: `P[4]`
• `P[0] = `7 (Coefficient of `x^0`)
• `P[1] = -6` (Coefficient of `x^1`)
• `P[2] = `4 (Coefficient of `x^2`)
• `P[3] = 7` (Coefficient of `x^3`)

### Visual Representation

The polynomial 7x^3+4x^2−6x+7 would be represented in an array as:

### Array Indexing

• The array index directly corresponds to the exponent, making it intuitive to access specific terms.
• For example, to access the coefficient of x2, we look at `P[2]`, which is 4.

### Algorithm to Represent and Evaluate a Polynomial using an Array

#### Step 1: Define the Polynomial

• Given polynomial: P(x)=7x^3+4x^2−6x+7

#### Step 2: Initialize the Array

• Create an array `P` of size 4 (since the highest exponent is 3).
• Initialize the array with coefficients: `P[4] = {7, -6, 4, 7}`.

#### Step 3: Evaluate the Polynomial

• To evaluate at a specific value of `x`, say `x = 2`.
• Initialize a variable `result` to 0.
• Loop through the array from index 0 to 3.
• For each index `i`, calculate `P[i] * 2^i` and add it to `result`.

#### Step 4: Print the Result

• After the loop ends, `result` will hold the value of the polynomial at `x = 2`.
• Print `result`.

#### Example: Evaluate at x = 2

1. Initialization:
• `result = 0`
• `x = 2`
2. Loop through Array:
• For `i = 0`: `result += P[0] * pow(x, 0) = 0 + 7 * 1 = `7
• For `i = 1`: `result += P[1] * pow(x, 1) = 7 - 6 * 2 = -`5
• For `i = 2`: `result += P[2] * pow(x, 2) = -5 + 4 * 4 = 11`
• For `i = 3`: `result += P[3] * pow(x, 3) = 11 + 7 * 8 = 67`
3. Result:
• `result = 67`
4. Output:
• Print: “Polynomial value for x = 2 is: 67”.

## C Program for Array Representation

``````#include <stdio.h>
#include <math.h>

// Function to evaluate a polynomial using an array
int evaluatePolynomial(int coefficients[], int degree, int x) {
int sum = 0;
for (int i = 0; i <= degree; i++) {
sum += coefficients[i] * pow(x, i);
}
return sum;
}

int main() {
// Example Polynomial: 3x^2 + 2x + 1
int coefficients[] = {1, 2, 3}; // Array representation (constant term first)
int degree = 2; // Degree of the polynomial
int x = 2; // Value at which we want to evaluate the polynomial

int result = evaluatePolynomial(coefficients, degree, x);
printf("Value of the polynomial at x = %d is: %d\n", x, result);

return 0;
}
``````

Output

``Value of the polynomial at x = 2 is: 67``

### Program Explanation

``````#include <stdio.h>
#include <math.h>``````

`stdio.h`: Standard input-output header, used for functions like `printf`.

`math.h`: Math library header, used for the `pow` function to calculate powers.

Function to Evaluate Polynomial:

``````// Function to evaluate a polynomial using an array
int evaluatePolynomial(int coefficients[], int degree, int x) {
int sum = 0;
for (int i = 0; i <= degree; i++) {
sum += coefficients[i] * pow(x, i);
}
return sum;
}``````

`evaluatePolynomial`: This function calculates the value of a polynomial for a given value of `x`.

It takes three parameters: an array `coefficients` containing the coefficients of the polynomial, the `degree` of the polynomial (highest exponent), and the value `x` at which the polynomial is evaluated.

The function initializes a variable `sum` to 0, which accumulates the value of the polynomial.

A `for` loop iterates over each term of the polynomial. For each term, it calculates the term’s value by raising `x` to the power of the term’s exponent (`i`) and multiplying it by the coefficient (`coefficients[i]`), then adds this value to `sum`.

Finally, the function returns the total sum, which is the value of the polynomial at `x`.

main Function:

``````int main() {
// Example Polynomial: 3x^2 + 2x + 1
int coefficients[] = {1, 2, 3}; // Array representation (constant term first)
int degree = 2; // Degree of the polynomial
int x = 2; // Value at which we want to evaluate the polynomial

int result = evaluatePolynomial(coefficients, degree, x);
printf("Value of the polynomial at x = %d is: %d\n", x, result);

return 0;
}``````

The `main` function is the entry point of the program.

It first defines the polynomial to be evaluated. In this case, the polynomial is `7x^3 + 4x^2 - 6x + 7`. The coefficients are stored in an array `coefficients` in the order of increasing exponents.

The degree of the polynomial is specified as 3 (since the highest power of `x` is 3).

The value of `x` at which the polynomial needs to be evaluated is set to 2.

The program then calls `evaluatePolynomial`, passing the coefficients array, degree, and the value of `x`.

The result of the polynomial evaluation is stored in `result` and printed out using `printf`.

Output:

The program calculates and prints the value of the polynomial `7x^3 + 4x^2 - 6x + 7` at `x = 2`.