I am having problems primarily with the sorting function. What I need is for the polynomials after being entered to be sorted from the highest exponent to the lowest and then added and to have that displayed. The other thing is that I need to do a pushback for the vector, but it does not compile when I do so and therefore I have a 10 element vector. Any help is greatly appreciated. Thanks in advance!

#include<iostream>
#include<vector>
using namespace std;

//================================================================================================================
//Normally part of header file

struct poly
{
int coef;
int exp;
};

void polyin(vector<poly>&);
    
void polyappend(int c, int e,int &i,vector<poly>&);
    
void display(vector<poly> &t, int &i);
    
void polyadd(vector<poly> &a, vector<poly> &b);

void polysort(vector<poly> &t);

//================================================================================================================

int main()
{
    vector<poly> a(0);
    vector<poly> b(0);

    cout << "Polynomial 1: \n";
    cout << "Type in the coefficient of the first term in your polynomial followed by the exponent.\n";
    polyin(a);

    cout << "\nPolynomial 2: \n";
    cout << "Type in the coefficient of the first term in your polynomial followed by the exponent.\n";
    polyin(b);
    cout << endl;
    polyadd(a,b);
    system("pause"); //used to prevent program from closing automatically in windows

}

//================================================================================================================
  
    void polyin(vector<poly>& t)
    {
    int c,e,i = 0;
    int psize = t.size() + 1;
    char userchoice;
    
         do
         {
          t.resize(psize);
          cin >> c >> e;
          polyappend(c,e,i,t);
          i++;
          cout << "Enter another term? [y/n]\n";
          cin >> userchoice;
        
              if(userchoice == 'y')
              {
              cout << "Enter next term.\n";
              }
          psize++;
          }while(userchoice == 'y');
         polysort(t);
         display(t,i);
      }
      
      void polyappend(int c,int e,int &i,vector<poly>& t)
      {
      t[i].coef = c;
      t[i].exp = e;
      }
      
      void display(vector<poly> &t,int &i)
      {
      int f = 0;
          for(int j = 0; j < i; j++)
          {
           if(t[j].exp != 0)
             cout << t[j].coef << "x^" << t[j].exp << " + ";
             else
             {
             cout << t[j].coef;
             f = 1;
             }
           }
           if(!f)
             cout << "\b\b ";
       }

       void polyadd(vector<poly> &a,vector<poly> &b)
       {
        int psize;
        if(a.size() > b.size())
        {
            psize = a.size();
        }
        else
        {
            psize = b.size();
        }
            for(int p = 0; p < psize; p++)
            {
             if(a[p].exp == b[p].exp)
             {
             a[p].coef +=b[p].coef;
             }
            }
            display(a,psize);
            cout << "\n";
       }

void polysort(vector<poly> &t)
{
     int target, temp;
     int j;
     int n = t.size();
     for(int i = 1; i <= n-1; i++)
     {
             j = i;
             target = t[i].exp;
             temp = t[i].coef; 
             while(j > 0 && target < t[j-1].exp)
             {
                     t[j].exp = t[j-1].exp;
                     t[j].coef = t[j-1].coef;
                     j--;
             }
             t[j].exp = target;
             t[j].coef = temp;
     } 
}

Since you are already using std::vector, why not use std::sort. All you have to do
is define a compare function.

bool sortByExp(const poly& lhs,const poly& rhs){
 return lhs.exp < rhs.exp;
}

//....
//somewhere in your code
std::sort(a.begin(),a.end(),sortByExp);

First of all I have to agree with firstPerson's advice.

Second PLEASE don't use variable names like exp and the do maths operations. You are bound to at some point want to use the maths library (cmath). Then you are in a mess!!

Finally why not include the addition and other functions as a method of a polynomial class?

This article has been dead for over six months. Start a new discussion instead.