Results 1 to 6 of 6
Hi everyone,
I'm very new to programming - sorry if this seems obvious!
I'm trying to solve quadratics using an if-else statement. I'm using the following program:
Code:
/*
solve ...
- 07-29-2008 #1Just Joined!
- Join Date
- Jul 2008
- Posts
- 14
tiny in C
Hi everyone,
I'm very new to programming - sorry if this seems obvious!
I'm trying to solve quadratics using an if-else statement. I'm using the following program:
It compiles seemingly happily, but whatever values I enter for a, b and c, I get "Not a quadratic, a is too close to zero" and asked to enter a, b and c again. Can anyone see why?Code:/* solve quadratic equation illustrating if-else statements. */ #include<stdio.h> #include<catam.h> double const tiny=1.0E-20; /*small nonzero number*/ double a,b,c; /*coeffs of x*x, x and 1 */ double r,i; /*real & imaginary parts of solution */ double tmp1,tmp2; /*temporary real variables */ int main(void) { a = 0; b = 0; c = 0; do { printf("Enter a, b and c "); fflush(stdout); scanf("%1f %1f %1f",&a,&b,&c); if (fabs(a) <= tiny) { printf("Not a quadratic, a is too close to zero\n"); } } while (fabs(a) <= tiny); tmp1 = b*b-4*a*c; if (tmp1 >= 0) { tmp1 = sqrt(tmp1)/(2*a); tmp2 = -b/(2*a); r = tmp2 + tmp1; printf("Solution 1 is %14.4f\n",r); r = tmp2 - tmp1; printf("Solution 2 is %14.4f\n",r); } else { r = -b/(2*a); i = sqrt(-tmp1)/(2*a); printf("Solution 1 is %14.4f + %14.4f i\n",r,i); printf("Solution 2 is %14.4f - %14.4f i\n",r,i); } return 0; }
Thanks!
- 07-30-2008 #2
What happens if you output the values of a, b, and c immediately after reading them in?
--
Bill
Old age and treachery will overcome youth and skill.
- 07-30-2008 #3Just Joined!
- Join Date
- Jul 2008
- Posts
- 14
Ok, I did this by adding the lines
before the first if. Was this the correct thing to do? (sorry, I only started programming last week, so am still unsure...)Code:printf("%4d %4d %4d\n",a,b,c); fflush(stdout);
The result:
Code:Enter a, b and c 1 1 1 1065353216 0 1065353216 Not a quadratic, a is too close to zero Enter a, b and c
Seems a little odd...
Thanks!
- 07-31-2008 #4
Your main problem, I believe, is that the variable type doesn't match the format, either for fscanf() or, in the revised version, the printf().
Compile your program with the
switch. When it tells you about the mismatches, read the scanf man page with an eye for the picky details.Code:-Wall
--
Bill
Old age and treachery will overcome youth and skill.
- 07-31-2008 #5Just Joined!
- Join Date
- Jul 2008
- Posts
- 5
Make sure your scanf is lf (double) not 1f (float) .... Big difference
. That's 'l' as in Larry ....
- 08-01-2008 #6True. And rclark is correct to point this out, because the difference looks subtle, but is quite imporant. But I didn't want to spell it out, for two reasons:Make sure your scanf is lf (double) not 1f (float) .... Big difference . That's 'l' as in Larry ....
- I want him to get familiar with the documentation.
- That's not the only format string problem in his code.
--
Bill
Old age and treachery will overcome youth and skill.


Reply With Quote