sidrs

Polynomial Representation and Addition

Jul 24th, 2024 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. typedef struct Polynomial {
  6.     int coeff;
  7.     int exponent;
  8. } Poly;
  9.  
  10. void populate(Poly POLY[], int TERMS){
  11.     cout << "Enter the Coefficents and Exponents for the Polynomial Expression:\n";
  12.     for (int i = 0; i < TERMS; i++){
  13.         int COEFF = 0; int EXPO = 0;
  14.         cout << "Enter the Coefficient: "; cin >> COEFF;
  15.         cout << "Enter the Exponent: "; cin >> EXPO;
  16.         POLY[i].coeff = COEFF;
  17.         POLY[i].exponent = EXPO;
  18.     }
  19. }
  20.  
  21. void display(Poly POLY[], int TERMS){
  22.     cout << "\nThe Polynomial Expression is: \n";
  23.     for (int i = 0; i < TERMS; i++){
  24.         if (POLY[i].coeff == 0) continue;
  25.         else if (i == (TERMS-1)) {
  26.             if (POLY[i].exponent == 0) {
  27.                 cout << POLY[i].coeff << endl;
  28.             } else {
  29.                 cout << POLY[i].coeff <<  "x^" << POLY[i].exponent << endl;            
  30.             }
  31.         } else {
  32.             cout << POLY[i].coeff << "x^" << POLY[i].exponent << " + ";
  33.         }
  34.     }
  35. }
  36.  
  37. void add(Poly *POLY1, Poly *POLY2, int TERMS1, int TERMS2) {
  38.     for (int i = 0; i < TERMS1; i++){
  39.         for (int j = 0; j < TERMS2; j++){
  40.             if (POLY1[i].exponent == POLY2[j].exponent){
  41.                 POLY1[i].coeff = POLY1[i].coeff + POLY2[j].coeff;
  42.             }
  43.         }
  44.     }
  45.     cout << "Required Sum is: \n";
  46.     for (int i = 0; i < TERMS1; i++){
  47.         if (POLY1[i].coeff == 0) continue;
  48.         else if (i == (TERMS1-1)) {
  49.             if (POLY1[i].exponent == 0) {
  50.                 cout << POLY1[i].coeff << endl;
  51.             } else {
  52.                 cout << POLY1[i].coeff <<  "x^" << POLY1[i].exponent << endl;            
  53.             }
  54.         } else {
  55.             cout << POLY1[i].coeff << "x^" << POLY1[i].exponent << " + ";
  56.         }
  57.     }
  58. }
  59.  
  60. int main(){
  61.     Poly P1[3];
  62.     Poly P2[3];
  63.     populate(P1, 3);            
  64.     populate(P2, 3);
  65.     add(P1, P2, 3, 3);
  66.     return 0;
  67. }
  68.  
  69.  
  70. /* PROGRAM OUTPUT *
  71.  
  72. Enter the Coefficents and Exponents for the Polynomial Expression:
  73. Enter the Coefficient: 1
  74. Enter the Exponent: 2
  75. Enter the Coefficient: 2
  76. Enter the Exponent: 1
  77. Enter the Coefficient: 3
  78. Enter the Exponent: 0
  79. Enter the Coefficents and Exponents for the Polynomial Expression:
  80. Enter the Coefficient: 7
  81. Enter the Exponent: 2
  82. Enter the Coefficient: 8
  83. Enter the Exponent: 1
  84. Enter the Coefficient: 9
  85. Enter the Exponent: 0
  86.  
  87. The Polynomial Expression is:
  88. 1x^2 + 2x^1 + 3
  89.  
  90. The Polynomial Expression is:
  91. 7x^2 + 8x^1 + 9
  92.  
  93. Required Sum is:
  94. 8x^2 + 10x^1 + 12
  95.  
  96. * END OUTPUT */
  97.  
  98.  
Advertisement
Add Comment
Please, Sign In to add comment