By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,482 Members | 3,157 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,482 IT Pros & Developers. It's quick & easy.

Problem with mathematical function in program

P: 2
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
Share this Question
Share on Google+
3 Replies


P: 4
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

P: 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
Expert 2.5K+
P: 3,652
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.