C program function. Write a function: int readPolynomial( int coefficients[ ], i
ID: 3602989 • Letter: C
Question
C program function. Write a function: int readPolynomial( int coefficients[ ], int degree);
The caller will pass in a polynomial (array of coefficients) and the maximum degree allowed for that polynomial. Recall that an n-degree polynomial has n+1 coefficients. (So the second argument is not the size of the array - it's the maximum index that can be used with that array.) The initial values in the coeff array are unknown. This function will read a properly-formatted text polynomial from the standard input stream, and record the coefficients into the proper locations in the array. If the degree is too large, the function returns 1 and fills the array with zeroes. Otherwise, the function returns 0 to indicate success. Follow exactly the text representation described earlier in this specification. There may be one or more whitespace characters (space, tab, linefeed) before the polynomial begins, which must be ignored. You may assume the polynomial ends with a linefeed (in) lf desired, the ungetc function can be used to place any such character back onto the standard input stream (stdin). When creating the readPoly function, I used a series of scanf statements that read in an integer or a character, depending on what I was expecting.Explanation / Answer
#include < stdio.h >
#include < conio.h >
#define MAX 20
struct addpolynomial {
int exp, coef;
};
void main() {
struct addpolynomial p1[MAX], p2[MAX], p3[MAX];
int max1, max2, max3;
clrscr();
printf("nEnter first addpolynomial : ");
max1 = read_addpolynomial(p1);
printf("nEnter second addpolynomial : ");
max2 = read_addpolynomial(p2);
max3 = add_addpolynomial(p1, p2, p3, max1, max2);
printf("nFirst addpolynomial is ");
print_addpolynomial(p1, max1);
printf("nSecond addpolynomial is ");
print_addpolynomial(p2, max2);
printf("n The resultant addpolynomial after addition is");
print_addpolynomial(p3, max3);
}
int read_addpolynomial(struct addpolynomial p[]) {
int i, texp;
i = 0;
printf("nEnter exp ( use -1 to exit) : ");
scanf("%d", &texp);
while (texp != -1) {
p[i].exp = texp;
printf("nEnter coef : ");
scanf("%d", &p[i].coef);
i++;
printf("nEnter exp ( use -1 to exit) : ");
scanf("%d", &texp);
}
return (i);
}
int print_addpolynomial(struct addpolynomial p[], intMAX1) {
int i;
for (i = 0; i < max1; i++)
printf("%+dX%d ", p[i].coef, p[i].exp);
return;
}
int add_addpolynomial( p1, p2, p3, max1, max2)
struct addpolynomial p1[], p2[], p3[];
intMAX1, max2;
{
int i,j,k;
i = j = k = 0;
while ( i <max1 && j <max2)
{
if( p1[i].exp > p2[j].exp)
{
p3[k] = p1[i];
k++;
i++;
}
else
if( p1[i].exp < p2[j].exp)
{
p3[k] = p2[j];
k++;
j++;
}
else
{
p3[k].exp = p1[i].exp;
p3[k].coef = p1[i].coef + p2[j].coef;
i++;
j++;
k++;
}
}
while( i <max1 )
{
p3[k] = p1[i];
k++;
i++;
}
while( j <max2 )
{
p3[k] = p2[j];
k++;
j++;
}
return(k);
}
#include<math.h>
#include<stdio.h>
#include<conio.h>
#define MAX 17
typedef struct node
{
int coeff;
struct node *next;
}node;
node * init();
void read(node *h1);
void print(node *h1);
node * add(node *h1,node *h2);
node * multiply(node *h1,node *h2);
void main()
{
node *h1=NULL,*h2=NULL,*h3=NULL;
int option;
do
{
printf(“nn1 : create 1’st polynomial”);
printf(“n2 : create 2’nd polynomial”);
printf(“n3 : Add polynomials”);
printf(“n4 : Multiply polynomials”);
printf(“n5 : Quit”);
printf(“nEnter your choice :”);
scanf(“%d”,&option);
switch(option)
{
case 1:h1=init();read(h1);break;
case 2:h2=init();read(h2);break;
case 3:h3=add(h1,h2);
printf(“n1’st polynomial -> “);
print(h1);
printf(“n2’nd polynomial -> “);
print(h2);
printf(“n Sum = “);
print(h3);
break;
case 4:h3=multiply(h1,h2);
printf(“n1’st polynomial -> “);
print(h1);
printf(“n2’nd polynomial -> “);
print(h2);
printf(“n Product = “);
print(h3);
break;
}
}while(option!=5);
}
void read(node *h)
{
int n,i,j,power,coeff;
node *p;
p=init();
printf(“n Enter number of terms :”);
scanf(“%d”,&n);
for (i=0;i<n;i++)
{ printf(“nenter a term(power coeff.)”);
scanf(“%d%d”,&power,&coeff);
for(p=h,j=0;j<power;j++)
p=p->next;
p->coeff=coeff;
}
}
void print(node *p)
{
int i;
for(i=0;p!=NULL;i++,p=p->next)
if(p->coeff!=0)
printf(“%dX^%d “,p->coeff,i);
}
node * add(node *h1, node *h2)
{
node *h3,*p;
h3=init();
p=h3;
while(h1!=NULL)
{
h3->coeff=h1->coeff+h2->coeff;
h1=h1->next;
h2=h2->next;
h3=h3->next;
}
return(p);
}
node * multiply(node *h1, node *h2)
{
node *h3,*p,*q,*r;
int i,j,k,coeff,power;
h3=init();
for(p=h1,i=0;p!=NULL;p=p->next,i++)
for(q=h2,j=0;q!=NULL;q=q->next,j++)
{
coeff=p->coeff * q->coeff;
power=i+j;
for(r=h3,k=0;k<power;k++)
r=r->next;
r->coeff=r->coeff+coeff;
}
return(h3);
}
node * init()
{
int i;
node *h=NULL,*p;
for(i=0;i<MAX;i++)
{
p=(node*)malloc(sizeof(node));
p->next=h;
p->coeff=0;
h=p;
}
return(h);
}
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAXSIZE 10
voidmain()
{
int a[MAXSIZE];
int i, N,power;
float x, polySum;
clrscr();
printf("Enter the order of the polynomial ");
scanf("%d", &N);
printf("Enter the value of x ");
scanf("%f", &x);
printf("Enter %d coefficients ",N+1);
for (i=0;i <= N;i++)
{
scanf("%d",&a[i]);
}
polySum = a[0];
for (i=1;i<= N;i++)
{
polySum = polySum * x + a[i];
}
power = N;
printf("Given polynomial is: ");
for (i=0;i<= N;i++)
{
if (power < 0)
{
break;
}
if (a[i] > 0)
printf(" + ");
else if (a[i] < 0)
printf(" - ");
else
printf (" ");
printf("%dx^%d ",abs(a[i]),power--);
}
printf(" Sum of the polynomial = %6.2f ",polySum);
}
#include "stdio.h"
#include "conio.h"
void main()
{
float a[100],sum=0,x;
int n,i;
clrscr();
printf("Enter the degree of the polynomial:");
scanf("%d",&n);
printf("Enter the coefficients into the array:");
for(i=n;i>=0;i--)
{
scanf("%f",&a[i]);
}
printf("Enter the value of x:");
scanf("%f",&x);
for(i=n;i>0;i--)
{
sum=(sum+a[i])*x;
}
sum=sum+a[0];
printf(" Value of the polynomial is =%f",sum);
getch();
}
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
void main()
{
int array[MAXSIZE];
int i, num, power;
float x, polySum;
printf("Enter the order of the polynomial ");
scanf("%d", &num);
printf("Enter the value of x ");
scanf("%f", &x);
printf("Enter %d coefficients ", num + 1);
for (i = 0; i <= num; i++)
{
scanf("%d", &array[i]);
}
polySum = array[0];
for (i = 1; i <= num; i++)
{
polySum = polySum * x + array[i];
}
power = num;
printf("Given polynomial is: ");
for (i = 0; i <= num; i++)
{
if (power < 0)
{
break;
}
if (array[i] > 0)
printf(" + ");
else if (array[i] < 0)
printf(" - ");
else
printf(" ");
printf("%dx^%d ", abs(array[i]), power--);
}
printf(" Sum of the polynomial = %6.2f ", polySum);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.