Trouble adding elements of an array

Hello Everyone,

I have a small problem that I think can be resolved easily. I am making a polynomial program, and the only hang-up is adding the coefficients.

At the end of the code, it prints coefficients that are the same, but there is some strange adding going on.

I am an amateur in C++ but not coding, so please be sure to critique my code if need be.

Thank you very much for your reply's.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include "Polynomial.h"

using namespace std;


int main()
{
	Polynomial user;

	cout << "How man polynomials do you want to enter?: ";
	cin  >> polynomial;
        user.EnterNumbers(polynomial);
	
	user.AddCoefficients();	

}



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
#include <iostream>
using namespace std;


int    counter;
double coefficient [100];
int    exponent [100];
double sum = 0;
int    newExponent = 0;
int    polynomial = 0;

class Polynomial
{
public: void EnterNumbers(int polyNum)
	{
		
		for (counter = 0; counter < polyNum; counter++)
		{
			cout << "Enter a coefficient " << (counter + 1) << ": ";
			cin  >>  coefficient[counter];
			cout << "Enter an exponent ";
			cin  >>  exponent[counter];		    
		} 
	}

public: void AddCoefficients()
	{
		cout << "You entered: ";

		for(counter = 0; counter < polynomial; counter++ )
			cout << " " << coefficient[counter] << "x^" << exponent[counter] << ", ";
		cout << endl;

		for(int i = 0; i < polynomial; i++)
		{
			if(exponent[i] == exponent[i + 1])
			{
				newExponent = exponent[i];
				sum += coefficient[i] + coefficient[i + 1]; 
				cout << sum << "^" << newExponent << " ";

			}
		}
	}
};


You could do with a better OOP approach.
The polynomial class should contain a vector of the coefficients arranged in their right indices.
Then overload the + operator or make a separate function to add.
Then add the polynomials as you'd add integers, using a loop!
Last edited on
Thank you very much for your reply. I will do exactly what you stated.

Can you explain to me why you would use vectors instead of arrays? I am not that familiar with vectors, I know that vectors are more flexible than arrays, but that's about it.
Last edited on
It takes care almost everything for you in case the size is unknown (here you may not know the degree of the polynomial before the user actually enters it), and may need to change to occasionally.

vectors don't need you to get much familiar with them for simple tasks.
All you need for this is
the push_back() , pop_back(), and back() functions.
Remember that you can access its elements like arrays, using the [ ] operator.

Also, a few tips:
1. Avoid global variables unless you are sure that your program requires them. (Here, none is required))
2.You need to mention public: once . After that everything you declare is public, until you put private: or protected: .
3. In this case you need to deal with the input in a better way; asking how many polynomials initially, then for each polynomial, asking the number of coefficients.
Thank you manas, your advice was extremely helpful and informative
Topic archived. No new replies allowed.