0% found this document useful (0 votes)
29 views7 pages

Assign 4

assign

Uploaded by

Lac Abh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views7 pages

Assign 4

assign

Uploaded by

Lac Abh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include<iostream>

using namespace std;

typedef struct poly

int coeff;

int exp;

}p;

p p1[10],p2[10],p3[30],p4[30];

int read(p p1[10]);

int add(p p1[10],p p2[10],int t1,int t2,p p3[30]);

void print(p pp[10], int term);

int mult(int t1,int t2,p p1[10],p p2[10],p p4[30]);

int evaluate(int x,int terms);

int main()

int t1,t2,t3,t4,x;

t1=read(p1);

print(p1,t1);

cout<<endl<<"\nEnter x= ";

cin>>x;
cout<<"\n Evaluated Result = "<<evaluate(x,t1);

t2=read(p2);

print(p2,t2);

t3=add(p1,p2,t1,t2,p3);

cout<<"\n\nAddition of Polynomial:\n";

print(p3,t3);

t4=mult(t1,t2,p1,p2,p4);

cout<<"\n\n Multiplication of Polynomial : \n";

print(p4,t4);

cout<<"Enter x= ";

cin>>x;

cout<<"\n Evaluated Result = "<<evaluate(x,t1);

return 0;

int read(p p[10])

int t1,i;

cout << "\n Total No. of terms= ";

cin >> t1;

cout<< "\n\n Read coeff and Expo in Descending order= ";


for(i=0;i<t1;i++)

cin >> p[i].coeff;

cin >> p[i].exp;

return (t1);

void print(p pp[10], int term)

int t1, i;

cout <<"\n Polynomial is= ";

for (i=0;i<term-1;i++)

cout <<pp[i].coeff <<"x^" << pp[i].exp<<" " << "+" <<" " ;

cout <<pp[i].coeff <<"x^" << pp[i].exp<<" " << "+" <<" " ;

int add(p p1[10],p p2[10],int t1,int t2,p p3[30])

int i=0,j=0,k=0;

int t3;

while(i<t1 && j<t2)

if(p1[i].exp==p2[j].exp)

{
p3[k].coeff=p1[i].coeff+p2[j].coeff;

p3[k].exp=p1[i].exp;

i++;j++;k++;

else if(p1[i].exp>p2[j].exp)

p3[k].coeff=p1[i].coeff;

p3[k].exp=p1[i].exp;

i++;k++;

else

p3[k].coeff=p2[j].coeff;

p3[k].exp=p2[j].exp;

j++;k++;

while(i<t1)

p3[k].coeff=p1[i].coeff;

p3[k].exp=p1[i].exp;

i++;k++;

}
while(j<t2)

p3[k].coeff=p2[i].coeff;

p3[k].exp=p2[i].exp;

j++;k++;

t3=k;

return (t3);

int mult(int t1,int t2,p p1[10],p p2[10],p p4[30])

int i,j,k,next,found;

int ex;

int co;

i=next=0;

while(i<t1)

j=0;

while(j<t2)

ex=p1[i].exp+p2[j].exp;

co=p1[i].coeff*p2[j].coeff;

k=0;

found=0;
while(k<next && !found)

if(p4[k].exp==ex)

found=1;

else

k++;

if(found)

co=p4[k].coeff+co;

else

p4[next].exp=ex;

p4[next].coeff=co;

next++;

j++;

i++;

return (next);

int evaluate(int x,int terms)

int sum=0,se=0;

int temp,po=x;
for(int i=0;i<terms;i++)

temp=p1[i].exp;

for(int j=temp;j>1;j--)

po=po*po;

if(temp==0)

sum+=p1[i].coeff;

else

sum+=p1[i].coeff * po;

po=x;

return(sum);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy