472,127 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Problem with mathematical function in program

I just started programming in c++ and i tried to explore and do programs on my own... So i wanted to do a program that would help me in my daily school work ... a program to solve quadratic equations.. I wrote the code correctly but errors are still detected and i have no idea why.. almost gave up and banged my head on the wall .. so i would really appreciate help.. This is the code so far..
Expand|Select|Wrap|Line Numbers
  1. #include <cmath>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     double x;
  9.     double y;
  10.     double z;
  11.  
  12.     cout<<"Please enter the coeff of (x square):";
  13.     cin>> x;
  14.     cout<<"Please enter the coeff of (x):";
  15.     cin>> y;
  16.     cout<<"Please enter the number without the unknown (x):";
  17.     cin>> z;
  18.  
  19.     double b = (((y * y) - (4 * x * z)));
  20.     double a = ((-y + (sqrt (b)) / (2 * x)));
  21.     double c = ((-y - (sqrt (b)) / (2 * x)));
  22.  
  23.     cout<<"X = "<< a << "or" << c;
  24.     cin.ignore();
  25.     cin.get();
  26. }
The answers i get arnt even close to the actual answers...

Please tell me what is wrong.. PLEASE PLEASE PLEASE
-------------------------------------------------------------------
This should help in understanding what i am trying to do.....

This program is supposed to solve equations like (3x^2 * -7x + 2)
so.. Using the general formula to solve this equation we take the coeff of x^2 to be "x", coeff of x to be "y" and coeff of number without unknown x to be "z".
Therefore x = 3
y = -7
z = 2
The equation to find x = ((-y) + sqrt((y^2) - (4*x*z))) / 2*x)
so... resulting to.. x = (7 + (sqrt (49 - 24)) / 6
and.. x = (7 + 5)/6
x = 2 //
As you can see.. in my code.. double b should = 25...

Any questions please ask.. i need help to solve this program.. I just really want to finish this program so please help
Feb 17 '08 #1
3 1864
In the statement
Expand|Select|Wrap|Line Numbers
  1. double a=(.......) .check the parenthesis u hav given.it shud be
  2. a=(-y+(sqrt(b)))/(2*x);
now it will give correct output....
and plz check that b(determinant)>=0,before proceeding further...
Feb 17 '08 #2
Thanks alot....it works now but if determinant is less then 0 how do i make the code say answer not valid or possible.. i just have to add (double b > 0)??
Feb 17 '08 #3
Ganon11
3,652 Expert 2GB
Have you looked at if statements? You might do something like:

Expand|Select|Wrap|Line Numbers
  1. // Calculate value of b...
  2. if (b < 0) {
  3.   // Answer is an imaginary number.
  4. } else if (b == 0) {
  5.   // Only one real answer
  6. } else {
  7.   // Two real answers.
  8. }
Feb 17 '08 #4

Post your reply

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

Similar topics

2 posts views Thread by moi | last post: by
16 posts views Thread by cody | last post: by
117 posts views Thread by Peter Olcott | last post: by
7 posts views Thread by Jef Driesen | last post: by
8 posts views Thread by Fan Zhang | last post: by
4 posts views Thread by Xah Lee | last post: by
5 posts views Thread by jeremito | last post: by
13 posts views Thread by jacek.strzelczyk | last post: by
reply views Thread by leo001 | last post: by

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.