By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,002 Members | 2,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,002 IT Pros & Developers. It's quick & easy.

question with quadratic equation

P: 2
hi, i'm a freshman with c++,i got a question from my assignment,does someone could give me some ideas for that.

Given the following prototype of a function that solves the [possibly complex] roots of a quadratic equation of the form ax2 + bx + c:
Ret_type fn_name(formal param list);
explain the nature of each of the parameter-passing mechanisms used and give a suitable example of code to call the function fn_name. Your answer should contain all necessary variable declarations and any initialisation of such.
Jun 16 '06 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
hi, i'm a freshman with c++,i got a question from my assignment,does someone could give me some ideas for that.

Given the following prototype of a function that solves the [possibly complex] roots of a quadratic equation of the form ax2 + bx + c:
Ret_type fn_name(formal param list);
explain the nature of each of the parameter-passing mechanisms used and give a suitable example of code to call the function fn_name. Your answer should contain all necessary variable declarations and any initialisation of such.
Firstly if it's a home work assignment you should have an attempt first at doing it yourself and bring your answer here for us to check it and suggest improvements.

Secondly if this is really the wording of the question then I am not sure how to answer it, specifically

"explain the nature of each of the parameter-passing mechanisms used"

There are no parameter-passing mechanisms used because no functions have actually been defined, all that's been given is a rather vague and not entirely accurate version of a function prototype.

I assume that you do know the equation for solving a quadratic equation? If not look it up on the web "quadratic equation solution" in google should do the trick.
Jun 16 '06 #2

P: 2
thanx for ur suggestion, i try do some code following, could u help me check it and give me some advices.

#include "stdafx.h"
# include <iostream>
# include <math.h>
using namespace std;
double qUeq(double a, double b, double c);
int main()
{
double a,b,c;
cout<<"Input a,b,c(b*b-4*a*c>0):";
cin>>a>>b>>c;
qUeq(a,b,c);
return 0;
}
double qUeq(double a, double b, double c)
{
double x1,x2;
double sqrtVal=sqrt(b*b-4*a*c);
if (b*b-4*a*c>0)
{
x1=(-b+sqrtVal)/(2*a);
x2=(-b+sqrtVal)/(2*a);
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl;
}
else
cout<<"Please input the correct numbers,and try again.:)"<<endl;
return 0;
}
Jun 18 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
OK tip number 1, when posting code if you use [code\ ... [/code\ round the code you're posting it will preserve the whitespace (indenting) and make it easier to read.

[html\ and [php\ also exist for those types of file.

OK some general comments, if you enforce the condition (b*b-4*a*c>0) then you will not complete the assignment correctly because it has asked you to solve the equation even for complex roots and complex roots occur when (b*b-4*a*c<0). If fact you wont even solve all the equations with non-complex roots because when (b*b-4*a*c==0) you will have 1 non-complex root but you have excluded it. This includes simple equations like y=(x+1)(x+1) which has the single root x = -1 but for which (b*b-4*a*c==0).

Are you familiar with j = sqrt(-1) (or i if you are a mathamatition), clearly you can not actually calculate this number but coupled with sqrt(a*b) = aqrt(a) * sqrt(b) it allows you to write sqrt(-a) = j * sqrt(a).

This is where the complex root solving comes in because once you have complex numbers of the form x + y*j (i.e. 3 + 4j) you can then solve the quadratic equation for any values of a, b and c.

When (b*b-4*a*c<0) this allows you to treat

-b +- sqrt(b*b-4*a*c) / 2a

as

-b / 2a +- j * sqrt(-(b*b-4*a*c)) / 2a

which is a complex solution

Right and now my comments on your code in bold below

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. # include <iostream> 
  3. # include <math.h> 
  4. using namespace std; 
  5. double qUeq(double a, double b, double c);
  6. /* You could declare qUeq as static since the code is all in 1 file */
  7. int main() 
  8.     double a,b,c;
  9.     cout<<"Input a,b,c(b*b-4*a*c>0):"; 
  10.     cin>>a>>b>>c;
  11. /* OK but have you considered what will happen if some 
  12.    idiot inputs a non-number for one of the values like for 
  13.    instance "a" */
  14.     qUeq(a,b,c); 
  15.     return 0;
  16. double qUeq(double a, double b, double c)
  17. /* Is there any point in returning double from this 
  18.    function when you only ever return 0.  You could return 
  19.    a bool status, true for OK false for error 
  20.    (for instance if a = 0) */
  21. {
  22.     double x1,x2;    
  23.     double sqrtVal=sqrt(b*b-4*a*c);
  24. /* You have calculated sqrt(b*b-4*a*c) before you 
  25.    have checked if it is possible to calculate it */
  26.     if (b*b-4*a*c>0)
  27.     {
  28.         x1=(-b+sqrtVal)/(2*a);
  29.         x2=(-b+sqrtVal)/(2*a);
  30. /* You have accidentally calculated the same value 
  31.    twice when you wanted to calculate the + and minus 
  32.    value.  Not also that sqrtVal could be 0 in which case 
  33.    you have only 1 root.  Also if a=0 then these lines will 
  34.    not work because you will try to divide by zero.*/
  35.         cout<<"x1="<<x1<<endl;
  36.         cout<<"x2="<<x2<<endl;
  37.     }
  38.     else 
  39.         cout<<"Please input the correct numbers,and try again.:)"<<endl;
  40.     return 0;
  41. }
On the whole not a bad first try, you need to handle the complex case though to properly complete your assignment.

You should create your self a test of test values to test your program with.

Include at least 1 equation with 2 real root, 1 equation with 1 real root and 1 equation with 2 complex roots.
Jun 18 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.