Efficient Programming: Troubleshooting Pow Function

  • Thread starter Thread starter beanryu
  • Start date Start date
AI Thread Summary
The discussion revolves around a C program intended to calculate a mathematical expression using the `pow` function. The initial code contains several errors, including an incorrect format specifier in the `scanf` function ("%FL" instead of "%lf") and a typo with the variable name. After corrections, the user still encounters issues, specifically that the program only returns "0" regardless of input. It is suggested that the user ensure the program is linked with the math library during compilation. Additionally, the correct format specifiers for `scanf` and `printf` should be "%lf" for double precision values, not "%fl". The user confirms that simpler arithmetic operations work, indicating the problem lies specifically with the `pow` function.
beanryu
Messages
90
Reaction score
0
I wrote the following "program"

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


int main()
{
double x,y;

printf("enter a number");
scanf("%FL,&x_one");


y = pow(x,3.0) - 4.0*x + 1.0;
printf("%d",y);

return 0;

}

could anyone tell me why I can't get a correct result from this?
What had I done wrong with the pow(power) operation?
THANX!
 
Technology news on Phys.org
The line

scanf("%FL,&x_one");

doesn't make any sense. Did you put the quote marks in the wrong place? Did you mean:

scanf("%FL", &x_one);

Of course, the variable x_one doesn't exist in your program, and you probably should use lowercase characters in your scanf -- like "%fl".

- Warren
 
even I corrected the errors you pointed out to me...

i can't get any other result but the degit "0"!
I use miracle C to compile it.

corrected program:
#include <stdio.h>
#include <math.h>


int main()
{
double x,y;

printf("enter a number");
scanf("%fl",&x);


y = pow(x,3.0) - 4.0*x + 1.0;
printf("%d",y);

return 0;

}

I think the "pow" function is not being recognized...
whatever vulue I enter in the parathesis after pow function, it returns 1!
 
Last edited:
Is %fl allowed in C at all? I know %f is.
 
my textbook said yes, its a must in a scanf() thing

the thing is the program works fine when its

y = x*3.0) - 4.0*x + 1.0
or y = x-3.0 - 4.0*x + 1.0
or whatever other thing I tried

just not pow(x,3.0)
 
I thought it was %lf, not %fl.

Anyways, did you remember to link your program with the math library, when you compiled it?
 
If you change your scanf and your printf statements to this
Code:
...
scanf("%lf",&x);
...
printf("%lf", y); 
...
it should work (I tried it, and it works fine)
 
Back
Top