c++ code for polynomial addition
C++
// Simple C++ program to add two polynomials
#include <iostream>
using namespace std;
// A utility function to return maximum of two integers
int max(int m, int n) { return (m > n)? m: n; }
// A[] represents coefficients of first polynomial
// B[] represents coefficients of second polynomial
// m and n are sizes of A[] and B[] respectively
int *add(int A[], int B[], int m, int n)
{
int size = max(m, n);
int *sum = new int[size];
// Initialize the porduct polynomial
for (int i = 0; i<m; i++)
sum[i] = A[i];
// Take ever term of first polynomial
for (int i=0; i<n; i++)
sum[i] += B[i];
return sum;
}
// 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 *sum = add(A, B, m, n);
int size = max(m, n);
cout << "\nsum polynomial is \n";
printPoly(sum, size);
return 0;
}
Also in C++:
- Title
- c++ modulo make it give only positive numbers
- Category
- C++
- Title
- static variable in c++
- Category
- C++
- Title
- how to round to nearest whole number unity
- Category
- C++
- Title
- c++ convert int to double
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- counting valleys hackerrank solution in c++
- Category
- C++
- Title
- leveling system c++
- Category
- C++
- Title
- max heap c++ stl;
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- function template
- Category
- C++
- Title
- c++ movment
- Category
- C++
- Title
- c++ server service ros
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- c++ remove item from list
- Category
- C++
- Title
- c++ public inheritance not getting protected
- Category
- C++
- Title
- how read a shader from another file c++
- Category
- C++
- Title
- is TLE means my code is correct but taking more time to computr
- Category
- C++
- Title
- differentialble programming
- Category
- C++
- Title
- c++ char to int
- Category
- C++
- Title
- gfg left view of tree
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- int to float c++
- Category
- C++
- Title
- insert function in c++ vector
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- knapsack
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- how to use max_element in c++ with vector
- Category
- C++
- Title
- vector concat c++
- Category
- C++
- Title
- getline in c++
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- primos menores que
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- print matrix c++
- Category
- C++
- Title
- mark occurances of elements in array cpp
- Category
- C++
- Title
- single line if c++
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- c++ delete dynamically allocated array
- Category
- C++
- Title
- std::iomanip c++
- Category
- C++
- Title
- E/flutter (20384): [ERROR:flutter/third_party/txt/src/minikin/FontFamily.cpp(184)] Could not get cmap table size! E/flutter (20384): F/flutter (20384): [FATAL:flutter/third_party/txt/src/minikin/FontCollection.cpp(95)] nTypefaces == 0
- Category
- C++
- Title
- iterative preorder traversal
- Category
- C++
- Title
- append string to another string c++
- Category
- C++
- Title
- c++ delet from memory
- Category
- C++
- Title
- multiset c++
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- variabili in c++
- Category
- C++
- Title
- longest common subsequence
- Category
- C++
- Title
- c++ string to stream
- Category
- C++
- Title
- C++ int to char*
- Category
- C++
- Title
- what does map.count() return in c++
- Category
- C++
- Title
- get elements of 2d array c++
- Category
- C++
- Title
- rgb(100,100,100,0.5) validation c++
- Category
- C++
- Title
- maximum subarray sum in c++
- Category
- C++
- Title
- select elements from array C++
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- hashset in c++
- Category
- C++
- Title
- c++ ternary operator
- Category
- C++
- Title
- index string c++
- Category
- C++
- Title
- find in set of pairs using first value cpp
- Category
- C++
- Title
- set precision in c++
- Category
- C++
- Title
- set lower bound c++
- Category
- C++
- Title
- string to vector c++
- Category
- C++
- Title
- c++ remove element from vector
- Category
- C++
- Title
- cloud hosting
- Category
- C++
- Title
- pionter in c++
- Category
- C++
- Title
- c++ random numbers
- Category
- C++
- Title
- Insert into vector C++
- Category
- C++
- Title
- how to have a queue as a parameter in c++
- Category
- C++
- Title
- c++ clear console
- Category
- C++
- Title
- cpp pi from acos
- Category
- C++
- Title
- insert elements in array in c++11
- Category
- C++
- Title
- primitive and non primitive data types in c++
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- retu7rn this c++
- Category
- C++
- Title
- min and max heap in cpp
- Category
- C++
- Title
- c++ function to find minimum element in array
- Category
- C++
- Title
- gcd in c++
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- iterate const vector
- Category
- C++
- Title
- expected initializer before 'isdigit'|
- Category
- C++
- Title
- bitset c++
- Category
- C++
- Title
- opperanf >> c++
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- private and public in namespace cpp
- Category
- C++
- Title
- console colors in C++
- Category
- C++
- Title
- how to switch to another branch in git
- Category
- C++
- Title
- how to type cast quotient of two integers to double with c++
- Category
- C++
- Title
- passing a vector to a function c++
- Category
- C++
- Title
- length of string c++
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- ue4 modular character
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- how to print a string to console in c++
- Category
- C++
- Title
- visual studio 2019 read and write text file c++
- Category
- C++
- Title
- how do for loops on c++
- Category
- C++
- Title
- check if intent has extras
- Category
- C++
- Title
- double max value c++
- Category
- C++
- Title
- string substr c++
- Category
- C++