Question: Help write this program in C A common way to represent a polynomial expression (e.g. 3x3 - 2.2x3 + 4x - 1.713) is with linked
Help write this program in C
A common way to represent a polynomial expression (e.g. 3x3 - 2.2x3 + 4x - 1.713) is with linked structures that have three members: a) A member to represent the coefficient (3) b) A member to represent the exponent (5) c) A pointer to the next term in the expression Write a program that does the following: 1) Write a function input_poly(ptrl) that reads in two polynomials at a time with coefficient and exponent from a file cp7_in.txt. The file will contain 2n polynomials, where n is an integer that is 10 or less. Space for the polynomial will be allocated in run time and there is no limit to the size of the polynomial, e..g the first two lines of the file is 35 -2.2 3 41-1.7130 3 100 -2.2 3 14 55 3.1 101 (Note the terms may be in any order) Then, two polynomials will be created 3x3 - 2.2x3 + 4x - 1.713 and 3.1x101 + 3x100 + 14x35 - 2.2x 2) Write a function add_poly(ptr1, ptr2) that expects pointers ptrl and ptr2 to two lists that represent polynomials and returns a pointer to a third list that represents the sum of polynomials. You will find the sum of every pair of polynomials. 3) Write a function mult_poly(ptrl, ptr2) that expects pointers ptrl and ptr2 to two lists that represent polynomials and returns a pointer to a third list that represents the product of polynomials. You will find the product of every pair of polynomials. 4) Write a function print_poly(ptrl) that prints the polynomial pointed to by ptrl to a file, cp7.out. The terms must be printed in descending order of exponent. That is to say, e.g. 3x3 - 2.2x3 + 4x - 1.713 is correct but e.g.- 2.2x3 + 3x3 + 4x - 1.713 is not. From the function main(), you may call the function print_poly(ptrl) to print out the following to cp7.out: The sum of polynomials:: 3x3 2.2x3 + 4x - 1.713 3.1x101 + 3x100 + 14x55 2.2x3 3.1x101 + 3x100 + 14x35 + 3x3 4.4x + 4x - 1.713 The product of polynomials:: 3x3 - 2.2x3 + 4x - 1.713 3.1x101 + 3x100 + 14x35 2.2x3 is 9.3x106 + 9x105 6.82x104 6.6x103 + 12.4x102 + 6.6897x101 3.139.100 + 42x60_ 30.8x38 + 56x56 23.982x55 6.6x3 + 4.8478 8.8x4 + 3.7686x3 Precision up to 3 digits after the decimal is enough. Do not need more and do not print more (I have here, but you should not). Note that each polynomial can have any number of terms (which is why you will require malloc/calloc) but the total number of polynomials in the file will be 2n. There will be n pairs of polynomials. n is 10 pairs or less (i.e. 20 polynomials maximum). A common way to represent a polynomial expression (e.g. 3x3 - 2.2x3 + 4x - 1.713) is with linked structures that have three members: a) A member to represent the coefficient (3) b) A member to represent the exponent (5) c) A pointer to the next term in the expression Write a program that does the following: 1) Write a function input_poly(ptrl) that reads in two polynomials at a time with coefficient and exponent from a file cp7_in.txt. The file will contain 2n polynomials, where n is an integer that is 10 or less. Space for the polynomial will be allocated in run time and there is no limit to the size of the polynomial, e..g the first two lines of the file is 35 -2.2 3 41-1.7130 3 100 -2.2 3 14 55 3.1 101 (Note the terms may be in any order) Then, two polynomials will be created 3x3 - 2.2x3 + 4x - 1.713 and 3.1x101 + 3x100 + 14x35 - 2.2x 2) Write a function add_poly(ptr1, ptr2) that expects pointers ptrl and ptr2 to two lists that represent polynomials and returns a pointer to a third list that represents the sum of polynomials. You will find the sum of every pair of polynomials. 3) Write a function mult_poly(ptrl, ptr2) that expects pointers ptrl and ptr2 to two lists that represent polynomials and returns a pointer to a third list that represents the product of polynomials. You will find the product of every pair of polynomials. 4) Write a function print_poly(ptrl) that prints the polynomial pointed to by ptrl to a file, cp7.out. The terms must be printed in descending order of exponent. That is to say, e.g. 3x3 - 2.2x3 + 4x - 1.713 is correct but e.g.- 2.2x3 + 3x3 + 4x - 1.713 is not. From the function main(), you may call the function print_poly(ptrl) to print out the following to cp7.out: The sum of polynomials:: 3x3 2.2x3 + 4x - 1.713 3.1x101 + 3x100 + 14x55 2.2x3 3.1x101 + 3x100 + 14x35 + 3x3 4.4x + 4x - 1.713 The product of polynomials:: 3x3 - 2.2x3 + 4x - 1.713 3.1x101 + 3x100 + 14x35 2.2x3 is 9.3x106 + 9x105 6.82x104 6.6x103 + 12.4x102 + 6.6897x101 3.139.100 + 42x60_ 30.8x38 + 56x56 23.982x55 6.6x3 + 4.8478 8.8x4 + 3.7686x3 Precision up to 3 digits after the decimal is enough. Do not need more and do not print more (I have here, but you should not). Note that each polynomial can have any number of terms (which is why you will require malloc/calloc) but the total number of polynomials in the file will be 2n. There will be n pairs of polynomials. n is 10 pairs or less (i.e. 20 polynomials maximum)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
