Program to evaluate a polynomial function

Hi everyone!
I'm hoping someone could steer me in the right direction or tell me where I'm going terribly wrong with this code.

I need to enter the degree of the polynomial, create an array to store the coefficients, enter coefficients into the array, display the equation, enter the x value, calculate the answer and then display it. The polynomial has a degree of 3, and the coefficients are 1, -1, 2 and -2, and the value is -2.

This is what I have so far.. I'm failing terribly at calculating the answer but that doesn't mean I didn't jack something up somewhere else as I'm not a strong programmer (as you can tell lol).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <math.h>
using namespace std;

int d; // degree of the polynomial
int i; // 
int c; // 
int value; 
int j;
int p;
int sum;



int main()
{
    cout << "Enter the degree of the polynomial: " << endl;
    cin >> d; // degree of the polynomial
    cout << "The degree of the polynomial you entered was " << d << endl;

    
    int *c = new int[i];
    
    for(i = 0; i <= d; i++)
    {
        cout << "Enter coefficients: " << endl;
        cin >> c[i];
        int c[d+1];

    }
    
        cout << "There are " << d + 1 << " coefficients";
        cout << " The coefficients are: ";
        
        for (i = 0; i < d + 1; i++)
        cout << "\n   " << c[i];
        cout << endl;
        
        cout << " Enter the value for evaluating the polynomials" << endl;
        cin >> value;
        sum = 0;
        cout << " The value is " << value << endl;
        
        cout << "First polynomial is: " << endl;
        cout << c[0] << "x^3 + " << c[1] << "x^2 + " << c[2] << "x + " << c[3] << endl;
        {

            for (i = 0; i <= d; i++)
            p = 1;
            {


                for (j = 0; j <= (d - 1); j++)
                p = p * value;
                sum = sum + p;
                sum = pow(c[0]*value,d)+pow(c[1]*value,d-1)+pow(c[2]*value,d-2)+pow(c[3]*value,d-3);
                
                cout << "The sum is " << sum << endl;
            }
            
        }
             
}
 


1
2
3
4
5
6
7
8
9
    int *c = new int[i]; //i=0 here
    
    for(i = 0; i <= d; i++)
    {
        cout << "Enter coefficients: " << endl;
        cin >> c[i]; //out of bounds
        int c[d+1]; //illegal (array size must be a compilation time constant). 
        //If it compiles for you, statement has no effect
    }
1
2
3
std::vector<int> coefficient(degree+1); //there is no limit in the length of the identifiers
for( int K=0; K<=degree; ++K) //limit the scope of the variables
   std::cin>>coefficient[K];



1
2
3
4
5
6
7
8
9
10
11
for (i = 0; i <= d; i++)
	p = 1; //the only statement of the loop
{ //superfluous braces, this is not part of the loop
	for (j = 0; j <= (d - 1); j++)
		p = p * value;
	sum = sum + p;
	//discarding the previous value
	sum = pow(c[0]*value,d)+pow(c[1]*value,d-1)+pow(c[2]*value,d-2)+pow(c[3]*value,d-3);

	cout << "The sum is " << sum << endl;
}
Last edited on
Topic archived. No new replies allowed.