answersLogoWhite

0


Best Answer

#include<stdio.h>

#include<stdlib.h>

void display(float **,int);

float** add(float **,float **,int,int,int);

int main()

{

float **p1,**p2,**p3,**p4;

int i,j,n1,n2,k=0,x;

printf("Enter no of terms of a pollynomial:\n");

scanf("%d",&n1);

printf("Enter no of terms of another pollynomial:\n");

scanf("%d",&n2);

p1=(float **) malloc(n1*sizeof(float *));

p2=(float **) malloc(n2*sizeof(float *));

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

p1[i]=(float *) malloc(2*sizeof(float));

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

p2[i]=(float *) malloc(2*sizeof(float));

printf("Enter the first pollynomial:\n");

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

{

printf("\nEnter value and exponent:");

scanf("%f %f",&p1[i][0],&p1[i][1]);

}

printf("Enter the second pollynomial:\n");

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

{

printf("\nEnter value and exponent:");

scanf("%f %f",&p2[i][0],&p2[i][1]);

}

printf("\nFirst pollynomial:\n");

display(p1,n1);

printf("\nSecond pollynomial:\n");

display(p2,n2);

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

for(j=0;j<n2;j++)

if(p1[i][1]==p2[j][1])

k++;

x=n1+n2-k;

p3=add(p1,p2,n1,n2,x);

printf("\nAdded polynomial:\n");

display(p3,x);

return 0;

}

void display(float **p,int n)

{

int i;

printf("%fx^%d",p[0][0],(int)p[0][1]);

for(i=1;i<n;i++)

printf("+%fx^%d",p[i][0],(int)p[i][1]);

}

float** add(float **p1,float **p2,int n1,int n2,int n)

{

int i,j,k;

float **p3;

p3=(float **)malloc(n*sizeof(float*));

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

p3[i]=(float *)malloc(2*sizeof(float));

i=0;

j=0;

k=0;

while(i<n1 && j<n2)

{

if(p1[i][1]==p2[j][1])

{

p3[k][0]=p1[i][0]+p2[j][0];

p3[k][1]=p1[i][1];

k++;

i++;

j++;

}

else if(p1[i][1]<p2[j][1])

{

p3[k][0]=p1[i][0];

p3[k][1]=p1[i][1];

k++;

i++;

}

else

{

p3[k][0]=p2[j][0];

p3[k][1]=p2[j][1];

k++;

j++;

}

}

while(i<n1)

{

p3[k][0]=p1[i][0];

p3[k][1]=p1[i][1];

k++;

i++;

}

while(j<n2)

{

p3[k][0]=p2[j][0];

p3[k][1]=p2[j][1];

k++;

j++;

}

return p3;

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Adding polynomials using array in C programming Language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What does nial mean?

It is a high-level programming language: Nested Interactive Array Language.


What is the definition of array length in the Java programming language?

When programming in Java language, you will need to use container objects to hold your specific values and these are called Arrays. The amount of values held in these containers is called the Array Length. This is generated once an Array is created and the length becomes locked.


Is two dimensional array is multi dimensional array in c language?

Yes because multi means more than one in programming, just like it does in English. No isn't that a surprise. Have you read any good programming text books lately.


What symbol or character determines end of string?

In C programming language, a string is an array of characters which is always terminated by a NULL character: '\0'


Is it necessary to give the size of array?

Depends on the language. For C, no you don't. You can type blank brackets (int Arr[]) when declaring the array, or you can just use a pointer (int* Arr). Both will allow you to use the variable as an array without having to declare the specific size. Hope this answers your question. In Java, an array is an object, and one which is dynamically allocated space. The default constructor does not require a size be specified.


Which one is used in array programming SISD or MISD?

MISD is used in systolic array.


How do you set the null value in array?

Depends on the programming language, some languages may have already initialize an array with null (or the default value of the type), some of them require explicitly assignments by stepping through each element of that array, and assigning them with null. (imperative languages)


When was the first python array written?

Python is a high level computer programming language. It allows programmers to use clearer code with fewer lines that other languages. A Python array is a method to store data. Whilst it is impossible to precisely date when the first python array was written, development work started on the language in December 1989.


Why is c string defined as an array?

Every programming language treats strings as arrays. A C string is defined as being a null-terminated array of characters. A C string that does not have a null-terminator is just an array of character values, but without a null-terminator the onus is upon the programmer to keep track of the array's length.


What is a bitset?

A bitset is an array of bits in computer programming.


Give the syntax to declare two dimensional array using array of pointers?

It is not possible to declare a two-dimensional array using an array of pointers in any programming language, but many programming languages support declarations of N-dimensional arrays of pointers.The exact syntax varies with the programming language, and requires support for N-dimensional arrays and pointers. In C, the following declares an array of pointer variables, each implemented as pointer to the generic type "void":void* array_1D[10];The type of the expression array_1D is "void * const."The following example expands on the previous one by declaring a two-dimensional array of "void" pointers:void* array_2D[10][20];The type of the expression array_2D is "void ** const."The last example declares a 3-dimensional array of "void" pointers, which can be seen as a 2-dimensional array of arrays of pointers:void* array_3D[10][20][30];


What is an ordered list of data structure using c plus plus?

An ordered list of data in any programming language is simply a sorted array or list. In C++ this can either mean a sorted array, vector, list or forward list.