Question

Arrays, Strings, Types and Operators A.1 Specifications A polynomial of a single variable x with integer coefficients is an e

Hint: Save the result in a temporary polynomial and then copy it to the first polynomial. Note: If a polynomial is initialize

/***********************************
* 
* Filename: poly.c 
* 
* 
************************************/

#include "poly.h"


/* 
  Initialize all coefficients and exponents of the polynomial to zero.
 */
void init_polynom( int coeff[ ], int exp[ ] )
{

   /* ADD YOUR CODE HERE */

}  /* end init_polynom */


/*
  Get inputs from user using scanf() and store them in the polynomial.
 */
void get_polynom( int coeff[ ], int exp[ ] )
{

   /* ADD YOUR CODE HERE */

}  /* end get_polynom */


/*
  Convert the polynomial to a string s.
 */
void polynom_to_string( int coeff[ ], int exp[ ], char s[ ] )
{

   /* ADD YOUR CODE HERE */

}  /* end polynom_to_string */



/*
  Evaluate the polynomial for the value of x and store the result p(x) in variable result.
 */
void eval_polynom( int coeff[ ], int exp[ ], double x, double *result )
{

   /* ADD YOUR CODE HERE */

}  /* end eval_polynom */



/*
  Add two polynomials and the result is stored in the first polynomial (arrays co1[] and ex1[]).
 */
void add_polynom( int co1[ ], int ex1[ ], int co2[ ], int ex2[ ] )
{

   /* ADD YOUR CODE HERE */

}  /* end add_ polynom */


/************************** END OF FILE ***************************/
/***********************************
*          
* Filename: poly.h              
* 
************************************/

#include <stdio.h>
#define ASIZE 50
/************************** END OF FILE ***************************/
/***********************************
*          
* Filename: polyMain.c         
* 
************************************/


#include "poly.h"

int main()
{
  int coeff1[ASIZE], exp1[ASIZE], coeff2[ASIZE], exp2[ASIZE];
  char strg[ASIZE*5];
  double x, y;

  do
  {
  init_polynom( coeff1, exp1 );
  get_polynom( coeff1, exp1 );
  polynom_to_string( coeff1, exp1, strg );
  printf( "%s\n", strg);

  init_polynom( coeff2, exp2 );
  get_polynom( coeff2, exp2 );
  polynom_to_string( coeff2, exp2, strg );
  printf( "%s\n", strg);

  add_polynom( coeff1, exp1, coeff2, exp2 );
  polynom_to_string( coeff1, exp1, strg );
  printf( "%s\n\n", strg);
  }
  while ( coeff1[0] );

  polynom_to_string( coeff2, exp2, strg );
  printf( "%s\n", strg);
  do {
    scanf( "%lf", &x );     /* to input a double use "%lf", not "%f" */
    eval_polynom( coeff2, exp2, x, &y ); 
    printf( "p(%.4lf) = %.4lf \n", x, y ); 
  }
  while ( x );

  return 0;  /* normal termination */
}
/************************** END OF FILE ***************************/

/*********************************** * * Filename: poly_in.txt * ************************************/

3 12 5 -5 2 20 0
5 -3 7 4 5 -7 3 10 2 9 0

3 -4 5 -10 2 20 0
5 -3 7 4 5 -7 3 10 2 9 0

0
6 17291 7 42 5 -7098 3 -1 2 8 1 -9 0 

1 1 0
10 1 64 2 50 3 32 4 25 -5 20 6 18 7 15 8 10 -9 1 10 0

2 1 10 1 0
3 1 200 1 45 -1 0

4 -3 3 -2 2 -1 1 -1 0
4 3 3 2 2 1 1 1 0

1.0 2.0 3.0 -2.0 2.55 100.0 7.99492 0.0

/*********************************** * * Filename: poly_Out.txt * ************************************/

12x^5-5x^2+20

-3x^7+4x^5-7x^3+10x^2+9
-3x^7+16x^5-7x^3+5x^2+29

-4x^5-10x^2+20
-3x^7+4x^5-7x^3+10x^2+9
-3x^7-7x^3+29

0
17291x^7+42x^5-7098x^3-x^2+8x-9
17291x^7+42x^5-7098x^3-x^2+8x-9

1
x^64+2x^50+3x^32+4x^25-5x^20+6x^18+7x^15+8x^10-9x+10
x^64+2x^50+3x^32+4x^25-5x^20+6x^18+7x^15+8x^10-9x+11

x^10+1
x^200+x^45-1
x^200+x^45+x^10

-3x^3-2x^2-x-1
3x^3+2x^2+x+1
0

3x^3+2x^2+x+1
p(1.0000) = 7.0000 
p(2.0000) = 35.0000 
p(3.0000) = 103.0000 
p(-2.0000) = -17.0000 
p(2.5500) = 66.2991 
p(100.0000) = 3020101.0000 
p(7.9949) = 1669.9082 
p(0.0000) = 1.0000 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

/* Initialize all coefficients and exponents of the polynomial to zero. */
void init_polynom(int coeff[],int exp[])
{
   int i;
   for(i=0;i<100;i++)
   {
       coeff[i]=0;
       exp[i]=0;
   }
}

/* Get inputs from user using scanf() and store them in the polynomial. */
void get_polynom(int coeff[],int exp[])
{
   int i,n;
   scanf("%d",&n);
   for(i=0;i<n;i++)
   {
       scanf("%d %d",&coeff[i],&exp[i]);
   }
}

int addValueIntoString(char s[],int k,int value)
{
   int temp=value,count=0;
   while(temp>=10)
   {
       temp=temp/10;
       count++;
   }
   int tens=(int)(pow(10,count));
   while(tens>0)
   {
       int digit=value/tens;
       s[k++]=(char)(digit+48);
       value=value%tens;
       tens=tens/10;
   }
   return k;
}
/* Convert the polynomial to a string s. */
void polynom_to_string(int coeff[],int exp[],char s[])
{
   int i,k=0;                               // k is the variable that points to index in s[]
   for(i=0;i<100;i++)
   {
       if(coeff[i]<0)
           s[k++]='-';                       // if number is -ve, then '-' is added into s[]
       int value=abs(coeff[i]);           // adding the coeff into s[]
       if(value>1)
           k=addValueIntoString(s,k,value);
       if(exp[i]!=0)
       {
           s[k++]='x';
           s[k++]='^';
           k=addValueIntoString(s,k,exp[i]);   // adding the exp into s[]
       }
       if(i+1<100 && coeff[i+1]>0)
           s[k++]='+';
   }
   s[k++]='\0';
}

/* Evaluate the polynomial for the value of x and store the result p(x) in variable result. */
void eval_polynom(int coeff[], int exp[], double x, double *result)
{
   double res=1;
   int i;
   for(i=0;i<100;i++)
   {
       if(exp[i]==0)
           res=res+coeff[i];
       else
           res=res*coeff[i]*pow(x,exp[i]);
   }
   *result=res;
}

/* Add two polynomials and the result is stored in the first polynomial (arrays co1[] and ex1[]). */
void add_polynom(int co1[], int ex1[], int co2[], int ex2[])
{
   int i,j,k;
   int coeff[100],exp[100];               // temporary arrays of coeff and exp
   init_polynom(coeff,exp);
   i=0;
   j=0;
   k=0;
   while(i<100 && j<100)
   {
       if(ex1[i]==ex2[j])                   //if both the exponents of polynomial are same then add both coefficients and increment both i,j
       {
           coeff[k]=co1[i]+co2[j];
           exp[k]=ex1[i];
           i++;
           j++;
       }
       else if(ex1[i]>ex2[j])               // if ith exponent is greater then add coeff and exp of i in array and increment i
       {
           coeff[k]=co1[i];
           exp[k]=ex1[i];
           i++;
       }
       else
       {
           coeff[k]=co2[j];               // if jth exponent is greater then add coeff and exp of j in array and increment j
           exp[k]=ex2[j];
           j++;
       }
       k++;
   }
  
   for(int i=0;i<100;i++)                   // add back the results of addition into co1[] and ex1[] arrays
   {
       co1[i]=coeff[i];
       ex1[i]=exp[i];
   }
  
}


int main()
{
   char str[100];
   int coeff1[100],exp1[100];                       // coeff1[] and exp1[] are arrays of polynomial1
   init_polynom(coeff1,exp1);
   printf("Polynomial 1:\n");
   get_polynom(coeff1,exp1);
   polynom_to_string(coeff1,exp1,str);
   printf("%s\n",str);
   double x=1,result=0;
   double *ptr=&result;
   eval_polynom(coeff1,exp1,x,ptr);
   printf("%lf\n",*ptr);
   int coeff2[100],exp2[100];                       // coeff2[] and exp2[] are arrays of polynomial2
   init_polynom(coeff2,exp2);
   printf("Polynomial 2:\n");
   get_polynom(coeff2,exp2);
   add_polynom(coeff1,exp1,coeff2,exp2);
   polynom_to_string(coeff1,exp1,str);
   printf("%s\n",str);
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
/*********************************** * * Filename: poly.c * * ************************************/ #include "poly.h" /* Initialize all coefficients and exponents...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){...

    I need the pseudocode for this c program source code. #include<stdio.h> void printarray(int array[], int asize){ int i; for(i = 0; i < asize;i++) printf("%d", array[i]); printf("\n"); } int sum(int array[], int asize){ int result = 0; int i = 0; for(i=0;i < asize;i++){ result = result + array[i]; } return result; } int swap( int* pA,int*pB){ int result = 0; if(*pA > pB){ int tamp = *pA; *pA = *pB; *pB = tamp; result = 1; } return result;...

  • Add and subtract polynomials using linked lists: The output should look like the following: The code...

    Add and subtract polynomials using linked lists: The output should look like the following: The code for the header file, Polynomial.h, is given as such: ***********HEADER************* #include <iostream> #include <cstdlib> using namespace std; class polyll { //this is class POLYLL, but all lower case private: struct polynode { float coeff; int exp; polynode* link; } * p; public: polyll(); void poly_append(float c, int e); void display_poly(); void poly_add(polyll& l1, polyll& l2); void poly_subtract(polyll& l1, polyll& l2); ~polyll(); }; polyll::polyll() {...

  • howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE]...

    howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8};    int index;    index=0;    res = values[index];    for (int j=1; j<SIZE; j++)    {        if (values[j] > res)        {            res = values[j];            index = j;        cout << index << res << endl;        }    }    cout <<...

  • #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”,...

    #include<stdio.h> #include<stdio.h> int main(){ int i; //initialize array char array[10] = {“Smith”, “Owen”, “Kowalczyk”, “Glass”, “Bierling”, “Hanenburg”, “Rhoderick”, “Pearce”, “Raymond”, “Kamphuis”}; for(int i=0; i<8;i++){ for(int j=0; j<9; j++){ if(strcmp(array[j],array[j+1])>0){ char temp[20]; strcpy(temp,array[j]); strcpy(array[j],array[j+1]); strcpy(array[j+1],temp); } } } printf(“---------File Names---------\n”); for(inti=0; i<9; i++){ printf(“\t%s\n”,array[i]); } printf(-------5 Largest Files according to sorting----\n”); for(int i=0;i>=5;i--) { printf(“\t%s\n”,array[i]); } return0; } Consider the "sort" program (using with void* parameters in the bubblesort function) from the week 10 "sort void" lecture. Modify it as follows...

  • Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin,...

    Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin, cout, cin.getline) . Re-make the func function by the following prototype: void func( double, double &); #include int user_interface(); double func(double); void print_table(); int main (int argc, char *argv[]) {    print_table(user_interface());    return 0 ; } int user_interface(int val){    int input = 0;    printf("This function takes in x and returns an output\n");    printf("Enter Maximum Number of X:");    scanf("%d", &input);...

  • Can anyone add the 1) add_to_coef, add, multiply, coefficient, eval and equals method in the main...

    Can anyone add the 1) add_to_coef, add, multiply, coefficient, eval and equals method in the main function..so can take it from text file.... or 2) make main function with user input for all methods mentioned in the program. import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; public class Polynomial { //array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array. double coefficients[]; static int size; //fube the following...

  • #include <stdio.h> int function() { int x, y; printf("Enter the value of x:"); scanf("%d", &x); y...

    #include <stdio.h> int function() { int x, y; printf("Enter the value of x:"); scanf("%d", &x); y = 3*x*x*x*x*x + 2*x*x*x*x - 5*x*x*x - x*x + 7*x - 6; printf("%d", y); return y; } int main() { function(); } Modify the program of Programming Project 5 so that the polynomial is evaluated using the following formula: ((((3x+2)x-5)x+7)x-6

  • Create a class to represent a term in an algebraic expression. As defined here, a term...

    Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. in the term 4x2, the coefficient is 4 and the exponent 2 in -6x8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent. Your class...

  • #ifndef RADIXSORT #define RADIXSORT #include<vector> // Function to get maximum value in array a[]. template <typename...

    #ifndef RADIXSORT #define RADIXSORT #include<vector> // Function to get maximum value in array a[]. template <typename dataType> int getmax(dataType a[], int n) { int max = a[0]; for (int x = 1; x < n; x++) if (a[x] > max) max = a[x]; return max; } // Function to do counting sort according to significant digits repesented by // exp (where exp is 10^i). template <typename dataType> void CountSort(dataType a[], int n, int exp) { vector <int> result(n); int i,...

  • I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves...

    I JUST NEED HELP WITH DISPLAY PART! please help! thanks in advance // This function saves the array of structures to file. It is already implemented for you. // You should understand how this code works so that you know how to use it for future assignments. void save(char* fileName) { FILE* file; int i; file = fopen(fileName, "wb"); fwrite(&count, sizeof(count), 1, file); for (i = 0; i < count; i++) { fwrite(list[i].name, sizeof(list[i].name), 1, file); fwrite(list[i].class_standing, sizeof(list[i].class_standing), 1, file);...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT