473,322 Members | 1,699 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,322 software developers and data experts.

Quadratic equation solver

15
Ask the user to enter the coefficients a, b and c. Have your program then solve the two (or possibly one) solution(s) for X. Be careful to make sure there is a solution by confirming that the square root is not executed on a negative number which is considered to be imaginary.

Be sure to #include<cmath> and call the pow() and sqrt() functions when making your math calculations. Also note, the coefficients a b and c are to be declared as a double, as well as your two solutions for X.
this is what i did so far:
Expand|Select|Wrap|Line Numbers
  1. #include <cmath>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main ()
  7. {                       
  8. int a;
  9. int b;
  10. int c;
  11. int x;
  12. double posx, negx; //declare positive x and negative y
  13.  
  14. cout<<"Enter the value of a: ";
  15. cin>>a;
  16. cout<<"Enter the value of b: ";
  17. cin>>b;
  18. cout<<"Enter the value of c: ";
  19. cin>>c;
  20.  
  21. int check;
  22. check=(b*b)-(4*a*c);      
  23. if (check>0)
  24. {
  25.    posx=(-b+sqrt(check))/2*a;
  26.    negx=(-b-sqrt(check))/2*a;
  27.  
  28. system ("pause");
  29. return 0;
  30.  
  31. }
  32.  
  33. }
Feb 26 '10 #1
14 3355
I am assuming your program attempts to solve a quadratic equation.
please mention the issue you are facing in more details. From the problem description i could not make out any issues at all !!

Here are a few pointers:
1) You have not declared a,b,c as double.
2) Declare 'check' as double as well or sqrt() will complain of an ambiguous call.

Do this and then let us know exactly where you are getting stuck.

And , most important , don't forget to put your code snippets inside the code-tags.
Feb 26 '10 #2
joeyke
15
i don't know how the output should look like because from what i get it shows this:
Last login: Thu Feb 25 21:57:40 on ttys000
/Volumes/JOANNEKEO/C++1.5/HWkeoAssignment2-3A
joanne-keos-macbook:~ joanne$ /Volumes/JOANNEKEO/C++1.5/HWkeoAssignment2-3A
Enter the value of a: 5
Enter the value of b: 6
Enter the value of c: 3
joanne-keos-macbook:~ joanne$

the picture is the quadratic equation
Attached Images
File Type: png Picture 3.png (13.5 KB, 157 views)
Feb 26 '10 #3
I think you forgot to display the values of posx and negx .

that i think is quite important. ;)
Feb 26 '10 #4
joeyke
15
oh yeah ! how do i go about that in my program? thank for you help btw!
Feb 26 '10 #5
try cout<<" posx ="<<posx<<" negx = "<<negx;
that should do a neat job of printing the values.
Feb 26 '10 #6
joeyke
15
where do i put that? after this:
posx=(-b+sqrt(check))/2*a;
negx=(-b-sqrt(check))/2*a;
Feb 26 '10 #7
When do you think the output should be displayed?
before the calculation or after it ? :)
Feb 26 '10 #8
joeyke
15
after i think. is it like this:

if (check>0)
{
cout<<" posx ="<<posx<<" negx = "<<negx;

cin>>posx, negx;

posx=(-b+sqrt(check))/2*a;

negx=(-b-sqrt(check))/2*a;


system ("pause");

return 0;

}

}
Feb 26 '10 #9
Banfa
9,065 Expert Mod 8TB
Have you tried to compile that?
If it compiled what result did you get printed on the screen?
Was that the result you expected?

grayMist question "When do you think the output should be displayed? Before the calculation or after it ?" is still relevant but perhaps you should also answer the question

Which piece of my code actually does the calculation.
Feb 26 '10 #10
joeyke
15
i still get this in my output:
Enter the value of a: 1
Enter the value of b: 2
Enter the value of c: 3

should there a calculation done in my output? if so then why isn't it printing that?
Feb 26 '10 #11
joeyke
15
wait i get it!
Enter the value of a: 1
Enter the value of b: 4
Enter the value of c: 6
posx =-3.81582e-232 negx = 0

is this right?
Feb 26 '10 #12
joeyke
15
that's what prints now after i did
double check;
check=(b*b)-(4*a*c);
cout<<" posx ="<<posx<<" negx = "<<negx;
cin>>posx, negx;
Feb 26 '10 #13
johny10151981
1,059 1GB
you have a huge mistake in the below coldes

posx=(-b+sqrt(check))/2*a;

negx=(-b-sqrt(check))/2*a;



please rethink. you must get a result.

Regards,
Johny
Feb 26 '10 #14
Banfa
9,065 Expert Mod 8TB
Enter the value of a: 1
Enter the value of b: 4
Enter the value of c: 6
posx =-3.81582e-232 negx = 0

is this right?
You should know if it is right the first time you run the program. You do this by using the co-efficients of a function that you know the roots of. For a quadratic (or any polynomial) this is relatively easy because many quadratics can be written in the form

(m.x + n).(o.x + p)

Set m and o to 1 to make things easy, set n to a small negative integer and p to a small postive integer and you get

a = 1
b = p + n
c = p * n

With roots -p and -n

So you have a known equation with known roots and you can plug those values into your program and you will know it it has output the right value.
Feb 26 '10 #15

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

Similar topics

3
by: parisgoblet | last post by:
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 roots of a quadratic...
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.
2
by: tristan.chaplin | last post by:
Is it possible to use the Excel Equation Solver in an Access Database, ie call it from vba code? Thanks, Tristan
3
by: amitsoni.1984 | last post by:
Hi, I need to do a quadratic optimization problem in python where the constraints are quadratic and objective function is linear. What are the possible choices to do this. Thanks Amit
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...
6
by: Trev17 | last post by:
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...
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...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.