473,395 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

question with quadratic equation

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 7569
Banfa
9,065 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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

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

Similar topics

1
by: Christian O'Connell | last post by:
Hello - apologies if this has been asked before and already answered. This is my first question and I have already unsuccessfully searched Google groups and the on-line help files for the answer. ...
1
by: tedla | last post by:
i want a fragment of code that accepts the coefficient of quadratic equation from user and calculate the eqt'n.
5
by: mulhadhevi | last post by:
Can anyone tell me how to write a program to find the roots of the quadratic equation using switch case.
2
by: DaRok28 | last post by:
// Program Description: // This program solves quadratic equations to find their roots. This // program takes values of a, b, and c as input and outputs the root(s). // The user can repeat the...
4
by: nbkreddy | last post by:
write a program to find the roots of the quadratic equation using switch statement
2
by: beambohus | last post by:
please how do i solve this........ To write an algorithm to solve quadratic equation using almighty formular. please your respond will very much appreciated
2
by: ioannoual | last post by:
Hi...I 'am new in C and I want a program that solves a quadratic equation!! I try something by my self to write some code about that but I want you to help me writing a new one that will work!! ...
1
by: bbench123 | last post by:
Make a program that will ask for values of a quadratic equation (ax2+bx+c). the program must determine the roots of the equation using the quadratic equation determinants to distinguish if the roots...
1
by: candacefaye1 | last post by:
1. write a C++ program to decide if the coefficients of a quadratic equation have real roots. The three choices will be to write the message “zero divide” when A is zero, write the message “no real...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.