Question

In: Computer Science

C++ program to accept characters as a polynomial and then output whether they're valid or not....

C++ program to accept characters as a polynomial and then output whether they're valid or not. Types of polynomials that don't work: n^5n, n^9.7, 8n, n^7-7*n

Solutions

Expert Solution

#include <iostream>

using namespace std;

// A[] represents coefficients of first polynomial

// B[] represents coefficients of second polynomial

// m and n are sizes of A[] and B[] respectively

int *multiply(int A[], int B[], int m, int n)

{

   int *prod = new int[m+n-1];

   // Initialize the porduct polynomial

   for (int i = 0; i<m+n-1; i++)

     prod[i] = 0;

   // Multiply two polynomials term by term

   // Take ever term of first polynomial

   for (int i=0; i<m; i++)

   {

     // Multiply the current term of first polynomial

     // with every term of second polynomial.

     for (int j=0; j<n; j++)

         prod[i+j] += A[i]*B[j];

   }

   return prod;

}

// A utility function to print a polynomial

void printPoly(int poly[], int n)

{

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

    {

       cout << poly[i];

       if (i != 0)

        cout << "x^" << i ;

       if (i != n-1)

       cout << " + ";

    }

}

// Driver program to test above functions

int main()

{

    // The following array represents polynomial 5 + 10x^2 + 6x^3

    int A[] = {5, 0, 10, 6};

    // The following array represents polynomial 1 + 2x + 4x^2

    int B[] = {1, 2, 4};

    int m = sizeof(A)/sizeof(A[0]);

    int n = sizeof(B)/sizeof(B[0]);

    cout << "First polynomial is n";

    printPoly(A, m);

    cout << "nSecond polynomial is n";

    printPoly(B, n);

    int *prod = multiply(A, B, m, n);

    cout << "nProduct polynomial is n";

    printPoly(prod, m+n-1);

    return 0;

}

the given output is in vaild types of polynomials .


Related Solutions

An issue that we have been constantly facing is determining whether valid characters exist in the...
An issue that we have been constantly facing is determining whether valid characters exist in the command line arguments for the laboratory tests. Many solutions rely on stepping through the argument list determining whether the current character is valid via a series of 'if' statements. Whilst this approach is certainly effective it is often prone to error. Consider an array of suitable characters for an application as follows: const char validCharacters[] = ".,-+eE0123456789"; Utilising nested 'for' loops, the characters in...
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
Design and implement a C++ program read in a whole line of characters as the input...
Design and implement a C++ program read in a whole line of characters as the input string; count and display how many times how frequently (among the letters) each (case insensitive) letter appears in the above mentioned input string; Sample program execution: An example of executing such a program is shown below. Note that the user input is in italic font. Please enter a line of characters: This is a really long line of characters! There are 41 characters in...
Create a C++ program that will accept any number of grades for an exam. The grades...
Create a C++ program that will accept any number of grades for an exam. The grades will be input as 4 for an A, 3 for a B, 2 for a C, 1 for a D, and 0 for an F. After all grades have been entered, allow the user to enter -1 to exit. Output the number of grades in each category. Using arrays.
Make a program for LAGRANGE INTERPOLATION METHOD using C++ program and can be evaluated both polynomial...
Make a program for LAGRANGE INTERPOLATION METHOD using C++ program and can be evaluated both polynomial and Transcendental Functions.
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions with arguments. Create programs with small functions where main goes to a series of functions where the real work takes place. Don’t use global variables and don’t use break within a loop (unless working with a switch statement). Functions can’t have more than 30 statements of code, not including comments, blank lines, or variable definitions. Don’t use a return in the middle of the...
Please write program in C++ format: Write a program to accept five negative numbers from the...
Please write program in C++ format: Write a program to accept five negative numbers from the user. (1) Find the average of the five numbers and display the answer to the standard output. Keep the answer two decimal points - 5 points (2) Output the numbers in ascending order and display the answer to the standard output. - 5 points
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT