Question

In: Computer Science

in the c programming language input is given in the form The input will be of...

in the c programming language input is given in the form The input will be of the form [number of terms] [coefficient k] [exponent k] … [coefficient 1] [exponent 1] eg.

5 ─3 7 824 5 ─7 3 1 2 9 0

in this there are 5 terms with -3x^7 being the highest

/* 
  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 */

Solutions

Expert Solution

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

int n=0;       // n is number of elements which is a global variable

/* Initialize all coefficients and exponents of the polynomial to zero. */
void init_polynom(int coeff[],int exp[])
{
   int i;
   for(i=0;i<n;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;
   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<n;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[]
       k=addValueIntoString(s,k,value);
       s[k++]='x';
       s[k++]='^';
       k=addValueIntoString(s,k,exp[i]);   // adding the exp into s[]
       if(i+1<n && 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<n;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[]). */
/* A small note to this method is, this method works correctly only if both the polynomials have same degrees or else wrong outputs will be generated. Please consider this point too. */
void add_polynom(int co1[], int ex1[], int co2[], int ex2[])
{
   int i;
   for(i=0;i<n;i++)
   {
       co1[i]=co1[i]+co2[i];
   }
}


int main()
{
   char str[100];
   scanf("%d",&n);
   int coeff[n],exp[n];
   init_polynom(coeff,exp);
   get_polynom(coeff,exp);
   polynom_to_string(coeff,exp,str);
   printf("%s\n",str);
   double x=1,result=0;
   double *ptr=&result;
   eval_polynom(coeff,exp,x,ptr);
   printf("%lf\n",*ptr);
   add_polynom(coeff,exp,coeff,exp);
   polynom_to_string(coeff,exp,str);
   printf("%s\n",str);
   return 0;
}


Related Solutions

Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output....
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output. Assignment: It’s an organization that accepts used books and then sells them a couple of times a year at book sale events. Some way was needed to keep track of the inventory. You’ll want two parallel arrays: one to keep track of book titles, and one to keep track of the prices. Assume there will be no more than 10 books. Assume no more...
Write a program to create a tree randomly. You can use C++ programming language. The input...
Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
JAVA programming language: For refrence: nextInt ( ) Scans the next token of the input as...
JAVA programming language: For refrence: nextInt ( ) Scans the next token of the input as an int. How many times does the while loop execute, if the input is 38 20 4 0? Assume a Scanner object scnr has been instantiated, and that the tokens in the input are delimited by the space character. That means there are 4 tokens in the input. int num = 2000; while (num >= 20) { //Do something num = scnr.nextInt(); } select...
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if (M <= N + 3 && (C == ‘N’ || C == ‘n’)) C = ‘0’; else C = ‘1’; Assume that M and N are 32-bit signed integer variables, and C is an 8-bit ASCII character variable. All variables are stored in memory, and all general-purpose registers are available for use.
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT