Adding polynomials using array in C programming Language?
#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;
}
How do you reverse a single linked list in recursive method?
Reversing a singly-linked list is (surprisingly) simple and can be done non-recursively. The following function accepts a reference to the current head of the list and returns the new head after reversing the list.
node* reverse (node* head) {
if (!head) return head;
node *lead, *temp;
lead = head;
while (lead->next) {
temp = lead->next;
lead->next = temp->next;
temp->next = head;
head = temp;
}
return head;
}
Doing the same thing recursively is a bit more tricky but it can be done as follows.
node* reverse (node* head, node* prev=NULL) {
node* temp;
if (!head) {
return head;
} else if (!head->next) {
head->next = prev;
return head;
} else {
temp = head->next;
head->next = prev;
return reverse (temp, head);
}
}
What function will read a specified number of elements from a file in c?
fread() function will read a specified number of elements from a file .
160 and 192.
How do you remove duplicate rows from a matrix using C language?
Sort the rows; the duplicates will then be grouped together. Scan from the top row; if the next row is a duplicate of the current row, remove it. If the next row is not a duplicate, make it the current row. Repeat until there is no next row. When complete, all the duplicates will have been removed.
Find an example or a recursive procedure and represent it as an iterative procedure?
int recursiveNFactorial (int n) {
if (n < 2) return 1;
if (n == 2) return n;
else return n * recursiveNFactorial (n - 1);
}
int iterativeNFactorial (int n) {
int result = 2;
if (n < 2) return 1;
while (n > 2) result *= n--;
return result;
}
For an if then else loop how do you include more than one condition?
You can combine more than one condition with AND or with OR. How this is written depends on the particular programming language.
You can also have several cases; example in Java:
if (condition1)
{
command1;
}
else if (condition2)
{
command2;
{
else
{
command3;
}
Follow the backslash with another backslash:
System.out.println("\\ " \");
will display \ " \ on the screen.
Number of all possible binary trees with 2 nodes is?
Two:
1. root and left child
2. root and right child
In c plus plus what are the three ways that classes can relate to each other?
Class wrappers (embedded objects), inheritance (derived objects) and friend classes.
What is automatic variable and what is its use?
It is a local variable known only to the function in which it is declared. Auto is the default storage class.
0 3 5
A logical statement is one that will return a boolean or a logical "True" or "False" output. It is used in cases where conditions need to be executed.
For ex: lets say you write a system that checks the age of the visitors to a bar, the system should only allow people who are over 18 yrs of age. So the logical condition will be like below:
if(age > 18) then "Let the Customer Enter"
else "The customer is a minor, send them back to stay out of trouble"
What are the 5 Latin languages?
There are 47 Romance Languages, according to Ethnologue. The most common are:
Romanian, Portuguese, Spanish, French, Italian, Catalán, and an appreciable segment of English.
For a complete list, see related links.
How do you find most occurring digit in a number?
Count them unless the number has a recurring ending.
What is function of GOSUB statement in subrouteen?
in BASIC, GOSUB and the RETURN statement allows the use of subrouteens.
Would majoring in International Business and minoring in Computer Science be a good idea?
All the knowledge can get are good. However, whats more important your goals, you have to know that some of the colleges offer both under the same degree plan. get more for your money.
What is the difference between reference variable and painter variable?
Reference Variable
Pointer Variable
Why are the indents necessary after the if and else statement?
Indents are not necessary for simple if statements:
if (true) /* do something */ ;
else /* do something */;
However, when a statement is a compound statement, it's best to place the statement body on a separate line and indent it:
if (true) {
/* do something */ ; /* do something */ ; /* do something */ ;
} else {
/* do something */;
/* do something */;
}
The indents help the reader; separating the controlling expressions from the compound statements and thus exposing the structure of the if statement itself.
C compiler for 64bit computer?
Platform-dependent (Windows, Linux, AIX, MacOs etx), but gcc seems to be a safe bet.