473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Quadratic Formula Woes

fb
Hello everyone. I'm having a touch of trouble solving a problem using
the quadratic formula. I get a domain error...Somewhere in the sqrt
function I think. Could you guys give me a hint on what's up? Is there
maybe some kind of standard Quadratic function hiding away in the
libraries? That would be nice to have...Here's my source:

/* Trying to calculate the real roots of "ax^2 + bx + c = 0"
using the quadratic formula */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
double a, b, c, d, x1, x2;

/* read input data */

printf("a = ");
scanf("%f", &a);
printf("b = ");
scanf("%f", &b);
printf("c = ");
scanf("%f", &c);

/* Carry out the calculations */

d = sqrt(b*b-4*a*c);
x1 = (-b + d) / (2 * a);
x2 = (-b - d) / (2 * a);

/* Display output */

printf("\nx1 = %e x2 = %e", x1, x2);

return EXIT_SUCCESS;
}

Nov 14 '05 #1
6 2311

On Sun, 29 Aug 2004, fb wrote:

/* Trying to calculate the real roots of "ax^2 + bx + c = 0"
using the quadratic formula */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
double a, b, c, d, x1, x2;

/* read input data */

printf("a = ");
Here you need a
fflush(stdout);
scanf("%f", &a);
This should be
scanf("%lf", &a);
and it's probably what's causing your troubles.

The same two comments apply twice again below.
printf("b = ");
scanf("%f", &b);
printf("c = ");
scanf("%f", &c);

/* Carry out the calculations */
Before doing anything else, you should be printing out the values
of 'a', 'b', and 'c', just to make sure you read them correctly.
Printing intermediate results is an important debugging technique
in /any/ language.
d = sqrt(b*b-4*a*c);
If the quadratic has no real-number solutions, this will cause
a domain error. Could your problem be simply that you're trying
to solve an unsolvable quadratic?
x1 = (-b + d) / (2 * a);
x2 = (-b - d) / (2 * a);

/* Display output */

printf("\nx1 = %e x2 = %e", x1, x2);
Better use "%g" unless you know what you're doing. And you
need to end the output with a newline.

printf("\nx1 = %g x2 = %g\n", x1, x2);
return EXIT_SUCCESS;
}


HTH,
-Arthur
Nov 14 '05 #2
fb wrote:

Hello everyone. I'm having a touch of trouble solving a problem
using the quadratic formula. I get a domain error...Somewhere in
the sqrt function I think. Could you guys give me a hint ... snip


What is the square root of -1?

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Nov 14 '05 #3
kal
CBFalconer <cb********@yahoo.com> wrote in message news:<41***************@yahoo.com>...
What is the square root of -1?


What is -1?
Nov 14 '05 #4
CBFalconer wrote:
fb wrote:
Hello everyone. I'm having a touch of trouble solving a problem
using the quadratic formula. I get a domain error...Somewhere in
the sqrt function I think. Could you guys give me a hint ... snip

What is the square root of -1?


i

:P
Nov 14 '05 #5
In message <z7*******************@fe2.columbus.rr.com>
dbtid <db***@dev.null.us> wrote:
CBFalconer wrote:
fb wrote:
Hello everyone. I'm having a touch of trouble solving a problem
using the quadratic formula. I get a domain error...Somewhere in
the sqrt function I think. Could you guys give me a hint ... snip

What is the square root of -1?


i

:P

tsk, tsk... Not true, but i squared equals -1 :-)

Arjan
Nov 14 '05 #6
fb wrote:
Hello everyone. I'm having a touch of trouble solving a problem using
the quadratic formula. I get a domain error...Somewhere in the sqrt
function I think. Could you guys give me a hint on what's up? Is there
maybe some kind of standard Quadratic function hiding away in the
libraries? That would be nice to have...Here's my source:

/* Trying to calculate the real roots of "ax^2 + bx + c = 0"
using the quadratic formula */

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
double a, b, c, d, x1, x2;

/* read input data */

printf("a = ");
scanf("%f", &a);
printf("b = ");
scanf("%f", &b);
printf("c = ");
scanf("%f", &c);

/* Carry out the calculations */

d = sqrt(b*b-4*a*c);
You are making the assumption that b^2 - 4ac is always >= 0 .
May be you need to be a guard here, that checks in case they end up
being -ve numbers.
x1 = (-b + d) / (2 * a);
x2 = (-b - d) / (2 * a);

/* Display output */

printf("\nx1 = %e x2 = %e", x1, x2);

return EXIT_SUCCESS;
}

--
Karthik.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Vinodh Kumar | last post by:
Number One: ========= I know its a 100% offline post.But seeing the diversity of the domain from which people are participating, I would like to post this question here.Pardon me.At the least, u...
3
by: amitsoni.1984 | last post by:
Hi, I need to do a quadratic optimization problem in python where the constraints are quadratic and objective function is linear. What are the possible choices to do this. Thanks Amit
6
by: Trev17 | last post by:
Hello, I am new to C++ and i have tried for several hours to make a program my teacher has given me as a lab. Here is the Lab question: the roots of the quadratic equation ax^2 + bx + c = 0, a...
2
by: DaRok28 | last post by:
// Program Description: // This program solves quadratic equations to find their roots. This // program takes values of a, b, and c as input and outputs the root(s). // The user can repeat the...
11
by: luftikus143 | last post by:
Hi there, I have a nice collection of photos on my photoblog, for which I created as well an archive, which shows the last 50 or so photos at the same time. Now, I would like to have the...
2
by: ioannoual | last post by:
Hi...I 'am new in C and I want a program that solves a quadratic equation!! I try something by my self to write some code about that but I want you to help me writing a new one that will work!! ...
1
by: bbench123 | last post by:
Make a program that will ask for values of a quadratic equation (ax2+bx+c). the program must determine the roots of the equation using the quadratic equation determinants to distinguish if the roots...
1
by: candacefaye1 | last post by:
1. write a C++ program to decide if the coefficients of a quadratic equation have real roots. The three choices will be to write the message “zero divide” when A is zero, write the message “no real...
10
by: bkustel | last post by:
I'm stuck on a problem where I want to use marshal for serialization (yes, yes, I know (c)Pickle is normally recommended here). I favor marshal for speed for the types of data I use. However it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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...
0
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,...

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.