answersLogoWhite

0


Want this question answered?

Be notified when an answer is posted

Add your answer:

Earn +20 pts
Q: What is the value of p1 if int x10y10int p1?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write a program to multiply two polynomials using an array?

#include<stdio.h> #include<malloc.h> int* getpoly(int); void showpoly(int *,int); int* addpoly(int *,int,int *,int); int* mulpoly(int *,int,int *,int); int main(void) { int *p1,*p2,*p3,d1,d2,d3; /*get poly*/ printf("\nEnter the degree of the 1st polynomial:"); scanf("%d",&d1); p1=getpoly(d1); printf("\nEnter the degree of the 2nd polynomial:"); scanf("%d",&d2); p2=getpoly(d2); printf("Polynomials entered are\n\n"); showpoly(p1,d1); printf("and\n\n"); showpoly(p2,d2); /*compute the sum*/ d3=(d1>=d2?d1:d2); p3=addpoly(p1,d1,p2,d2); printf("Sum of the polynomials is:\n"); showpoly(p3,d3); /*compute product*/ p3=mulpoly(p1,d1,p2,d2); printf("Product of the polynomials is:\n"); showpoly(p3,d1+d2); } int* getpoly(int degree) { int i,*p; p=malloc((1+degree)*sizeof(int)); for(i=0;i<=degree;i++) { printf("\nEnter coefficient of x^%d:",i); scanf("%d",(p+i)); } return(p); } void showpoly(int *p,int degree) { int i; for(i=0;i<=degree;i++) printf("%dx^%d + ",*(p+i),i); printf("\b\b\b "); printf("\n"); } int* addpoly(int *p1,int d1,int *p2,int d2) { int i,degree,*p; degree=(d1>=d2?d1:d2); p=malloc((1+degree)*sizeof(int)); for (i=0;i<=degree;i++) if((i>d1) && (i<=d2)) *(p+i)=*(p2+i); else if((i>d2) && (i<=d1)) *(p+i)=*(p1+i); else *(p+i)=*(p1+i)+*(p2+i); return(p); } int* mulpoly(int *p1,int d1,int*p2,int d2)/* this is the function of concern*/ { int i,j,*p; p=malloc((1+d1+d2)*sizeof(int)); for(i=0;i<=d1;i++) for(j=0;j<=d2;j++) p[i+j]+=p1[i]*p2[j]; return(p); }


How many constructors can c have?

A class can have any number of constructors, as far as they are having different parameters or different number of parameters. For example, a class A can have following constructors & even more: A() -the default constructor A(A objectA) -the copy constructor A(int p) A(int p1, int p2) A(int[] p1, float p2) A(double p1, double p2, int p3) A(A objA, int[] p) A(B objB)


What is the C code for preemptive priority scheduling?

#include<stdio.h> #include<conio.h> int main() { char p[10][5],temp[5]; int i,j,pt[10],wt[10],totwt=0,pr[10],temp1,n; float avgwt; clrscr(); printf("enter no of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter process%d name:",i+1); scanf("%s",&p[i]); printf("enter process time:"); scanf("%d",&pt[i]); printf("enter priority:"); scanf("%d",&pr[i]); } for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(pr[i]>pr[j]) { temp1=pr[i]; pr[i]=pr[j]; pr[j]=temp1; temp1=pt[i]; pt[i]=pt[j]; pt[j]=temp1; strcpy(temp,p[i]); strcpy(p[i],p[j]); strcpy(p[j],temp); } } } wt[0]=0; for(i=1;i<n;i++) { wt[i]=wt[i-1]+et[i-1]; totwt=totwt+wt[i]; } avgwt=(float)totwt/n; printf("p_name\t p_time\t priority\t w_time\n"); for(i=0;i<n;i++) { printf(" %s\t %d\t %d\t %d\n" ,p[i],pt[i],pr[i],wt[i]); } printf("total waiting time=%d\n avg waiting time=%f",tot,avg); getch(); }


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; }


C program for polynomial addition?

POLYNOMIAL ADDITION#include#includetypedef struct poly{int coeff;int expo;}p;p p1[10],p2[10],p3[10];void main(){int t1,t2,t3,k;int read(p p1[10]);int add(p p1[10],p p2[10],int t1,int t2,p p3[10]);void print(p p2[10],int t2);void printo(p pp[10],int t2);clrscr();t1=read(p1);print(p1,t1);t2=read(p2);print(p2,t2);t3=add(p1,p2,t1,t2,p3);printo(p3,t3);getch();}int read(p p[10]){int t1,i;printf("\n Enter the total no of terms");scanf("%d",&t1);printf("\n Enter the coeff and expo in descending order");for(i=0;i

Related questions

How do you Add 2 numbers using pointer and array in C?

#include<stdio.h> #include<conio.h> void main() { clrscr(); int i; int a[4]={1,2,3,4}; int *p1,*p2,*p3; for(i=0;i<4;i++) { p1=&a[0]; p2=&a[2]; p3=*p2-*p1; printf("\n value of p3%d",p3); } getch(); }


How do you print twin primenumbers in c program?

void PrintTwinPrimes (int p1, int p2) { printf ("%d and %d are twin-primes\n", p1, p2); }


Write a program to multiply two polynomials using an array?

#include<stdio.h> #include<malloc.h> int* getpoly(int); void showpoly(int *,int); int* addpoly(int *,int,int *,int); int* mulpoly(int *,int,int *,int); int main(void) { int *p1,*p2,*p3,d1,d2,d3; /*get poly*/ printf("\nEnter the degree of the 1st polynomial:"); scanf("%d",&d1); p1=getpoly(d1); printf("\nEnter the degree of the 2nd polynomial:"); scanf("%d",&d2); p2=getpoly(d2); printf("Polynomials entered are\n\n"); showpoly(p1,d1); printf("and\n\n"); showpoly(p2,d2); /*compute the sum*/ d3=(d1>=d2?d1:d2); p3=addpoly(p1,d1,p2,d2); printf("Sum of the polynomials is:\n"); showpoly(p3,d3); /*compute product*/ p3=mulpoly(p1,d1,p2,d2); printf("Product of the polynomials is:\n"); showpoly(p3,d1+d2); } int* getpoly(int degree) { int i,*p; p=malloc((1+degree)*sizeof(int)); for(i=0;i<=degree;i++) { printf("\nEnter coefficient of x^%d:",i); scanf("%d",(p+i)); } return(p); } void showpoly(int *p,int degree) { int i; for(i=0;i<=degree;i++) printf("%dx^%d + ",*(p+i),i); printf("\b\b\b "); printf("\n"); } int* addpoly(int *p1,int d1,int *p2,int d2) { int i,degree,*p; degree=(d1>=d2?d1:d2); p=malloc((1+degree)*sizeof(int)); for (i=0;i<=degree;i++) if((i>d1) && (i<=d2)) *(p+i)=*(p2+i); else if((i>d2) && (i<=d1)) *(p+i)=*(p1+i); else *(p+i)=*(p1+i)+*(p2+i); return(p); } int* mulpoly(int *p1,int d1,int*p2,int d2)/* this is the function of concern*/ { int i,j,*p; p=malloc((1+d1+d2)*sizeof(int)); for(i=0;i<=d1;i++) for(j=0;j<=d2;j++) p[i+j]+=p1[i]*p2[j]; return(p); }


How many constructors can c have?

A class can have any number of constructors, as far as they are having different parameters or different number of parameters. For example, a class A can have following constructors & even more: A() -the default constructor A(A objectA) -the copy constructor A(int p) A(int p1, int p2) A(int[] p1, float p2) A(double p1, double p2, int p3) A(A objA, int[] p) A(B objB)


What is the C code for preemptive priority scheduling?

#include<stdio.h> #include<conio.h> int main() { char p[10][5],temp[5]; int i,j,pt[10],wt[10],totwt=0,pr[10],temp1,n; float avgwt; clrscr(); printf("enter no of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter process%d name:",i+1); scanf("%s",&p[i]); printf("enter process time:"); scanf("%d",&pt[i]); printf("enter priority:"); scanf("%d",&pr[i]); } for(i=0;i<n-1;i++) { for(j=i+1;j<n;j++) { if(pr[i]>pr[j]) { temp1=pr[i]; pr[i]=pr[j]; pr[j]=temp1; temp1=pt[i]; pt[i]=pt[j]; pt[j]=temp1; strcpy(temp,p[i]); strcpy(p[i],p[j]); strcpy(p[j],temp); } } } wt[0]=0; for(i=1;i<n;i++) { wt[i]=wt[i-1]+et[i-1]; totwt=totwt+wt[i]; } avgwt=(float)totwt/n; printf("p_name\t p_time\t priority\t w_time\n"); for(i=0;i<n;i++) { printf(" %s\t %d\t %d\t %d\n" ,p[i],pt[i],pr[i],wt[i]); } printf("total waiting time=%d\n avg waiting time=%f",tot,avg); getch(); }


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; }


What is a shallow copy in c plus plus?

Shallow copies occur whenever we copy "naked" pointers.int* p1 = new int {42};int* p2 = p1; // shallow copyWe call this a shallow copy because although p1 and p2 are independent pointer variables, they both refer to the same memory. If we change the value stored in that memory, we affect both pointers:assert (*p2 == 42);*p1 = 0;assert (*p2 == 42); // error!Side-effects such as this are completely at odds with how a "normal" copy behaves:int x = 42;int y = x;assert (y == 42);x = 0;assert (y == 42); // okHere, changing the value of one variable has no effect upon the other because they are completely independent variables.To mimic this behaviour with pointer variables we must perform a deep copy, copying the memory being referred to:int* p1 = new int {42}; int* p2 = new int {*p1}; // deep copyassert (*p2 == 42);*p1 = 0;assert (*p2 == 42); // okA shallow copy is (usually) cheaper than a deep copy, thus it is not unusual for class designers to postpone an expensive deep copy until it becomes absolutely necessary to do so (also known as a lazy copy). However, since C++11, the inclusion of move semantics and standard library smart pointers and resource handles has greatly reduced the need for shallow copying.


What if you feed an int value and an unsigned int value?

Feed? What do you mean by that


C program for polynomial addition?

POLYNOMIAL ADDITION#include#includetypedef struct poly{int coeff;int expo;}p;p p1[10],p2[10],p3[10];void main(){int t1,t2,t3,k;int read(p p1[10]);int add(p p1[10],p p2[10],int t1,int t2,p p3[10]);void print(p p2[10],int t2);void printo(p pp[10],int t2);clrscr();t1=read(p1);print(p1,t1);t2=read(p2);print(p2,t2);t3=add(p1,p2,t1,t2,p3);printo(p3,t3);getch();}int read(p p[10]){int t1,i;printf("\n Enter the total no of terms");scanf("%d",&t1);printf("\n Enter the coeff and expo in descending order");for(i=0;i


Swap the value of two variables using call by value by c plus plus?

Lets start simple by swapping two int variables in a function call. You must use a & to pass the variables by reference so that when you edit their values the original variables get changed as well. void swapVariables(int &var1, int &var2) { int temp = var1; var1 = var2; var2 = temp; } You could change this function to use any variable type that you want or you could use a template function instead so that the same function could be used with any variable type. template <class T> void swapVariables(T &var1, T &var2) { T temp = var1; var1 = var2; var2 = temp; } Another interesting thing you can do when swapping two variables is use an xor function to swap the two variables without using a temp variable. void swapVariables(int &var1, int &var2) { var1 = var1^var2; var2 = var1^var2; var1 = var1^var2; } Which can also be simplified to: void swapVariables(int &var1, int &var2) { var1 ^= var2 ^= var1^= var2; }


What value is return by int main method?

1. A method declared as "int" returns an int value. 2. The main() method in Java is not declared as "int", but as "void", meaning it returns no value.


Polynomial multiplication program using data structures in c?

#include <stdio.h> #include <conio.h> #define MAX 10 struct term { int coeff ; int exp ; } ; struct poly { struct term t [10] ; int noofterms ; } ; void initpoly ( struct poly *) ; void polyappend ( struct poly *, int, int ) ; struct poly polyadd ( struct poly, struct poly ) ; struct poly polymul ( struct poly, struct poly ) ; void display ( struct poly ) ; void main( ) { struct poly p1, p2, p3 ; clrscr( ) ; initpoly ( &p1 ) ; initpoly ( &p2 ) ; initpoly ( &p3 ) ; polyappend ( &p1, 1, 4 ) ; polyappend ( &p1, 2, 3 ) ; polyappend ( &p1, 2, 2 ) ; polyappend ( &p1, 2, 1 ) ; polyappend ( &p2, 2, 3 ) ; polyappend ( &p2, 3, 2 ) ; polyappend ( &p2, 4, 1 ) ; p3 = polymul ( p1, p2 ) ; printf ( "\nFirst polynomial:\n" ) ; display ( p1 ) ; printf ( "\n\nSecond polynomial:\n" ) ; display ( p2 ) ; printf ( "\n\nResultant polynomial:\n" ) ; display ( p3 ) ; getch( ) ; } /* initializes elements of struct poly */ void initpoly ( struct poly *p ) { int i ; p -> noofterms = 0 ; for ( i = 0 ; i < MAX ; i++ ) { p -> t[i].coeff = 0 ; p -> t[i].exp = 0 ; } } /* adds the term of polynomial to the array t */ void polyappend ( struct poly *p, int c, int e ) { p -> t[p -> noofterms].coeff = c ; p -> t[p -> noofterms].exp = e ; ( p -> noofterms ) ++ ; } /* displays the polynomial equation */ void display ( struct poly p ) { int flag = 0, i ; for ( i = 0 ; i < p.noofterms ; i++ ) { if ( p.t[i].exp != 0 ) printf ( "%d x^%d + ", p.t[i].coeff, p.t[i].exp ) ; else { printf ( "%d", p.t[i].coeff ) ; flag = 1 ; } } if ( !flag ) printf ( "\b\b " ) ; } /* adds two polynomials p1 and p2 */ struct poly polyadd ( struct poly p1, struct poly p2 ) { int i, j, c ; struct poly p3 ; initpoly ( &p3 ) ; if ( p1.noofterms > p2.noofterms ) c = p1.noofterms ; else c = p2.noofterms ; for ( i = 0, j = 0 ; i <= c ; p3.noofterms++ ) { if ( p1.t[i].coeff p2.t[j].exp ) { p3.t[p3.noofterms].coeff = p1.t[i].coeff + p2.t[j].coeff ; p3.t[p3.noofterms].exp = p1.t[i].exp ; i++ ; j++ ; } else { p3.t[p3.noofterms].coeff = p1.t[i].coeff ; p3.t[p3.noofterms].exp = p1.t[i].exp ; i++ ; } } else { p3.t[p3.noofterms].coeff = p2.t[j].coeff ; p3.t[p3.noofterms].exp = p2.t[j].exp ; j++ ; } } return p3 ; } /* multiplies two polynomials p1 and p2 */ struct poly polymul ( struct poly p1, struct poly p2 ) { int coeff, exp ; struct poly temp, p3 ; initpoly ( &temp ) ; initpoly ( &p3 ) ; if ( p1.noofterms != 0 && p2.noofterms != 0 ) { int i ; for ( i = 0 ; i < p1.noofterms ; i++ ) { int j ; struct poly p ; initpoly ( &p ) ; for ( j = 0 ; j < p2.noofterms ; j++ ) { coeff = p1.t[i].coeff * p2.t[j].coeff ; exp = p1.t[i].exp + p2.t[j].exp ; polyappend ( &p, coeff, exp ) ; } if ( i != 0 ) { p3 = polyadd ( temp, p ) ; temp = p3 ; } else temp = p ; } } return p3 ; }