sum two quadratic polynomials

hello. we were asked to Implement a class named Quadratic that represents second degree polynomials. one of the functions is to sum two polynomials as such:
write a “sum” function which takes two quadratic polynomials and save their summation in the calling object (the calling object is a polynomial r with coefficients equal to 0)
Note that if p(x)= ax2 + bx + c and q(x)= a′x2 +b′x +c′, then their summation is the polynomial given by (a + a′)x2 + (b + b′)x + (c + c′).
i will show you what i tried to write.

1
2
3
4
5
6
7
8
9
10
11
  int sum(double Parr[2], double Qarr[2])
	{
		for(int i = 0; i < 3; i++){
			for(int j = 0; j < 3; j++){
				if(i == j){
					double s[] = Parr[i] + Qarr[j];
				}
			}
		}
		return s;
	}
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
#include <iostream>

using namespace std;

double* sum(double Parr[],  double Qarr[])
{
           double s[2];
           for(int i = 0; i < 3; i++)
           {
                      s[i] = Parr[i]  + Qarr[i];
           }
           
           return s;
}

int main()
{
           double P[2], Q[2];

           cout<<"Enter the coefficients of P:\n";
	   for(int i = 0; i < 3; i++)
	   {
		cin>>P[i];
           } 

   	   cout<<"Enter the coefficients of Q:\n";
	   for(int i = 0; i < 3; i++)
	   {
		cin>>Q[i];
           } 

	   cout<<endl;

           double* add;
	   add = sum(P, Q);

           for(int i = 0; i < 3; i++)
	   {
		cout<<add[i]<<endl;;
           }

           return 0;

}


This is what my output is (definitely wrong):

Enter the coefficients of P:
5.6
6.2
9.3
Enter the coefficients of Q:
2.2
5.4
7.8

10
3.42823e+230
17.1



What is the mistake? Can't find it.
Multiple runs show that
1. The third elements of both the arrays are added correctly.
2. Second elecment gives some random value
3. First element of the add array is actually the sum of first and third element of array Q.
@sheel Even if you are working on the same project, you seem to be a different person encountering a different error. I think you should make your own topic that contains a link to this one. And mention that you are working on the same project as the this topic, but having a different error. If you are someone who is trying to help, you may want to learn some more about what the OP was trying to ask. There are no clear questions given by the OP. And I recommend you turn on warning messages for your compiler. On line 7 you allocate the s array on the function's local stack and return the pointer value on line 13. With warnings enabled, you would realized this leads to undefined behavior. It is okay to return a stack variable by value or by copy, but not by reference. In summary, turn on warnings and encourage OPs to ask at least one question. Welcome to the forums :)

@eliiofaddoul Please provide more details about what you have tried, what you expect the program to do, and what it is actually doing. That will help us to help you better in the future. On line 6, I see that you are allocating a variable local to the innermost for loop. This variable will not be in scope for the return statement on line 10. Since you are practicing object oriented programming by using a class that contains this function, you should create a local variable that shares the same class type to hold the results and return it. For example, it might look something like this:

1
2
3
4
5
6
7
Quadratic Quadratic::sum(Quadratic other) {
    Quadratic s;
    for(int i = 0; i < 3; ++i) {
        s.coefficients[i] = coefficients[i] + other.coefficients[i];
    }
    return s;
}
Last edited on
Okay @kevinkjt2000, to clear things up i will show the whole code. I am interested in the part in main where the member function "sum" is called. Maybe now it's a bit clearer
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
#include <iostream>
using namespace std;

class quadratic
{
private:
	int a;
	int b;
	int c;
public:
	quadratic()
	{
		a=b=c=0;
	}
	quadratic(int A, int B, int C)
	{
		A=a;
		B=b;
		C=c;
	}

	int evaluate(int x)
	{
		int value = a*(x^2) + b*(x) + c;
		return value;
	}

	void print()
	{
		cout << a << "x^2 + " << b << "x + " <<  c << endl;
	}

	int derivative(int X)
	{
		int deriv = 2*a*X + b;
		return deriv;
	}

	int sum(double Parr[2], double Qarr[2])
	{
		for(int i = 0; i < 3; i++){
			for(int j = 0; j < 3; j++){
				if(i == j){
					double s[] = Parr[i] + Qarr[j];
				}
			}
		}
		return s;
	}

	int delta ()
	{
		int Delta = b^2 - 4*a*c;
		return Delta;
	}

	int realRoots(double &y, double &z)
	{
		int nbr_of_roots;
		if(a != 0 && delta() >= 0){
			nbr_of_roots = 2;
			y = (- b + (delta()^(1/2))) / (2*a);
			z = (- b - (delta()^(1/2))) / (2*a);
			if(delta() == 0)
				y = z = (- b + (delta()^(1/2)) ) / (2*a);
		}

		if(a != 0 && delta() < 0){
			nbr_of_roots = 0;
		}

		if(a == 0 && b != 0) {
			nbr_of_roots = 1;
			y = -c/b;
		}

		if(a == 0 && b==0) {
			if(c != 0)
				nbr_of_roots = 0;
			else nbr_of_roots = 999;
		}
		return nbr_of_roots;
	}
};

int main()
{
	quadratic p(1,5,3),q(1,2,1), r;
	cout<<"p: ";p.print();
	cout<<"q: ";q.print();
	cout<<"r: ";r.print();
	cout<<"p(2)="<<p.evaluate(2)<<endl;
	r.sum(p,q);
	cout<<"p+q: ";r.print();
	cout<<"derivative of p for x=5: "<<p.derivative (5)<<endl;
	double x1,x2;
	int nbRoots = p.realRoots(x1,x2);
	if (nbRoots ==2)
		cout<<"roots of p : "<<x1<<","<<x2<<endl;
	else if (nbRoots==1)
		cout<<"root of p: "<<x1<<endl;
	else if (nbRoots==0)
		cout<<"No roots for p"<<endl;
	else if (nbRoots=999)
		cout<<"Infinetly many solutions for p"<<endl;
	cout<<endl;
	return 0;
}
No Kevin, I am not working on a similar project. I was just trying to answer this thread. I wrote the code above and tried to verify it before posting here. It was then that I encountered this error.

However, I found my mistake (a really silly one). I declared P and Q with wrong size.

Sorry Guys.
Last edited on
closed account (48T7M4Gy)
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
#include <iostream>

const int QUADRATIC_LIMIT = 2;

void add( double [], double []);
void display( double []);
double* sum( double [], double []);

int main()
{
    double a[] = {2, 3};
    double b[] = {4, -7};
    double c[] = {99, -99};
    
    double total[QUADRATIC_LIMIT] = {0};
    display(total);
    
    add(total, a);
    display(total);
    
    add(total, b);
    display(total);
    
    add(total, c);
    display(total);
    
    double* output = sum(a,b);
    display(output);
    
    
    return 0;
}

void display( double coefficients[])
{
    for (int i = 0; i < QUADRATIC_LIMIT; i++)
    {
        std::cout << coefficients[i] << ' ';
    }
    std::cout << '\n';

}

void add( double result[], double additional[])
{
    for(int i = 0; i < QUADRATIC_LIMIT; i++)
    {
        result[i] += additional[i];
    }
    
    return;
}

double* sum( double aa[], double bb[])
{
    double* the_sum = new double[QUADRATIC_LIMIT];
    
    for(int i = 0; i < QUADRATIC_LIMIT; i++)
    {
        the_sum[i] = aa[i] + bb[i];
    }
    
    return the_sum;
}


0 0 
2 3 
6 -4 
105 -103 
6 -4 
Program ended with exit code: 0
Last edited on
@eliiofaddoul Your code still has the issue I mentioned on line 44. Line 93 (with the example I gave would look more like Quadratic r = p.add(q); //this resembles r = p + q I recommend changing the private abc variables to an array named cefficients. (I'm going to go edit my other post arr -> coefficients) Or, change the add example to not use a for loop: return Quadratic(a + other.a, b + other.b, c+ other.c);
Last edited on
closed account (48T7M4Gy)
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
#include <iostream>

class quadratic
{
private:
    double a, b, c;
public:
    quadratic(double aa = 0, double bb = 0, double cc = 0)
    {
        a = aa;
        b = bb;
        c = cc;
    }
    
    double evaluate(double x)
    {
        return a * x * x + b * x + c;
    }
    
    void print()
    {
        std::cout << a << "x^2 + " << b << "x + " << c << std::endl;
    }
    
    double derivative(double x)
    {
        return 2 * a * x + b;
    }
    
    quadratic sum( quadratic rhs)
    {
        
        this -> a += rhs.a;
        this -> b += rhs.b;
        this -> c += rhs.c;
        
        return *this;
    }
};

int main()
{
    quadratic p(1,5,3), q(1,2,1);
    
    std::cout << "p: "; p.print();
    std::cout << "q: "; q.print();
    
    quadratic r = p.sum(q);
    std::cout << "r = p+q: " ;
    r.print();
    
    return 0;
}

p: 1x^2 + 5x + 3
q: 1x^2 + 2x + 1
r = p+q: 2x^2 + 7x + 4
Program ended with exit code: 0
Last edited on
closed account (iE8M92yv)
The original question, although slightly oddly phrased, seemed to imply that although sum was a member function, r should be another object than p and q (although why it needs to originate with zero coefficients I don't know). So I changed kemort's member function above and changed the call to
r.sum(p,q)

Also, no reason to restrict attention to coefficients which are doubles - could also be int, float, complex(?), or any mathematical field with appropriate +,-,*,= operators defined. Here's a suggested template version:
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
#include <iostream>

template <class T> class quadratic
{
private:
    T a, b, c;
public:
    quadratic(T aa = 0, T bb = 0, T cc = 0)
    {
        a = aa;
        b = bb;
        c = cc;
    }
    
    T evaluate(T x)
    {
        return a * x * x + b * x + c;
    }
    
    void print()
    {
        std::cout << a << "x^2 + " << b << "x + " << c << std::endl;
    }
    
    T derivative(T x)
    {
        return 2 * a * x + b;
    }
    
    quadratic sum( quadratic lhs, quadratic rhs)
    {       
        a = lhs.a + rhs.a;
        b = lhs.b + rhs.b;
        c = lhs.c + rhs.c;
    }
};




int main()
{
    quadratic <double> p(1,5,3), q(1,2,1), r(0,0,0);
    
    std::cout << "p: "; p.print();
    std::cout << "q: "; q.print();
    
    r.sum(p, q);
    std::cout << "r = p+q: " ;
    r.print();
    
    return 0;
}
Topic archived. No new replies allowed.