Questions about functions and polynomials

I have to write a program that will find the roots of a 2nd degree polynomial. I have to use a function, use pass by value, and cout everything in the main function. I am having trouble knowing how to format the equation into the function and how to call the function twice for each root. This is what i have so far.

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
  #include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;


double find_root (double, double, double);
int main()
{
	double a;
	double b;
	double c;
	double root1;
	double root2;
	double x1;
	double x2;
	double disc = ((b*b) - (4*a*c));

	cout<<"This function will find the roots of a 2nd degree polynomial"
		"Please input the coefficients in the form Ax^2 + Bx + C"<<endl<<endl;
	cin>>a>>b>>c;

	root1 = find_root(x1, x2);







	system("Pause");
	return 0;
}

double find_root(double a, double b, double c)
{
	
	static double x1;
	static double x2;
	double disc =  ((b*b)-(4*a*c));


	x1 = (-b + sqrt(b*b - 4*a*c))/(2*a);
	x2 = (-b - sqrt(b*b - 4*a*c))/(2*a);

	return 0;

}
in like 24 i changed it to root1=find_root(a,b,c);
You are basically there. Why not just have two cout << statements in your find_root function? That will definitely work. Also, you should use double disc in that function since you have initialized it properly. Just put it in the x1 = and x2 = expressions.
I'm confused, what do you mean by use double disc in that function? and put x1 = / x2 = where? I am not supposed to "cout" anything from my find_root function, only from main0. Here is my updated code, I think i almost have it. I have to format it for the imaginary roots, but am i returning x1 then x2? I can't tell right now.

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<iomanip>
#include<cmath>
#include<string>
using namespace std;


double find_root (double, double, double);
int main()
{
	double a = 0;
	double b = 0;
	double c = 0;
	double root1;
	double root2;
	double disc = ((b*b) - (4*a*c));

	cout<<"This function will find the roots of a 2nd degree polynomial"
		"Please input the coefficients in the form Ax^2 + Bx + C"<<endl<<endl;
	cin>>a>>b>>c;

	root1 = find_root(a,b,c);
	root2 = find_root(a,b,c);


	cout<<root1<<endl;
	cout<<root2<<endl;



	system("Pause");
	return 0;
}

double find_root(double a, double b, double c)
{
	
	static double x1;
	static double x2;
	double disc =  ((b*b)-(4*a*c));


	x1 = (-b + sqrt(b*b - 4*a*c))/(2*a);
	x2 = (-b - sqrt(b*b - 4*a*c))/(2*a);

	return x1;

}

As you have it right now, your root1 and root2 are the same, since find_root is only returning x1. Are you allowed to add a 4th argument to your find_root function? If so, you could do something like this:

1
2
3
4
5
6
7
8
9
double find_root(double a, double b, double c, int sign)
{

      double x;
      double disc = b*b-4*a*c;
      
      x = (-b+sign*sqrt(disc))/(2*a);  //This is what I meant in my first post... disc wasn't doing anything before
      return x;
}


The disadvantage to the above code is that the user should only be entering +1 or -1 for the sign variable, but is free to enter whatever they want. But, there are plenty of ways you could modify that to make it easier to use.
how do i return x1 and x2 to the main function and cout them there, i guess is my main question at the moment.
I am only supposed to use 3 arguments, unfortunately. I am able to use a static variable(s) to return the two values, i am just not quite sure how to do that because we just went over it.
My prof said we should be either using static variables if we want, or just call the function twice to send both roots back, i am unsure on how to do both is the problem.
Something like ?
1
2
3
4
5
6
double r1=//something
static double r2=//something
if(somethings happen)
  return r1
else
  return r2
yes, but how would that return both roots? Wouldn't that only return one OR the other?
I'm not the most experienced programmer in the world, so maybe this isn't the best method, but you could do the following:

1
2
3
4
5
6
7
8
9
10
double find_root(double a, double b, double c)
{
      double x;
      double disc = b*b - 4*a*c;
      static int sign = 1;
      sign = -sign;       //Change sign for second root

      x = (-b + sign*sqrt(disc))/(2*a);
      return x;
}


You'll have to call the find_root function twice for each set of parameters a, b, and c; each call to the find_root function flips the sign of the +/- in the quadratic formula.
Ok, that should work. How exactly do i call the function twice in my main?
You already are in the second example you posted.
Last edited on
Oh, ok. I think i am still getting the same answers for root1 and root2
What were your a,b,c? This will happen for some choices.
Well, here is my updated program. Still a few bugs. Such as: my while loop doesn't return to the beginning, after input "ans", it just stop the program. Also, still getting root1 = root2 and also i am sometimes getting weird roots like "-1.ind#". where am i going wrong?


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
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;


double find_root (double, double, double);
int main()
{
	double a = 0;
	double b = 0;
	double c = 0;
	double root1;
	double root2;
	double disc = ((b*b) - (4*a*c));
	string ans = "yes";


	while (ans == "yes" || ans == "Yes")

	{

	cout<<"This function will find the roots of a 2nd degree polynomial"
		"Please input the coefficients in the form Ax^2 + Bx + C"<<endl<<endl;
	cin>>a>>b>>c;

	root1 = find_root(a,b,c);
	root2 = find_root(a,b,c);

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

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

		else
		{
			cout<<"The roots are "<<root1<<" and "<<root2<<endl;


		}

	cout<<"Would you like to run this again? Yes or yes for yes, anything else for no."<<endl;
	cin>>ans;

	system("Pause");
	return 0;

	}
}

double find_root(double a, double b, double c)
{
	
	static double x;
	static int sign;
    sign = -sign;
	double disc =  ((b*b)-(4*a*c));


	x = (-b + (sign)*sqrt(b*b - 4*a*c))/(2*a);


	return x;

}
fixed the while loop, still getting the same answer for every input of a,b,c. except a =0, that is working.
The while doesn't keep going because you put return 0; inside the loop. You want it outside. You also need to insert an additional << in line 25. The other stuff isn't working because you didn't initialize static int sign. Right now, it by default initializes to 0, which will obviously produce the wrong result.

Hope this works!
It did! Just one more problem, probably simple, but i am getting "501...BB.." before my cout of the complex roots. Also, when a "0" is input for b, it seems to ignore the setprecision and it couts "0.00000" instead of just "0.00".


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
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;


double find_root (double, double, double);
int main()
{
	
	double a = 0;
	double b = 0;
	double c = 0;
	double root1;
	double root2;
	double disc = 0;
	string ans = "yes";


	while (ans == "yes" || ans == "Yes")

	{

	cout<<"This function will find the roots of a 2nd degree polynomial\n"
		<<"Please input the coefficients in the form Ax^2 + Bx + C"<<endl<<endl;
	cin>>a>>b>>c;
	system("Cls");

	root1 = find_root(a,b,c);
	root2 = find_root(a,b,c);
	disc = ((b*b) - (4*a*c));

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

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

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


		}

	cout<<"Would you like to run this again? Yes or yes for yes, anything else for no."<<endl;
	cin>>ans;

	}

	system("Pause");
	return 0;

	
}

double find_root(double a, double b, double c)
{
	
	static double x;
	static int sign = 1;
	double disc =  ((b*b)-(4*a*c));
	sign = -sign;

	 x = (-b + (sign)*sqrt(abs(disc)))/(2*a);


	return x;

}
It seems like setw sets the number of characters to output; setprecision sets the decimal precision you would like to output. Consider 0.00923. Setprecision of two should output as 0.0092.
Topic archived. No new replies allowed.