Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. /*
  2. Rapando C Samson
  3.  
  4. A Program that adds two polynomials in an array and displays the result
  5.  
  6. Sat Oct 3rd 2015
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. #define MAXDEGREE 90 //the maximum degree of any polynomial that can be added.
  13. // declare a structure to hold the polynomials
  14. typedef struct
  15. {
  16. int deg; //degree
  17. int coef[MAXDEGREE]; //stores the coefficients
  18. } poly;
  19.  
  20. int main()
  21. {
  22. poly polynomialA; // the first polynomial
  23. poly polynomialB; // the second polynomial
  24. 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
  25. int terminator; // a variable that will prevent the program from stopping after execution
  26. /*
  27. First, let the user enter the degree of the two polynomials
  28. */
  29. printf("\nAdd polynomials in an array\n\n_______________\n\n"
  30. "Enter the degree of the first polynomial: ");
  31. scanf("%d", &polynomialA.deg);
  32. printf("\nEnter the degree of the second polynomial: ");
  33. scanf("%d", &polynomialB.deg);
  34. /*
  35. Then get the larger degree and assign it to maxdeg
  36. */
  37. if(polynomialA.deg > polynomialB.deg) {
  38. maxdeg = polynomialA.deg;
  39. } else if (polynomialA.deg < polynomialB.deg) {
  40. maxdeg = polynomialB.deg;
  41. } else {
  42. maxdeg = polynomialA.deg;
  43. }
  44.  
  45. /*
  46. Get the coefficients of the two polynomials
  47. */
  48. printf("\nEnter the coefficients of a, from the largest power of X including 0s for blank fields\n...\n");
  49.  
  50. for(i=0; i<=maxdeg; i++) {
  51. scanf("%d", &polynomialA.coef[i]);
  52. }
  53. printf("\n\nNow enter the coefficients of b, from the largest power of X including 0s for blank fields\n...\n");
  54. for(j=0; j<=maxdeg; j++) {
  55. scanf("%d", &polynomialB.coef[j]);
  56. }
  57. printf("\n\n...");
  58. /*
  59. call the function to add and display the sum of the polynomials
  60. */
  61. addpolynomials(polynomialA, polynomialB);
  62. //prevent terminating the program
  63. scanf("%d", &terminator);
  64. return 0;
  65.  
  66. }
  67. /*
  68. Function to add and display the polynomials and also display their sums
  69. */
  70. void addpolynomials(poly a, poly b)
  71. {
  72. poly c; //declare another polynomial to add the two polynomials above;
  73. int y,z; //counter
  74. int maxdeg; // the maximum degree
  75. //find the maximum degree and assign it to maxdeg
  76. if(a.deg > b.deg) {
  77. maxdeg = a.deg;
  78. } else
  79. {
  80. maxdeg = b.deg;
  81. }
  82. // set the degree of the resultant polynomials to be the degree of the largest polynomial
  83. c.deg = maxdeg;
  84.  
  85. //add the coefficients of the two polynomials and assign them to the respective position of the coefficients of the resultant polynomial
  86. for(y =0; y <= maxdeg; y++) {
  87. c.coef[y] = a.coef[y] + b.coef[y];
  88. }
  89. //print out the first polynomial
  90.  
  91. //print out the resultant polynomial
  92. printf("\n_____\nAnd the resultant polynomial is \n");
  93.  
  94. for(z = 0; z<=maxdeg; z++) {
  95. printf("%i ", c.coef[z]);
  96. }
  97. printf("\nAnd its degree is %d\n", c.deg);
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement