432,002 Members | 2,194 Online + 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
3 Replies

 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 # include 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="<

 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 #include "stdafx.h" # include   # include   using namespace std;  double qUeq(double a, double b, double c); /* You could declare qUeq as static since the code is all in 1 file */ int main()  {      double a,b,c;     cout<<"Input a,b,c(b*b-4*a*c>0):";      cin>>a>>b>>c; /* OK but have you considered what will happen if some     idiot inputs a non-number for one of the values like for     instance "a" */     qUeq(a,b,c);      return 0; }  double qUeq(double a, double b, double c) /* Is there any point in returning double from this     function when you only ever return 0.  You could return     a bool status, true for OK false for error     (for instance if a = 0) */ {     double x1,x2;         double sqrtVal=sqrt(b*b-4*a*c); /* You have calculated sqrt(b*b-4*a*c) before you     have checked if it is possible to calculate it */     if (b*b-4*a*c>0)     {         x1=(-b+sqrtVal)/(2*a);         x2=(-b+sqrtVal)/(2*a); /* You have accidentally calculated the same value     twice when you wanted to calculate the + and minus     value.  Not also that sqrtVal could be 0 in which case     you have only 1 root.  Also if a=0 then these lines will     not work because you will try to divide by zero.*/         cout<<"x1="< 