Quadratic Equation


So this is an exercise for a C++ class and i got a task:

How many real different solutions does an equation:
ax2+bx+c = 0 has.

Input is: -10^6<=a,b,c>=10^6 it means a,b and c can be 0.

I need to output amount of the solutions. If there are infinite solutions i should output -1.

This is my code and however it fails i tried looking in the internet about these equations however couldnt find much.

Any hint would be great

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>

using namespace std;



int main()
{
	long long a, b, c;
	cin >> a >> b >> c;
	set<long> talpa;
	
	for (int i = -100000; i <= 100000; i++) {
		if ((a * i *i) + (b*i) + c == 0) {
				talpa.insert(i);
				
		}
			
		if (talpa.size() > 2) {
			break;
		}

	}

	if (talpa.size() > 2)
		cout << -1 << endl;
	else
		cout << talpa.size() << endl;



	return 0;
}
Last edited on
Given the coefficients of a quadratic equation, the discriminant can be calculated using this formula:
 
Δ = b2 - 4*a*c

Just substitute the inputted coefficients and check the discriminant to ensure that it has real roots. Obviously, if a is 0, then it's not a quadratic equation, thus the formula will not hold.
Then you can just calculate the roots as follows
 
x = (-b ± √Δ) / (2*a)
I do understand quadratic part very well however the exercise is put that way that the equation may i believe be not a quadratic a simple one and i need to understand how do i check if i it has infinite solutions

i need to understand how do i check if i it has infinite solutions

As far as I know, quadratic functions can only have up to 2 solutions, not infinite. If you look at the graph of a parabola, it can only ever hit the x-axis a maximum of 2 times. It will never hit it an infinite number of times. Therefore, quadratic functions can never have infinite roots.
Well in this case a can be 0 so once a is 0 the equation is no longer quadratic so that is why i am asking this question
The only way that you can get an infinite number of solutions is if a, b and c are all zero. Then every value of x satisfies it!

A more pertinent question might be when it has no solutions.

Your original post says:
How many real different solutions does ...

If a is not 0, then it has 2 different real solutions if the discriminant is strictly positive, 1 repeated real solution if the discriminant is 0, and no real solutions if the discriminant is less than 0.

If a is 0, then it has 1 real solution if b is not equal to 0. If a = b = 0 then it has an infinite number of solutions if c=0 and no solutions if c is not zero.

Now there's a few 'if' statements for you!
Last edited on
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>

using namespace std;



int main()
{
	long long a, b, c;
	cin >> a >> b >> c;
	set<long long> talpa;
	
	if (a == 0 && b == 0 && c == 0) {
		cout << "-1" << endl;
	}
	else {
		long long d = (b*b) - (4 * a * c);
		if (d > 0)
			cout << "2" << endl;
		else if (d == 0)
			cout << "1" << endl;
		else
			cout << "0" << endl;
	}



	return 0;
}


Yeaah i modified this code a bit however i am failing some test cases and i wonder why
You aren't allowing for all cases. What if a=0, but b and c aren't zero?

I think you need some nesting of if statements when a = 0.
Oooh so that's what i was missing.. I was caught up in an idea that maybe there was somehow more times when it has infinite solutions.. Now i got it completely. Thank you very much!
Topic archived. No new replies allowed.