Data Structure Slip No_4B

 
#include <stdio.h>
 
#define MAX_DEGREE 100
 
void addPolynomials(int p1[], int p2[], int result[], int d1, int d2)
{
    int maxDegree = (d1 > d2) ? d1 : d2;
 
    for (int i = 0; i <= maxDegree; i++)
   {
        result[i] = p1[i] + p2[i];
    }
}
 
int main()
{
    int d1, d2;
   
    printf("Enter the degree of the first polynomial: ");
    scanf("%d", &d1);
 
    int p1[MAX_DEGREE+1]; // +1 to store coefficients from 0 to degree
    printf("Enter the coefficients of the first polynomial:\n");
    for (int i = 0; i <= d1; i++)
   {
        scanf("%d", &p1[i]);
    }
 
    printf("Enter the degree of the second polynomial: ");
    scanf("%d", &d2);
 
    int p2[MAX_DEGREE+1];
    printf("Enter the coefficients of the second polynomial:\n");
    for (int i = 0; i <= d2; i++)
    {
        scanf("%d", &p2[i]);
    }
 
    int result[MAX_DEGREE+1];
    addPolynomials(p1, p2, result, d1, d2);
 
    printf("Resultant polynomial after addition:\n");
    for (int i = 0; i <= (d1 > d2 ? d1 : d2); i++)
   {
        printf("%d ", result[i]);
    }
    printf("\n");
 
    return 0;
}

No comments:

Post a Comment