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...
c++ Create a program where you can add binary while they're still in binary form and...
c++ Create a program where you can add binary while they're still in binary form and convert them to decimal, adds them, then converts then back to binary. (without built in conversions). Convert 34 to Binary Convert 22 to Binary Convert 1001 to Decimal Convert 111011 to Decimal Add 11 and 1110 in Binary Return the Answer in Binary Subtract 111 from 1010 in Binary, then Convert the Answer to Decimal
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...
write a program in C Write a function that is passed an array of characters containing...
write a program in C Write a function that is passed an array of characters containing letter grades of A, B, C, D, and F, and returns the total number of occurrences of each letter grade. Your function should accept both lower and upper case grades, for example, both 'b' and 'B' should be bucketed into your running total for B grades. Any grade that is invalid should be bucketed as a grade of 'I' for Incomplete. You must use...
Write a C program with a struct that contains a string of 12 characters and two...
Write a C program with a struct that contains a string of 12 characters and two integers: number1 and number2. Read all 3 values in from the keyboard using scanf. Print the string and the sum of the two integers using the fully qualified struct names.
Write a program that can check whether or not some string is a valid VUnet ID....
Write a program that can check whether or not some string is a valid VUnet ID. A valid VUnet ID consists of 3 letters followed by 3 digits. The letters in the VUnet ID can be written as lower case letters as well as upper case letters. Examples of valid VUnet IDs are 'ABC123', 'def456', and 'Ghi789'. An example of an invalid VUnet ID is 'ab123c'. Define functions to structure your program. That is what this assignment is all about!...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT