Adding two polynomials with vector of structs

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#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(10);
    vector<poly> b(10);

    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 << "\n";
    polyadd(a,b);
    
    system("pause"); //used to prevent program from closing automatically in windows

}

//================================================================================================================
  
    void polyin(vector<poly>& t)
    {
    int c,e,i = 0;
    char userchoice;
    
         do
         {
          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";
              }
          }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 flag = 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;
             flag = 1;
             }
           }
           if(!flag)
             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 temp;
     int index_of_largest;
     int j;
     for(int i = 0; i < t.size(); i++)
     {
             j = i;
             while(j < (t.size()-1))
             {
                     if(t[j].exp < t[j+1].exp)
                     {
                             index_of_largest = j+1;
                             
                             temp = t[i].exp;
                             t[i].exp = t[index_of_largest].exp;
                             t[index_of_largest].exp = temp;
                     
                             temp = t[i].coef;
                             t[i].coef = t[index_of_largest].coef;
                             t[index_of_largest].coef = temp;
                     }
                     j++;
             }
             
                     /*temp = t[i].exp;
                     t[i].exp = t[index_of_largest].exp;
                     t[index_of_largest].exp = temp;
                     
                     temp = t[i].coef;
                     t[i].coef = t[index_of_largest].coef;
                     t[index_of_largest].coef = temp;*/
             
     } 
}

Last edited on
Since I don't have the patience to read through 148 lines of code:

I think your problem is:
t[i].coef = c;
t[i].exp = e;

read this: http://www.cplusplus.com/reference/stl/vector/operator[]/
and pay attention to what it says about bounds checking.

you might need to do the following:
1
2
3
4
struct poly myPoly;
myPoly.coef = c;
myPoly.end=e;
t.push_back(myPoly);
Topic archived. No new replies allowed.