Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,626 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,984 people online right now. Registration is fast and FREE... Join Now!





Synthetic Division

This program can divide a polynomial of any degree by a binomial of the type (x-h)

Submitted By: Louisda16th
Actions:
Rating:
Views: 3,253

Language: C++

Last Modified: September 21, 2006
Instructions: compile, build and run

Snippet


  1. //Created by Louisda16th/Ashwith
  2. #include<iostream>
  3. using namespace std;
  4. int main()
  5. {
  6.      float *coeff, *anscoeff, k;                    //coeff contains the coefficients of the dividend and anscoeff contains
  7.      int n,i;                                        //coefficient of the answer. n is the degree of the polynomial, i is a counter
  8.  
  9.      cout<<"Enter Degree Of Expression";
  10.      cin>>n;
  11.      
  12.      coeff = new float[n];
  13.      anscoeff = new float[n-1];
  14.      i=n;
  15.      
  16.      //this loop gets the coefficients of the polynomial
  17.      while (i>=0)
  18.      {
  19.           if (i!=0)
  20.           {
  21.                if (i == 1)
  22.                {
  23.                     cout<<endl<<"Coefficient of x";
  24.                     cin>>coeff[i];
  25.                }
  26.                else
  27.                {
  28.                     cout<<endl<<"Coefficient of x^"<<i;
  29.                     cin>>coeff[i];
  30.                }
  31.           }
  32.           else
  33.           {
  34.                cout<<endl<<"Enter Constant Term";
  35.                cin>>coeff[i];
  36.           }
  37.                i--;
  38.      }
  39.      anscoeff[n] = coeff[n];
  40.      
  41.      cout<<endl<<"Enter Constant term(k) of Divisor (x-k)";
  42.      cin>>k;
  43.      cout<<"Quotient= "<<anscoeff[n]<<"x^"<<n-1;
  44.      i=(n-1);
  45.      //this loop calculates the coefficients of the quotient and stores them in anscoeff
  46.      while (i>0)
  47.      {
  48.           anscoeff[i]=coeff[i]+k*anscoeff[i+1];
  49.           if((i-1)!=0 && (i-1)!=1)
  50.           {
  51.                if (anscoeff[i]>=0)
  52.                     cout<<" +"<<anscoeff[i]<<"x^"<<(i-1);
  53.                else
  54.                     cout<<" "<<anscoeff[i]<<"x^"<<(i-1);
  55.           }
  56.           else if((i-1)==1)
  57.           {
  58.                if (anscoeff[i]>=0)
  59.                     cout<<" +"<<anscoeff[i]<<"x";
  60.                else
  61.                     cout<<" "<<anscoeff[i]<<"x";
  62.           }
  63.           else if((i-1)==0)
  64.           {
  65.                if (anscoeff[i]>=0)
  66.                     cout<<" +"<<anscoeff[i];
  67.                else
  68.                     cout<<" "<<anscoeff[i];
  69.           }
  70.  
  71.           i--;
  72.      }
  73.      cout<<endl<<"Remainder= "<<(coeff[0]+k*anscoeff[1]);
  74.      cout<<endl;
  75.      return 0
  76.  
  77. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month