Pass by Reference

Objective is to have a program that will calculate the roots of a 2nd degree polynomial using pass by reference. I am getting 0.00 for every answer, i don't think it is my equation. I am pretty sure i am using pass by reference wrong, but not sure how.

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

using namespace std;

void find_root(double&, double&, double&);
int main()

{
	double a = 0;
	double b = 0;
	double c = 0;
	double disc = 0;
	string ans = "yes";
	double x1 = 0;
	double x2 = 0;


	while(ans == "yes")

	{

	cout<<"This program will calculate the roots of a 2nd degree polynomial"<<endl
		<<"Please input the values for A, B, and C"<<endl<<endl;
	cin>>a>>b>>c;
	system("cls");

	if (disc < 0)
	{
	
		cout<<"The roots are "<<fixed<<setw(7)<<setprecision(2)<<(-b/(2*a))<<" +/- "
			<<fixed<<setw(7)<<setprecision(2)<<(sqrt(abs(disc))/2*a)<<" i"<<endl<<endl;

	}
	else 
		
		if(a ==0)
			cout<<"That is a line, not a 2nd degree polynomial."<<endl<<endl;

		else
		{
			cout<<"The roots are "<<fixed<<setw(7)<<setprecision(2)<<x1<<" and "
				<<fixed<<setw(7)<<setprecision(2)<<x2<<endl<<endl;


		}

		cout<<"Would you like to run this again? yes for yes, any other key for no"<<endl<<endl;
			cin>>ans;
	}
	
		system("pause");
	return 0;
}

void find_root(double& a, double& b, double& c)
{

	
	double disc =  ((b*b)-(4*a*c));
	

	double x1 = (-b + sqrt(abs(disc)))/(2*a);
	double x2 = (-b - sqrt(abs(disc)))/(2*a);


	


	return;
}
Besides the fact I didn't call the function...
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
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>

using namespace std;

void find_root(double&, double&, double&);
int main()

{
	double a = 0;
	double b = 0;
	double c = 0;
	double disc = 0;
	string ans = "yes";
	double x1 = 0;
	double x2 = 0;


	while(ans == "yes")

	{

	cout<<"This program will calculate the roots of a 2nd degree polynomial"<<endl
		<<"Please input the values for A, B, and C"<<endl<<endl;
	cin>>a>>b>>c;
	system("cls");

	find_root(a,b,c);

	if (disc < 0)
	{
	
		cout<<"The roots are "<<fixed<<setw(7)<<setprecision(2)<<(-b/(2*a))<<" +/- "
			<<fixed<<setw(7)<<setprecision(2)<<(sqrt(abs(disc))/2*a)<<" i"<<endl<<endl;

	}
	else 
		
		if(a ==0)
			cout<<"That is a line, not a 2nd degree polynomial."<<endl<<endl;

		else
		{
			cout<<"The roots are "<<fixed<<setw(7)<<setprecision(2)<<x1<<" and "
				<<fixed<<setw(7)<<setprecision(2)<<x2<<endl<<endl;


		}

		cout<<"Would you like to run this again? yes for yes, any other key for no"<<endl<<endl;
			cin>>ans;
	}
	
		system("pause");
	return 0;
}

void find_root(double& a, double& b, double& c)
{

	
	double disc =  ((b*b)-(4*a*c));
	

	double x1 = (-b + sqrt(abs(disc)))/(2*a);
	double x2 = (-b - sqrt(abs(disc)))/(2*a);


	


	return;
}
closed account (48T7M4Gy)
You need to pass x1 and x2 to the function as well.

So you need:

void find_root(double&, double&, double&, double&, double&); ///

find_root(a, b, c, x1, x2); ///

1
2
3
4
5
6
7
void find_root(double& a, double& b, double& c, double &x1, double &x2) ///
{
	double disc = ((b*b) - (4 * a*c));
	x1 = (-b + sqrt(abs(disc))) / (2 * a);///
	x2 = (-b - sqrt(abs(disc))) / (2 * a);///
	return;
}
Last edited on
Thank you, it works like a charm.
Topic archived. No new replies allowed.