C/C++ Creating a Function Prototype in C++ to Calculate a Value Using f(x)

  • Thread starter Thread starter Freyster98
  • Start date Start date
  • Tags Tags
    Function Prototype
AI Thread Summary
To create a function prototype in C++ for calculating a value using f(x), the function must be declared before it is called in the main function. The correct prototype is "double f(double x);" followed by the function definition. While using the pow function is acceptable, it is more efficient to use direct multiplication for integer powers. An optimized version of the function can be expressed as "x*(x*(5*x-3)+2)+1" for better performance. This approach allows for the calculation of values like f(2.4) effectively.
Freyster98
Messages
49
Reaction score
0
I am a beginner trying to enter a function prototype into C++. This is what I have entered...where am I going wrong?

double f(double x)
{
return 5*pow(x,3)-3*pow(x,2)+2*x+1;
}

I would like to be able to enter something like f(2.4) later in the code, and have it calculate the value using this function. How would I do this?

f(x)=5x3-3x2+2x+1
 
Technology news on Phys.org
There's nothing wrong with what you have.

Code:
#include <iostream>
#include <math>

double f(double x)
{
    return 5*std::pow(x,3) - 3*std::pow(x,2) + 2*x + 1;
}

int main()
{
    std::cout << f(2.4) << std::endl;
}
 
OK, thanks. I didn't know I had to declare it before main().
 
You don't have to define a function before it's called, just declare a prototype. Same code with an actual prototype:
Code:
#include <iostream>
#include <math>

double f(double x);    /* this is the prototype */

int main()
{
    std::cout << f(2.4) << std::endl;
}

double f(double x)
{
    return 5*std::pow(x,3) - 3*std::pow(x,2) + 2*x + 1;
}
 
Side note: while it is not generally an error, using pow function to raise float number to an integer power is an overkill. It is absolutely enough to use multiplication:

Code:
5*x*x*x-3*x*x+2*x+1

It will execute faster then your code. But it can be very simply modified to be executed even faster:

Code:
x*(x*(5*x-3)+2)+1
 
Thank you all for your help.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Back
Top