quadratic equation using function

i m supposed to write a program to solve a quadratic equation. output should look like as follows:
coefficients a1=1,b=2,c=5,yield s1=-1+2i and s2=-1-2i
this is what i have done
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
#include<iostream>
#include<cmath>
using namespace std;
bool QuadraticFormula(float a, float b, float c, float r1, float r2,float i1,float i2)
{
	float d=(b*b-4*a*c);
	if ( d<0)
	{
		return false;
		r1=r2=-b/(2*a);
		i1= sqrt(-d);
		i2= -sqrt(-d);
	}
	else 
	{
		i1=i2=0;
		r1=(-b+sqrt(d))/(2*a);
		r1=(-b-sqrt(d))/(2*a);
		return true;
	}
}
int main()
{
	float a,b,c,r1,r2,i1,i2;
	cout<<"enter a:";
	cin>>a;
	cout<<endl;
	cout<<"enter b:";
	cin>>b;
	cout<<endl;
	cout<<"enter c:";
	cin>>c;
	cout<<endl;
	cout<<"coefficients a="<<a<<" b="<<b<<" c="<<c;
	if(QuadraticFormula(a,b,c,r1,r2,i1,i2))
	{
		cout<<"yields"<<endl;
		cout<<"s1="<<r1<<"+i"<<i1<<endl;
		cout<<"s2="<<r2<<"+i"<<i2<<endl;
	}
	else
	{
		cout<<"yields"<<endl;
		cout<<"s1="<<r1<<"+i"<<i1<<endl;
		cout<<"s2="<<r2<<"+i"<<i2<<endl;
	}
}

help me out what am i doing wrong here!!






Last edited on
Hi,

l9: return false should be at l12 because return end the function so the calculs below are not done.

You should use references or pointers for your function QuadraticFormula. In your case the results you want r1 r2 i1 or i2 exist only in your function (it's a problem of scope)

See here : http://www.cplusplus.com/doc/tutorial/pointers/

Good luck

Last edited on
okay i did replaced as you. but no success. output is :
s1=0+0i, s2=0+0i. always.
i would be great help if you could elaborate this program with pointers. i m kinda noob here!!
Last edited on
anyone how to use pointers for this program??
okay i figured it out here is my final code if anyone could find it useful:
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
#include<iostream>
#include<cmath>
using namespace std;
bool QuadraticFormula(float a, float b, float c, float* r1, float* r2,float* i1,float* i2)
{
	float d=(b*b-4*a*c);
	if ( d<0)
	{
		*r1=*r2=-b/(2*(a));
		*i1= sqrt(-d)/(2*a);
		*i2= -sqrt(-d)/(2*a);
		return false;
	}
	else 
	{
		*i1=*i2=0;
		*r1=(-b+sqrt(d))/(2*(a));
		*r2=(-b-sqrt(d))/(2*(a));
		return true;
	}
}
int main()
{
	float a=0.0f,b=0.0f,c=0.0f,r1=0.0f,r2=0.0f,i1=0.0f,i2=0.0f;
	cout<<"enter a:";
	cin>>a;
	cout<<endl;
	cout<<"enter b:";
	cin>>b;
	cout<<endl;
	cout<<"enter c:";
	cin>>c;
	cout<<endl;
	cout<<"coefficients a="<<a<<" b="<<b<<" c="<<c;
	if(QuadraticFormula(a,b,c,&r1,&r2,&i1,&i2))
	{
		cout<<"yields"<<endl;
		cout<<"s1="<<r1<<"+"<<i1<<"i"<<endl;
		cout<<"s2="<<r2<<"+("<<i2<<")i"<<endl;	}
	else
	{
		cout<<"yields"<<endl;
		cout<<"s1="<<r1<<"+"<<i1<<"i"<<endl;
		cout<<"s2="<<r2<<"+("<<i2<<")i"<<endl;
	}
	cin.get();
	return 0;
}








talk about over complicating things, rofl. I did this with only 3 variabls: a, b, and c! lol
Oh, jeeze, I haven't seen a quadratic equation since high school. Just reminds me of all the things I've forgotten, guess I should get back into practice with math.
Thanks for the flashback!
Topic archived. No new replies allowed.