// polymult.cpp // // // This program reads an input n from the user. // Then, it either reads or randomly generates 2 polynomials, // each with n coefficients // It multiplies these 2 polynomials. // Finally, it prints out the result of the multiplication. // // // A polynomial with n coefficient is represented as // an array of n integers. // // // The program contains the following subroutines: // // void polyread( int *p, int n) // This function reads in n coefficients from the user for // polynomial p // // void polyrand( int *p, int n) // This function creates random coefficients for polynomial p, // which has n coefficients. // // void polyprint( int *p, int n) // This function prints the coefficients of the polynomial on the screen // starting with the coefficient of the monomial of degree 0. // // void polymult( int *p, int *q, int *r, int n) // This function multiplies polynomials p and q, each has n coefficients // and stores the result in r. // #include #include void polyread( int *p, int n); void polyrand( int *p, int n); void polyprint( int *p, int n); void polymult( int *p, int *q, int *r, int n); void main() { int *a,*b,*c,n; cout << "Enter N: "; cin >> n; a=new int[n]; b=new int[n]; if (n <= 10) { polyread(a,n); // read the first polynomial polyread(b,n); // read the second polynomial } else { polyrand(a,n); // random first polynomial polyrand(b,n); // random second polynomial } c=new int[2*n-1]; polymult(a,b,c,n); // multiplying the polynomials // printing out the result cout<< " The coefficients of the first polynomial are" << endl; polyprint(a,n); cout<< " The coefficients of the second polynomial are" << endl; polyprint(b,n); cout<< " The coefficients of the resulting polynomial are" << endl; polyprint(c,2*n-1); // freeing up space at the end delete [] c; delete [] b; delete [] a; } void polyread( int *p, int n) { cout << "Please enter the " << n << " coefficients, sep. by spaces\n"; for (int i = 0; i < n; i++) { cin >> p[i]; } } void polyrand( int *p, int n) { for( int i=0; i