Click here to Skip to main content
14,216,449 members
Rate this:
Please Sign up or sign in to vote.
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)?
Convert to rad?
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
Comments
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:
Please Sign up or sign in to vote.

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.
   
Comments
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:
Please Sign up or sign in to vote.

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;
}
   
Comments
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 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190619.2 | Last Updated 5 Dec 2016
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100