Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a code in C A common way to represent a polynomial expression (e.g. 3x3 - 2.2x3 + 4x - 1.713) is with linked structures

image text in transcribed

Write a code 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Demystifying Databases A Hands On Guide For Database Management

Authors: Shiva Sukula

1st Edition

8170005345, 978-8170005346

More Books

Students also viewed these Databases questions

Question

b. Why were these values considered important?

Answered: 1 week ago

Question

=+ Are some laws more important than others? If so, which ones?

Answered: 1 week ago

Question

=+j Describe how EU directives impact IHRM.

Answered: 1 week ago