Monday 15 February 2016

NPTEL(WEEK-3.4)

Programming Assignment 3.4: Evaluating a Polynomial


You are given a polynomial of degree n. The polynomial is of the form P(x) = a
nxn + an-1xn-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 two positive integers x & y and returns xy. If y is 0, the function returns 1.


The prototype of this function is

int power(int x, int y);
You do not have to write the program for power ( ) function. This function is automatically added at the end of the code segment that you write.
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 <=1
0




#include<stdio.h> /* function to calculate power x^y */ int power(int x, int y){ int pow=1; while (y!=0){ pow*=x; y--; } return pow; }


int pol(int ,int ,int[] ,int ); int main(){ int i,n,x,a[15]; scanf("%d%d",&n,&x); for(i=0;i<n+1;i++){ scanf("%d",&a[i]); } printf("%d",pol(x,n,a,0)); return 0; } int pol(int x,int n,int a[],int i){ if(n==0) return a[i]; else return (a[i]*power(x,n))+pol(x,n-1,a,++i); }

No comments:

Post a Comment

How to install google-chrome in redhat without redhat subscription

Install google-chrome in redhat  Download the .rpm file of chrome https://www.google.com/chrome/thank-you.html?installdataindex=empty&st...