Quadratic Formula Program

I have to create this program for class, a quadratic formula program that will output three results using an I/O file, "The roots are complex," "The roots are real," and "The formula has one real root." For the second output, I must output the roots. I get the proper result (the type), but I am not getting the proper roots.

Thanks

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
#include <cmath>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{	
	ifstream inData;
	ofstream outData;
	
	float a; //coefficent of x^2
	float b; //
	float c; //constant
	float discrminant;
	double TERMA;
	double TERMB;

	inData.open("input.txt");
	outData.open ("output.txt");
	
	inData >> a >> b >> c;
	
	outData << fixed <<showpoint <<setprecision(3);
	
	discrminant =  pow(b, 2) - (4*a*c);
	TERMA = b - (  (   pow(b, 2)   -   (4*a*c)  ) / (2 * a)  ); 
	TERMB = b + ((pow(b, 2) - (4*a*c)) / (2 * a));
	
	if (discrminant = 0)
	{
		outData << "The roots are repeated.";
		outData << TERMA;
	}
	else if(discrminant > 0)
	{
		outData << "The root are real.";
		outData << TERMA << "\n";
		outData << TERMB << "\n";
	}
	else if (discrminant < 0)
	{
		outData << "The roots are complex.";
	}
	
	inData.close();
	outData.close();
	
	return 0;	 
}
I thnk you need sqrt( pow(b, 2) - (4*a*c)
We are not allowed to use sqrt. The professor was like, "It is not in the codding prompt." Believe me, that would make my life easier.
I found this mathforum.org/dr.math/faq/faq.sqrt.by.hand.html maybe you can create a function that based on it
Last edited on
>>"We are not allowed to use sqrt. The professor was like, "It is not in the codding prompt." Believe me, that would make my life easier. "

If you can't use the square root function, you are not using the Quadratic Formula. That doesn't make sense. I don't think it can be done without the square root function.

Also, a couple things caught my eye:
(i) you compute the discriminant, but then you don't use it on the next two lines, in the computation of TERMA and TERMB. Instead of writing all that out, you could have just used your value of discriminant.
(ii) I think you have an opening bracket in the wrong place. The 2a term in the denominator is supposed to divide everything; as it is now, it seems to be just dividing the second term.
I don't think it can be done without the square root function.
there are other ways to get the square root of a number. Also, you can simply raise to the power of .5 :P (same as square rooting without creating your own method like the Babylon method or some other).
well, just use pow() to get the number raised to the power 0.5 instead of sqrt().

[as an aside, I'm always amazed at how many people prefer to call a function rather than just write b*b for b2]
BTW, you have changed the output from the assignment.
You say "The roots are repeated."

No they are not. There is only one root when the discriminant is zero. Hence, "The formula has one real root."


Also, go ask your professor what exactly he means by "it's not in the coding prompt." You cannot compute the quadratic formula without calculating the square root.
I emailed my professor and he told me just to use pow(discriminant,0.5)
LOL. In other words, use pow() to compute the square root.

That works, though. Good job for emailing the prof and asking questions. Too few people do that.
Thanks, I was finally able to solve my problem. It was the problem regarding the square root and misplacing parenthesis. Thanks for all you help.
"there are other ways to get the square root of a number. Also, you can simply raise to the power of .5 :P (same as square rooting without creating your own method like the Babylon method or some other). "

True. One way or the other, you are computing the square root of the discriminant. I read the OP's post and understood it to mean the square root could not be computed, not just that particular function. I thought, how else to do it? Computers can't do it by inspection!?

In any case, I am glad to see it appears to have worked out for the OP.
Topic archived. No new replies allowed.