patterncMinor
Evaluating a polynomial
Viewed 0 times
polynomialevaluatingstackoverflow
Problem
You are given a polynomial of degree n. The polynomial is of the form
P(x) = an · xn + an-1 · xn-1 + … + a0
where the ai‘s are the coefficients. Given an integer
x, write a program that will evaluate P(x).
You are provided with a function named
positive integers x and y and returns xy. If y is
0, the function returns 1.
The prototype of this function is
INPUT:
OUTPUT:
A single integer which is P(x).
CONSTRAINTS:
The inputs will satisfy the following properties. It is not necessary
to validate the inputs.
My code
P(x) = an · xn + an-1 · xn-1 + … + a0
where the ai‘s are the coefficients. Given an integer
x, write a program that will evaluate P(x).
You are provided with a function named
power( ) that takes twopositive integers x and y and returns xy. If y is
0, the function returns 1.
The prototype of this function is
int power(int x, int y);INPUT:
- Line 1 contains the integers n and x separated by whitespace.
- Line 2 contains the coefficients an, an-1, …, a0 separated by whitespace.
OUTPUT:
A single integer which is P(x).
CONSTRAINTS:
The inputs will satisfy the following properties. It is not necessary
to validate the inputs.
1 <= n <= 10
1 <= x <= 10
0 <= ai <=10My code
#include
int power(int x, int y);
int main()
{
int n,x,a[11],i; unsigned long long int sum=1;
scanf("%d%d",&n,&x);
if(n10) return 0;
if(x10) return 0;
for(i=0;i10) return 0;
}
for(i=0;i<n;i++)
{
sum+=power(x,n-i)*a[i];
}
printf("%llu",sum);
return 0;
}
int power(int x, int y)
{
int result = x,i;
if(y == 0) return 1;
if(x < 0 || y < 0) return 0;
for(i = 1; i < y; ++i)
result *= x;
return result;
}Solution
-
Recommend a uniform and more indented style. E.g.
-
Both
-
Else suggest for marginal range improvement:
-
BTW: should not it be:
-
Note: Stated objective says "It is not necessary to validate the inputs.". Still I like the testing you have done. Possible that the final reviewer (teacher?) may object to unneeded code. Could wrap in an
-
Faster way to do
-
See no reason for
Recommend a uniform and more indented style. E.g.
for(i=0;i<n;i++)
{
sum+=power(x,n-i)*a[i];
}-
Both
a[11] and if(... n>10) rely on the same value. Also to avoid undocumented magic numbers, consider the following (also the same for x and a[]):#define n_MAX (10)
#define n_MIN (0)
int a[n_MAX + 1];
if(nn_MAX) return 0;-
unsigned long long int sum serves little value in protecting against numbers larger than INT_MAX as power() is the most likely candidate to overflow. (Strictly speaking, it is not even known that LLONG_MAX is greater than INT_MAX.) So if you want a larger range, re-write power() as unsigned long long power(int x, int y), otherwise leaving sum as int is OK.Else suggest for marginal range improvement:
sum += (unsigned long long) power(x,n-i) * a[i];-
BTW: should not it be:
unsigned long long int sum = 0; //( 0 not 1)-
Note: Stated objective says "It is not necessary to validate the inputs.". Still I like the testing you have done. Possible that the final reviewer (teacher?) may object to unneeded code. Could wrap in an
#if DEBUG macro...-
Faster way to do
power()-
See no reason for
x
-
Do power() with unsigned rather than int as " function named power( ) that takes two positive integers x and y".
-
@Constructor comment leads to an excellent suggestion. Calculate sum with a loop that does the below. No need for a power function. Running sum can be unsigned long long and code gets great range. Its faster & simple.
// sum = (((a[n]*x + a[n-1])*x + a[n-2])*x + .... )*x + a[0];
// Something like
unsigned long long sum = 0;
for (int i=n; i>=0; i--) {
sum *= x;
sum += a[i];
}
-
Lastly, even the array a` is not needed:#include
int main() {
int n;
unsigned x, a;
unsigned long long sum = 0;
scanf("%d%u", &n, &x);
while (n-- >= 0) {
scanf("%u", &a);
sum *= x;
sum += a;
}
printf("%llu", sum);
return 0;
}Code Snippets
for(i=0;i<n;i++)
{
sum+=power(x,n-i)*a[i];
}#define n_MAX (10)
#define n_MIN (0)
int a[n_MAX + 1];
if(n<n_MIN || n>n_MAX) return 0;sum += (unsigned long long) power(x,n-i) * a[i];unsigned long long int sum = 0; //( 0 not 1)if (y < 0) return 0;Context
StackExchange Code Review Q#45488, answer score: 4
Revisions (0)
No revisions yet.