Efficient Programming: Troubleshooting Pow Function

  • Thread starter Thread starter beanryu
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a programming issue related to the use of the pow function in C. Participants explore potential errors in the code, specifically focusing on input handling and function recognition. The scope includes technical explanations and debugging strategies.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant identifies an error in the scanf statement, suggesting it should be corrected to use "%lf" instead of "%FL" and notes that the variable x_one does not exist.
  • Another participant reports that even after correcting the scanf statement, the output remains "0" and suspects that the pow function is not being recognized.
  • There is a question about the validity of using "%fl" in scanf, with one participant asserting that "%f" is standard.
  • A participant mentions that their textbook states "%fl" is necessary in scanf, while also noting that the program works with direct multiplication instead of the pow function.
  • Another participant suggests that "%lf" is the correct format specifier and asks if the math library was linked during compilation.
  • A later reply proposes changing both scanf and printf statements to use "%lf" and "%lf" respectively, claiming that this resolves the issue.

Areas of Agreement / Disagreement

Participants express differing views on the correct format specifier for scanf and whether the pow function is functioning as intended. There is no consensus on the root cause of the problem, as multiple potential issues are raised.

Contextual Notes

There are unresolved questions regarding the linking of the math library and the correct usage of format specifiers in scanf and printf. The discussion reflects uncertainty about the behavior of the pow function in this specific context.

Who May Find This Useful

Programmers and students working with C who are troubleshooting similar issues with mathematical functions and input/output operations.

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)
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
14
Views
4K
Replies
10
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K