Question

A polynomial P is a function defined by an array of coefficients CI. Example : C-(4, 2, 1), then In the general case, given t

*Please try to use C++ specific coding for my understanding because I never learned C.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//pls provide +ve rating, thanks

#include<iostream>

using namespace std;

int poly(int x, int coe[], int n);
int derivative(int x, int coe[], int n);

int main()
{
   int n;
   cout << "Enter number of terms: ";
   cin >> n;
   int *coe;
   //allocate memory for n number of elements
   coe = new int[n];
  
   for (int i = 0; i < n; i++)
   {
       cout << "coefficients " << i << ": ";
       cin >> coe[i];
   }
   int x;
   cout << "Enter the value of x: ";
   cin >> x;
  
   cout << "P(x): " << poly(x, coe, n) << endl;;
   cout << "P'(x): " << derivative(x, coe, n);
}

int poly(int x, int coe[], int n)
{
   int sum = 0;
   for (int i = 0; i < n; i++)
   {
       sum += coe[i] * pow(x, i);
   }
   return sum;
}
int derivative(int x, int coe[], int n)
{
   int sum = 0;
   for (int i = n - 1; i > 0; i--)
   {
       sum += i * coe[i] * pow(x, i - 1);
   }
   return sum;
}

=====================================

/*Output1
Enter number of terms: 3
coefficients 0: 4
coefficients 1: 2
coefficients 2: 1
Enter the value of x: 2
P(x): 12
P'(x): 6
//output2
Enter number of terms: 4
coefficients 0: 89
coefficients 1: 6
coefficients 2: 4
coefficients 3: 3
Enter the value of x: 2
P(x): 141
P'(x): 58
*/

Add a comment
Know the answer?
Add Answer to:
*Please try to use C++ specific coding for my understanding because I never learned C. A...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • I need help with my homework please. I should write this program in C++ language and...

    I need help with my homework please. I should write this program in C++ language and use Inventory.h file (header file), Inventory.cpp file (implementation file), and main.cpp file (main program). This is the program: Demonstrate the class in a driver program. Input validation, don’t accept negative values for item number, quantity, or cost. 6. Inventory Class Design an Inventory class that can hold information and calculate data for items ma retail store's inventory. The class should have the following private...

  • The polynomial addition C function of Program 2.6 padd is the code when the polynomial is used to...

    The polynomial addition C function of Program 2.6 padd is the code when the polynomial is used to arrange the polynomial in the two arrangement methods of the polynomial described in the text 2.4.2. For the remaining method, when the expression polynomial is arranged by a coefficient, create a polynomial addition C function padd() corresponding to Program 2.6. 66 Arrays And Structures are zero are not displayed. The term with exponent equal to zero does not shouw able since x...

  • C++ Could you check my code, it work but professor said that there are some mistakes(check...

    C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private:    string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){    return name; } }; class Circle : public...

  • Please Use C++ for coding . . Note: The order that these functions are listed, do...

    Please Use C++ for coding . . Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all.cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printodd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention...

  • please can I have help understanding part C A block of 20 kg is connected as...

    please can I have help understanding part C A block of 20 kg is connected as shown in Figure QA1. Numerical values: k = 600 N/m, c = 40 N.s/m K I Soros (N) Figure QA1. Mass connected between two springs 2 a. Write the equation of motion of the system. [5 marks] b. Determine the damping ratio of the system and establish what type of motion occurs (underdamped, overdamped, critically damped) [5 marks] =. The mass is displaced 1...

  • C++ please use only easy code and stdio.h because im just a beginner Description Write the...

    C++ please use only easy code and stdio.h because im just a beginner Description Write the program that can manage the information of students as follows: S 1. This program has two phase; the input phase and the output phase. 2. In an input phase, a teacher inputs the information (name, score, and department) for each student. The total number of students who can be inputted should be defined as a NUM OF_STUDENT constant value. In this example, the value...

  • Please Use C++ Language. Thank you. Please I need the actual code. Donot post psudocode!! ​And...

    Please Use C++ Language. Thank you. Please I need the actual code. Donot post psudocode!! ​And also I have codes but just donot work so make sure that it works. Requested files: CrosswordGenerator.cpp, CrosswordGenerator.h, CrosswordGenerator_test.cpp CrosswordGenerator - Write a program that helps to generate a crossword puzzle by organizing words that share letters.   For this assignment, you will write a program that forms the basis of a crossword puzzle generator. In order to create a crossword puzzle you need to...

  • I have updated my previously posted C++ question to ensure that I have included all of...

    I have updated my previously posted C++ question to ensure that I have included all of the necessary documentation in order to complete Part 2 - Bank Account of the assigned lab in its entirety. Please note that the lab below is somewhat lengthy, and I do not expect you to answer each question! If coding the full lab is too much to ask, please focus soley on Part 2 of the lab, titled Bank Account and the subsections that...

  • I need help with my coding for C++! The goal of this is to calculate the...

    I need help with my coding for C++! The goal of this is to calculate the total cost of a product by combining the products price and the amount of tax, then display it to the user. The numbers we have to test out is 100 for the price and 8.25 for the percentage. When I test is out, the formula works but it isn't rounding the 108.25 to 108.3, which is the total price we are supposed to display...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT