14,216,449 members
Rate this:
See more:
Equation : -0.87 sin (4x) - 0.56 log (4x) + 1.32 ln (5x) Range of x : 8<= x <18 Step of x : 0.100
Generate value of f(x)?
Can the code be more flexible in selecting which sin, cos, tan, or cosine, log or even ln to be used on any equation? Means could the user give their own input for the to be used replacing log, ln or sin in the equation above? If could not, just according to the equation is good enough. thank you :)

What I have tried:

```#include <math.h>;
#include <iostream>;
#include <iomanip>;
using namespace std;

double func( double x )
{
return -0.87*sin( 4*x ) - 0.56*log10( 4*x ) + 1.32*log( 5*x );
}

for( double x{ 8.0 }; x < 18.0; x += 0.1 ){

cout<< func(x) << "\n";```

But I've got bug at line *for* and it say that error : expected unqualified id before for , error : 'x' does not name a type
Posted
Updated 4-Dec-16 21:35pm
v2
Patrice T 5-Dec-16 0:18am

Do not Repost
Use Improve question to update your question.
Member 12886403 5-Dec-16 0:26am

what repost?
Patrice T 5-Dec-16 0:33am

Repost of How do I do this coding for this C++ problem[^]
with just a little change.

Rate this:

## Solution 1

You got some typos in the for loop. This should work:
```for( double x = 8.0; x < 18.0; x += 0.1 ){
cout<< func(x) << "\n";
}```

If you want to enhance your program you can work with cin to get values from the user. A tutorial for cout and cin. And than use a switch or compare to use different code pathes.
Member 12886403 5-Dec-16 3:49am

Thank you. I'm currently is working on an assignment and it is super long coding and this is part of it. Thanks a lot. i'll ask for more opinion in the future. Thanks a lot.
Rate this:

## Solution 2

The `for` loop is not part of any function in your code and the declaration and initialisation of `x` is wrong.

It should be probably part of the `main` function:
```int main()
{
for( double x = 8.0; x < 18.0; x += 0.1 )
{
cout<< func(x) << "\n";
}
return 0;
}```
Member 12886403 5-Dec-16 3:54am

double func( double x )
{
return -0.87*sin( 4*x ) - 0.56*log10( 4*x ) + 1.32*log( 5*x );
}

// This act like func definition?
Jochen Arndt 5-Dec-16 4:20am

Yes, that is a function definition (code that implements the function).

A function declaration would be

double func( double x );

Note the ';' at the end. It would tell the compiler that there will be a corresponding declaration somewhere else.

In your case there is no need for a declaration because the definition is placed before the function is used (implicite declaration).
Member 12886403 5-Dec-16 3:54am

Thanks btw :)

Top Experts
Last 24hrsThis month
 OriginalGriff 360 Maciej Los 280 BillWoodruff 90 F-ES Sitecore 80 Richard MacCutchan 75
 OriginalGriff 3,648 Richard MacCutchan 1,303 Maciej Los 1,100 Patrice T 1,008 Thomas Daniels 940