Efficient Programming: Troubleshooting Pow Function

  • Thread starter beanryu
  • Start date
In summary, the conversation is about a program that uses the pow() function to calculate a mathematical equation. The program is giving incorrect results and the conversation focuses on correcting the errors, such as using the correct syntax for scanf and printf statements and linking the math library when compiling. Ultimately, changing the scanf and printf statements to use %lf instead of %fl allows the program to run correctly.
  • #1
beanryu
92
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
  • #2
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
 
  • #3
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:
  • #4
Is %fl allowed in C at all? I know %f is.
 
  • #5
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)
 
  • #6
I thought it was %lf, not %fl.

Anyways, did you remember to link your program with the math library, when you compiled it?
 
  • #7
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)
 

1. What is the difference between C and C++?

C and C++ are both programming languages, but C++ is an extension of C. C is a procedural language, while C++ is an object-oriented language, meaning it allows for the use of objects and classes for more complex programming tasks.

2. What is a compiler?

A compiler is a program that translates human-readable code (such as C) into machine-readable code that can be executed by a computer. It takes the source code and converts it into an executable file that can be run on a specific operating system or hardware platform.

3. What is the difference between a variable and a constant?

In C programming, a variable is a named storage location in memory that can hold a value, which can be changed during program execution. A constant, on the other hand, is a value that cannot be modified once it is assigned. It is often used to store values that are not expected to change, such as mathematical constants or program settings.

4. What is a function in C?

A function in C is a block of code that performs a specific task and can be called from other parts of the program. It allows for code reusability and helps to organize large programs into smaller, more manageable parts. Functions in C can have input parameters and return values, making them versatile for different programming needs.

5. What are pointers in C?

Pointers in C are variables that store memory addresses of other variables. They allow for direct access and manipulation of memory locations, which can be useful for tasks such as memory management or passing parameters to functions by reference. Pointers can be tricky to use, but they are a powerful tool for experienced C programmers.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
900
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
733
  • Programming and Computer Science
Replies
20
Views
2K
Replies
10
Views
1K
  • Programming and Computer Science
Replies
1
Views
646
  • Programming and Computer Science
Replies
3
Views
995
Back
Top