Find the answer to your Linux question:
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 ...
  1. #1
    Just 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:
    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;
    }
    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?

    Thanks!

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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.

  3. #3
    Just Joined!
    Join Date
    Jul 2008
    Posts
    14
    Ok, I did this by adding the lines

    Code:
    printf("%4d  %4d  %4d\n",a,b,c);
    fflush(stdout);
    before the first if. Was this the correct thing to do? (sorry, I only started programming last week, so am still unsure...)

    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!

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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
    Code:
    -Wall
    switch. When it tells you about the mismatches, read the scanf man page with an eye for the picky details.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Just 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 ....

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Make sure your scanf is lf (double) not 1f (float) .... Big difference . That's 'l' as in Larry ....
    True. 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:
    1. I want him to get familiar with the documentation.
    2. That's not the only format string problem in his code.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...