Please I have a little problem with my project. It goes
"A polynomial of degree n is represented as
Pn(x)=A0 + a1x + a2x2 + … + anxn
Where a0, a1, a2, a3 , … , an are constant coefficients. Evaluation of suc polynomials may be important if Pn(x) is to be computed many times for different values of x. Design and implement a program that asks for the user to enter a value for the degree of the polynomial n, and values for n+1 coefficient.
Your program should read these values and then in a loop it should prompt the user to enter the values for the polynomial. Your program should terminate execution if the last two values read for x are equal. Your program should be able to process polynomials of degree 6 or less."
So far i guess that n+1 should be an array, but i don't know what to do. Please I need help

Structurally speaking, it should look like this:
1. Read the degree of the polinomial. ( n )
2. For i = 0 ; i <= n; i++ ( A grade N polynomial has N+1 coefficients, that's the reason for using <= instead of < )
Read the contents of an array. These will be the a0....aN coefficients.
Then prompt the user to enter a value ( this will be the X value)
Using the stament
Value = a0 * x^ 0 + a1 * x ^1 + .... + aN * x^n , run through the array and compute the result.

Value = a0 * x^ 0 + a1 * x ^1 + .... + aN * x^n , run through the array and compute the result.

Just as NB, don't do it directly as written, it is highly inefficient.
a0 + x*(a1 + x*(a2 + ... +x*(a[N-1] + x*aN)))))...))) is a way to go.

Edited 6 Years Ago by nezachem: n/a

Yeah...and that could go recursively i guess. :)
Edit: Why bother about efficiency? Even if we were still on Pentium 1's, for such a small scale project, it really wouldn't matter.

Edited 6 Years Ago by TotoTitus: n/a

Structurally speaking, it should look like this:
1. Read the degree of the polinomial. ( n )
2. For i = 0 ; i <= n; i++ ( A grade N polynomial has N+1 coefficients, that's the reason for using <= instead of < )
Read the contents of an array. These will be the a0....aN coefficients.
Then prompt the user to enter a value ( this will be the X value)
Using the stament
Value = a0 * x^ 0 + a1 * x ^1 + .... + aN * x^n , run through the array and compute the result.

But can you like write it in C format. And how can you make a value
multiply each element of the array

Well, here it goes:

#include <stdio.h>
#include <math.h>
int grade, x;
int a[20];
long value=0;
printf("Enter polynomial grade\n");
scanf("%d",&grade);
printf("Enter a0...aN values\n");
for(int i=0; i<=grade;i++)
{
    printf("\nEnter a%d",i);
    scanf("%d",&a[i]);
}
printf("\nEnter value of X");
scanf("%d",&x);
for(i=0;i<=grade;i++)
{
    value+=a[i]*pow(x,i);
}
printf("\nThe value of the polynomial is %ld",value);

Correct me if there are any mistakes.

Edited 6 Years Ago by TotoTitus: n/a

Yeah...and that could go recursively i guess. :)

It can, but why?

Edit: Why bother about efficiency? Even if we were still on Pentium 1's, for such a small scale project, it really wouldn't matter.

Consider it a common courtesy. The program is not alone in the system. It shall consume as few resources as possible. Besides extra cycles burned contribute to a global warming.

Seriously, even in a small project you may face surprises. Efficiency is not just speed and/or footprint. It is also a way to handle data correctly. This is especially important when dealing with floating point values. Each arythmetic operation results in a certain lost of precision. A wrong evaluation schedule may make such loss intolerable. The Horner schedule I mentioned reduces a number of operations dramatically therefore giving much better precision and less risk of underflowing.

This article has been dead for over six months. Start a new discussion instead.