Starting from:

$25

CPD-Exercise 4 solved

Let’s write a program that can evaluate polynomial values and practice how to use  struct .  

 First, the user needs to provide the numbers of the polynomial term with the coefficient and  exponent of each term. The program will show the polynomial for the user. For example, the

 2    1 polynomial with two terms      3  𝑥 + 2 𝑥 = will be displayed as           3 x^2 + 2x^1 = by the program.           Moreover, please use the  struct  to store each polynomial  term. If you don’t do so, you  will get no points for this exercise. 

 After creating a polynomial equation, the user will then input values for the unknown number  𝑥  , and the program needs to show the calculation  result for each  𝑥 . The program will firstly    show the message “ How many times to calculate:    ” to   ask users to specify the  number of how many times the user can input the unknown number. Then, the program will  display “  The unknown number x is:” to ask the user     to input the integer value for the  unknown number  𝑥  . For example, the program allows  users to input the unknown number  two times in the example inputs below. However, for unwary users, we need to prevent them  from inputting 0 for the number of the polynomial term, calculating times, and the unknown  number 𝑥   . Therefore, the warning message “  The number  must be greater than  zero! ” will be displayed if the user input zero for      the unknown number          x.  Then the program  will show the “ “  The unknown number x is: ” to ask    the user to input the value again ( see the second input example).

 The table below shows the example input and output. The underscored number is the input  from users.  Notice: The data type for the calculation  result of the polynomial is long  integer. 

 Input
 Output
Enter the number of terms:   2 

 Enter each term with cof and exp: 

 3 2 

2  1 

 How many times to calculate: 2 

The unknown number x is:    1  

The unknown number x is:    2  
3 x^2 + 2x^1 = 

 5 

 16 
Enter the number of terms:   2 

 Enter each term with cof and exp: 

3  2 

 2 1 

 How many times to calculate: 2 

The unknown number x is:    0  

The unknown number x is:    1  

The unknown number x is:     2 
3 x^2 + 2x^1 = 

 The number must be greater  than zero! 

 5 

 16 
Enter the number of terms:  0  

Enter the number of terms:   1 

 Enter each term with cof and exp: 

 1 1 

 How many times to calculate: 2 

The unknown number x is:    1  

The unknown number x is:    2  
 The number must be greater  than zero! 

1 x^1 = 

 1 

 2 
 

More products