|
-
Dec 8th, 2003, 08:16 PM
#1
Thread Starter
Hyperactive Member
Any suggestion on Polynomial class design?
Hello everyone, I'm having troubles find the least messest way to make this program. I'm suppose to create a Polynomial class that can output the following:
Coefficients of the quadratics you created above, using the accessor functions, that is the get... functions.
How many roots each of them have
Real roots of each quadratic, if any
The result of the polynomial addition and subtraction using the overloaded operators + and -. Print out at least the sum and the difference of the quadratics given below
Q1+Q2
Q2+Q3
Q3+Q4
Q3+Q5
Q2-Q5
Q5-Q3
Q4-Q2
I got this program to work fine, it just looks messy. He told us:
Write two more member functions to return the real roots of the quadratic expression. If there are two distinct real roots one of the functions returns the smaller of the two roots, and the other returns the larger of the two roots. If every value of x is a real root, the both functions should return 10000
This makes no sense to me, why would you want 2 seperate member functions? it seems redudant to me. But my way isn't any better, I just use 1 member function, called smallerOfRoots, which finds the smaller of the roots and takes a reference parm to assign the other root.
the calling statement looks like this:
Code:
cout <<"Q2's real roots :" << Q2.smallerOfRoots(Q2.numOfRoots(),biggerRoot2) << endl;
cout <<"Q2's other root: " << biggerRoot2 << endl;
I was thinking about making another member variable, called numOfRoots, so the user doesn't have to call the Q1.numOfRoots. But thats the only cleaner thing I can think of at the moment. I was wondering if there was any way to find out if the function can see if the calling object has 2 real roots, if it does, some how print the small root and big root, but if it doens't, then jsut print the one root. the problem is, he wants us to make 2 seperate functions, why would you want to do that? then the user would have to call the smallerOfRoots function, then call the biggerOfRoots function, when the user doesn't even know if the quadratic will even have 2 roots at the time. any idea's?
Thanks.
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Dec 9th, 2003, 11:53 AM
#2
You could make a getRoots() function that returns a std::pair<float,float>, containing the two roots. The lower in returnvalue.first, the higher in returnvalue.second.
Last edited by twanvl; Dec 9th, 2003 at 12:48 PM.
-
Dec 9th, 2003, 12:33 PM
#3
Thread Starter
Hyperactive Member
that sounds like it would work but we never used that function, could you give me an example?
I modified my find roots function to do the following:
Code:
void Polynomial::findRoots()
{
switch(numOfRoots)
{
case 0: cout<<"No real roots"<<endl;
break;
case 1: cout<<"Real Root: "<<(-c/b)<<endl;
break;
case 2: cout <<"Bigger Root: " << (-b+sqrt(b*b-4*a*c))/(2*a)<<endl;
cout <<"Smaller Root: " << (-b-sqrt(b*b-4*a*c))/(2*a)<<endl;
break;
case 10000: cout<<"Infinitley many roots"<<endl;
}
}
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Dec 9th, 2003, 12:53 PM
#4
Code:
#include <utility>
#inlcude <cmath>
#inlcude <exception>
std::pair<float,float> polynomial::findRoots() const
{
float d=b*b-4*a*c;
if (d>0) {
return std::pair<float,float>(
(-b-std::sqrt(d))/(2*a),
(-b+std::sqrt(d))/(2*a)
);
} else {
// no real solutions
throw std::exception("no solutions");
}
}
-
Dec 9th, 2003, 03:04 PM
#5
Thread Starter
Hyperactive Member
excellent! thanks
C¤ry Sanchez
Computer Science/Engineering
@ Penn State
IBM.zSeries Intern
Mandriva 2007
-
Dec 9th, 2003, 03:08 PM
#6
Monday Morning Lunatic
That's not general for any polynomial though, only a 2nd-order one (quadratic).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Dec 9th, 2003, 03:30 PM
#7
transcendental analytic
you can find the roots upto 4'th order polynominals rationally (cubic and quartic)
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|