#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;
}
Chat with our AI personalities
In C programming language, a string is an array of characters which is always terminated by a NULL character: '\0'
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.
MISD is used in systolic 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)
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.