473,395 Members | 1,558 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.

quadratic equation program

22
Hello, I am new to C++ and i have tried for several hours to make a program my teacher has given me as a lab. Here is the Lab question:

the roots of the quadratic equation ax^2 + bx + c = 0, a cannot equal 0 are given by the following formula -b + or - square root of (b^2 - 4ac) / 2a.

If b^2 - 4ac = 0 then equation has a single root. if b^2 - 4ac > 0 then equation has two real roots. if b^2 - 4ac < 0 then equation has two complex roots. Write a program that prompts the user to input the value of a,b,c, and outputs the type of roots of the equation. Furthermore, if b^2 - 4ac >= 0 the program should output the roots of the quadratic equation.

This is the coding i have so far. I am getting a few errors:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int _tmain(int argc, _TCHAR* argv[])
  8. {
  9.     double a != 0;
  10.     double b;
  11.     double c;
  12.     double x;
  13.     double root1;
  14.     double root2;
  15.     double descrim;
  16.  
  17.     cout << "Please input the value of a: " << endl;
  18.     cin >> a;
  19.     cout << "Please input the value of b: " << endl;
  20.     cin >> b;
  21.     cout << "Please input the value of c: " << endl;
  22.     cin >> c;
  23.  
  24.     descrim = pow(b,2) - (4 * a * c);
  25.     root1 = (-b + (pow(b,2) - (4 * a * c))) / (2 * a);
  26.     root2 = (-b - (pow(b,2) - (4 * a * c))) / (2 * a);
  27.  
  28.     if descrim = 0
  29.         cout << "Equation has a single (repeated) root" << endl;
  30.     if descrim > 0
  31.         cout << "Equation has two real roots" << endl;
  32.     if descrim < 0
  33.         cout << "Equation has two complex roots" << endl;
  34.     if descrim >= 0
  35.         cout << "The roots of the quadratic equation are: " << endl;
  36.         cin >> root1;
  37.         cin >> root2;
  38.  
  39.     system("pause");
  40.     return 0;
Feb 12 '07 #1
6 14811
RRick
463 Expert 256MB
If this doesn't compile, try displaying the errors. I doubt the compiler will like double a!= 0; Add some logic and a error message for this after the user inputs the 'a' value.

Your ideas are sound, but the root values are missing a sqrt call. It looks like the logic for read and imaginary solutions (the couts at the bottom) are reversed.

If you want use doubles for the roots, you'll have to add some logic for imaginary roots. Sqrt does not like negative values. Remember, a complex root has two parts: real and imaginary.

There is a better solution. C++ now has a complex object (<complex>). Complex also supports negative sqrts. Instead of using doubles, use complex for your root calculations. For sqrt, use pow with a .5 power value.
Feb 12 '07 #2
Ganon11
3,652 Expert 2GB
I'd like to add a few points:

You declare double x, but never use it - you can safely rid yourself of it.

Next, when finding root1 and root2, you explicitly type pow(b,2) - (4 * a * c), but you already have this value scored in descrim - why not use descrim in your calculations?

Finally, to avoid finding the square root of negative numbers, once you have the descrim you can use an if...statement to make sure you only find the roots if descrim >= 0. If not, you can display "2 complex roots" and end the program. If it is >= 0, you can determine if it is exactly 0 (and if so, print 1 root) or if it is positive (and if so, print both roots).
Feb 12 '07 #3
Trev17
22
thanks for the help guys, its greatly appreciated. If i have any more problems with the program i'll be sure to post.
Feb 12 '07 #4
Trev17
22
If this doesn't compile, try displaying the errors. I doubt the compiler will like double a!= 0; Add some logic and a error message for this after the user inputs the 'a' value.

Your ideas are sound, but the root values are missing a sqrt call. It looks like the logic for read and imaginary solutions (the couts at the bottom) are reversed.

If you want use doubles for the roots, you'll have to add some logic for imaginary roots. Sqrt does not like negative values. Remember, a complex root has two parts: real and imaginary.

There is a better solution. C++ now has a complex object (<complex>). Complex also supports negative sqrts. Instead of using doubles, use complex for your root calculations. For sqrt, use pow with a .5 power value.
yeah, i changed the coding a little bit but i still get a syntax error for descrim. Also, i don't really understand how to set pow with a .5 value. Here are the changes i have made:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <cmath>
  5. #include <complex>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int _tmain(int argc, _TCHAR* argv[])
  11. {
  12.     double a;
  13.     double b;
  14.     double c;
  15.     complex<double> root1;
  16.     complex<double> root2;
  17.     double descrim;
  18.  
  19.     cout << "Please input the value of a: " << endl;
  20.     cin >> a;
  21.     if (a != 0) 
  22.         cout <<"Please input a value other than 0: " << endl;
  23.         cin >> a;
  24.     cout << "Please input the value of b: " << endl;
  25.     cin >> b;
  26.     cout << "Please input the value of c: " << endl;
  27.     cin >> c;
  28.  
  29.     descrim = pow(b,2) - (4 * a * c);
  30.     root1 = (-b + (pow(b,2) - (4 * a * c))) / (2 * a);
  31.     root2 = (-b - (pow(b,2) - (4 * a * c))) / (2 * a);
  32.  
  33.     if descrim = 0
  34.         cout << "Equation has a single (repeated) root" << endl;
  35.     if descrim > 0
  36.         cout << "Equation has two real roots" << endl;
  37.     if descrim < 0
  38.         cout << "Equation has two complex roots" << endl;
  39.     if descrim >= 0
  40.         cout << "The roots of the quadratic equation are: " << endl;
  41.         cin >> root1;
  42.         cin >> root2;
  43.  
  44.     system("pause");
  45.     return 0;
  46. }
  47.  
Feb 12 '07 #5
RRick
463 Expert 256MB
I assume your errors are at the conditionals for discrim. It looks like you need parentheses around the conditional expressions.
===> if ( discrim > 0 ) ....

Use the complex objects, but be warned, there is a bit of a learning curve to understand them. That said: you can use an int or a double value for the second parameter to the complex version of pow.

Expand|Select|Wrap|Line Numbers
  1. Complex a(9.0), b;
  2. b = pow(a, 2);      //  b = a * a
  3. b = pow(a, .5);      //  b = sqrt( a)
  4.  
Feb 12 '07 #6
Trev17
22
it works, thanks for the help
Feb 13 '07 #7

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

Similar topics

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.
6
by: chronoxx251 | last post by:
Hi, I'm doing a school project where I need to make a program to calulate the quadratic equation. Now, I already got that part taken care of with the following code: #include "stdafx.h" #include...
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
1
by: shiska | last post by:
hello ,there could u please help me to display a c program on a quadratic equation ax2+bx+c. thanx
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.