Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following rules should be observed throughout the assignment: • Each polynom

ID: 3853781 • Letter: T

Question

The following rules should be observed throughout the assignment:

• Each polynomial should be represented as a singly-linked list (see files “list.h” and “list.c” from the book).

• Each element in the linked list should represent one of the terms in the polynomial.

• The data held by each element should be type double representing the constant for that term.

• For example, the polynomial 6.0x3 - 5.3x + 3.1 would be represented by the linked list 6.0 0.0 -5.3 3.1.

a) (1 point) Implement a function called appendTerm:

void appendTerm(List *pPolynomial, double constant);

This function should append (insert at the end) the value constant to pPolynomial. For example, appending 3.1 to pPolynomial already containing 6.0 0.0 -5.3 should result in the value 3.1 being added at the end: 6.0 0.0 -5.3 3.1. If the append fails the program should exit.

b) (2 points) Implement a function called display:

void display(List *pPolynomial);

This function should output the polynomial to stdout in proper polynomial format. For example, displaying polynomial 6.0 0.0 -5.3 3.1 should result in 6.0x^3 - 5.3x + 3.1 being output.

c) (2 points) Implement a function called evaluate:

double evaluate(List *pPolynomial, double x);

This function should evaluate the polynomial for the given value of x and return the result. For example, given polynomial 6.0 0.0 -5.3 3.1 and x having value 7.0 the function should return 2024.0 (the result of evaluating 6.0*7.03 - 5.3*7.0 + 3.1).

d) (4 points) Write a program to test the functions from parts a – c. Your test program should demonstrate creating, displaying, and evaluating the following polynomials with the given values for x:

• x + 1.0 with x = 1.0

• x 2 - 1.0 with x = 2.03

• -3.0x3 + 0.5x2 - 2.0x with x = 05.0

• -0.3125x4 - 9.915x2 - 7.75x - 40.0 with x = 123.45

e. (1 point) Make sure your source code is well-commented, consistently formatted, uses no magic numbers/values, follows programming best-practices, and is ANSI-compliant.

Explanation / Answer

#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
void appendTerm(List *pPolynomial, double constant)//method to append value to list
{
    List *t=pPolynomial;
   if(pPolynomial==NULL)//initially list must be empty
   {
       pPolynomial = (List*)malloc(sizeof(List));//creating new node
       if(pPolynomial==NULL)//if append fails then exit
       exit(0);
      
       pPolynomial.c = constant;//assigning value to new node
       pPolynomial.next = NULL;
      
   }
   else//if list is not empty
   {
           while(t->next !=NULL)
           {
               t=t->next;  
           }//moving to last node
           //creating new node
           t->next = (List*)malloc(sizeof(List));//appending new node to list
           if(t->next==NULL)//if append fails then exit
           exit(0);
           t->next.c = constant;//assigning value to new node
           t->next->next = NULL;//setting next value to constant..
          
   }  
}
void display(List *pPolynomial)
{
   List *t = pPolynomial;
   while(t!=NULL)//loop to traverse list
   {
       cout<<t->c<<" ";//printing values of the list
       t=t->next;//moving to next node  
   }
   cout<<" ";
   return ;
  
}
//method which recursively evaluates the polynomial..
double evaluate(List *pPolynomial, double x)//method to evaluate polynomial for given x value..
{
   static int i=0;
   double value;
   if(pPolynomial==NULL)
   return 0;
   value = evaluate(pPolynomial->next,x);
   value = value+pPolynomial->c * pow(x,i);
   i++;
   return value;
  
  
}


int main()
{
   List *p;
  
   appendTerm(p,6);
   appendTerm(p,0);
   appendTerm(p,-5.3);
   appendTerm(p,3.1);
   display(p);
  
   cout<<evaluate(p,7)<<endl;//evaluating polynomial
   //similary for other polynomials also
   List *q;
   appendTerm(q,1);
   appendTerm(q,1);
   display(q);
   cout<<evaluate(q,1)<<endl;//evaluating polynomial
   List *m;
   appendTerm(m,1);
   appendTerm(m,0);
   appendTerm(m,1);
   display(m);
   cout<<evaluate(m,2.03)<<endl;//evaluating polynomial
   List *n;
   appendTerm(n,-3);
   appendTerm(n,0.5);
   appendTerm(n,-2);
   appendTerm(n,0);
   display(n);
   cout<<evaluate(n,5)<<endl;//evaluating polynomial
   List *o;
   appendTerm(o,-0.3125);
   appendTerm(o,0);
   appendTerm(o, - 9.915);
   appendTerm(o,- 7.75);
   appendTerm(o,- 40.0 );
   display(o);
   cout<<evaluate(o,123.45 )<<endl;//evaluating polynomial
  
  
   ///then append the value..
  
  
   return 0;
}

//note: to run this code you should first include list.h or list.c