Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Rapando C Samson
- A Program that adds two polynomials in an array and displays the result
- Sat Oct 3rd 2015
- */
- #include <stdio.h>
- #include <stdlib.h>
- #define MAXDEGREE 90 //the maximum degree of any polynomial that can be added.
- // declare a structure to hold the polynomials
- typedef struct
- {
- int deg; //degree
- int coef[MAXDEGREE]; //stores the coefficients
- } poly;
- int main()
- {
- poly polynomialA; // the first polynomial
- poly polynomialB; // the second polynomial
- int i,j, maxdeg; // i and j are counters to help us in looping, maxdeg stores the degree of the polynomial with the larger degree
- int terminator; // a variable that will prevent the program from stopping after execution
- /*
- First, let the user enter the degree of the two polynomials
- */
- printf("\nAdd polynomials in an array\n\n_______________\n\n"
- "Enter the degree of the first polynomial: ");
- scanf("%d", &polynomialA.deg);
- printf("\nEnter the degree of the second polynomial: ");
- scanf("%d", &polynomialB.deg);
- /*
- Then get the larger degree and assign it to maxdeg
- */
- if(polynomialA.deg > polynomialB.deg) {
- maxdeg = polynomialA.deg;
- } else if (polynomialA.deg < polynomialB.deg) {
- maxdeg = polynomialB.deg;
- } else {
- maxdeg = polynomialA.deg;
- }
- /*
- Get the coefficients of the two polynomials
- */
- printf("\nEnter the coefficients of a, from the largest power of X including 0s for blank fields\n...\n");
- for(i=0; i<=maxdeg; i++) {
- scanf("%d", &polynomialA.coef[i]);
- }
- printf("\n\nNow enter the coefficients of b, from the largest power of X including 0s for blank fields\n...\n");
- for(j=0; j<=maxdeg; j++) {
- scanf("%d", &polynomialB.coef[j]);
- }
- printf("\n\n...");
- /*
- call the function to add and display the sum of the polynomials
- */
- addpolynomials(polynomialA, polynomialB);
- //prevent terminating the program
- scanf("%d", &terminator);
- return 0;
- }
- /*
- Function to add and display the polynomials and also display their sums
- */
- void addpolynomials(poly a, poly b)
- {
- poly c; //declare another polynomial to add the two polynomials above;
- int y,z; //counter
- int maxdeg; // the maximum degree
- //find the maximum degree and assign it to maxdeg
- if(a.deg > b.deg) {
- maxdeg = a.deg;
- } else
- {
- maxdeg = b.deg;
- }
- // set the degree of the resultant polynomials to be the degree of the largest polynomial
- c.deg = maxdeg;
- //add the coefficients of the two polynomials and assign them to the respective position of the coefficients of the resultant polynomial
- for(y =0; y <= maxdeg; y++) {
- c.coef[y] = a.coef[y] + b.coef[y];
- }
- //print out the first polynomial
- //print out the resultant polynomial
- printf("\n_____\nAnd the resultant polynomial is \n");
- for(z = 0; z<=maxdeg; z++) {
- printf("%i ", c.coef[z]);
- }
- printf("\nAnd its degree is %d\n", c.deg);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement