473,699 Members | 2,812 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>
  4. using namespace std;
  6. int main()
  7. {
  8.     double x;
  9.     double y;
  10.     double z;
  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;
  19.     double b = (((y * y) - (4 * x * z)));
  20.     double a = ((-y + (sqrt (b)) / (2 * x)));
  21.     double c = ((-y - (sqrt (b)) / (2 * x)));
  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 1981
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
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
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

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 of x0, tolerance, and a boolean as to whether the approximate or exact df/dx is to be used. and its specified that the function names and their signatures have to be as they are below. thats where im getting all buggered up really. i totally...
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 with the same A or B value. But there can be more than one object with A = null or B = null in the bucket. Sometimes there is only one valid solution, sometimes there are more valid solutions, and sometimes there isn't a complete solution at...
by: Peter Olcott | last post by:
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: // Assignment operators. virtual base<T>& operator=(const base<T>& rhs) = 0;
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 minutes with Python 3 times a week since, have 14 years of computing experience, 8 years in mathematical computing and 4 years in unix admin and perl, i have quickly found the official doc: http://python.org/doc/2.4.1/lib/module-gzip.html
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 can't call the integer number directly inside the loop, otherwise, the results are not sensible. The code is as the following, int main()
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 algorithmic essence of the visual quality in the art work. (for detail, see Algorithmic Mathematical Art at http://xahlee.org/Periodic_dosage_dir/t1/20040113_cmaci_larcu.html ) Mathematica programers, especially Michael Trott, have been doing...
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 calculate what it needs. For example I want to tell my function to calculate "x^2 + 3x +2", but later on I may want: "x + 3". Does anybody know how I can get an arbitrary string in Python (but proper mathematical function) turned into a C++...
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 advance, Jacek
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.