473,503 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with mathematical function in program

2 New Member
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 1967
vikasdev
4 New Member
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
newbprogrammer
2 New Member
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 Recognized Expert Specialist
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

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

Similar topics

2
16143
by: moi | last post by:
Can someone please help with this problem im having. i have to use the newton-raphson technique to find the root of a function, in this case X^2 - 1. basically, the program has to read in values...
16
2636
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
117
7102
by: Peter Olcott | last post by:
www.halting-problem.com
7
1511
by: Jef Driesen | last post by:
Suppose I have an abstract base class that looks like this: template <typename T> class base { public: // Typedefs typedef double value_type; typedef std::size_t size_type; public: //...
25
7717
by: Xah Lee | last post by:
Python Doc Problem Example: gzip Xah Lee, 20050831 Today i need to use Python to compress/decompress gzip files. Since i've read the official Python tutorial 8 months ago, have spent 30...
8
1621
by: Fan Zhang | last post by:
Dear group, I wrote a simple code trying to use the integer index of a "for" loop inside the loop. The idea is to convert it to double type and lead it into a function. However it appears I...
4
1900
by: Xah Lee | last post by:
i've long time been interested in algorithmic mathematical art. That is, mathematical or algorithmic visual art works that are generated by computer such that the program's source code reflects the...
5
4422
by: jeremito | last post by:
I am extending python with C++ and need some help. I would like to convert a string to a mathematical function and then make this a C++ function. My C++ code would then refer to this function to...
13
2135
by: jacek.strzelczyk | last post by:
Hello, I'm looking for a C library that provides the notation of n- dimensional mathematical functions. Or is there any other way to decode that kind of functions in C language? Thanks in...
0
7199
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
7076
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...
1
6984
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...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.