473,657 Members | 2,765 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 1979
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
16153
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...
16
2653
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...
117
7175
by: Peter Olcott | last post by:
www.halting-problem.com
7
1519
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;
25
7743
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
8
1629
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()
4
1912
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...
5
4429
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++...
13
2148
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
0
8384
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, 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...
0
8302
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8820
Oralloy
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...
1
8499
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,...
0
8601
tracyyun
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...
0
7314
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
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 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...
0
5630
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1601
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 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.