Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
class DefInt
{
private:
double a;
double b;
double (*f)(double x);
int N;
public:
DefInt(double c, double d, double (*g)(double y))
{
a = c;
b = d;
f = g;
}
double BySimpson()
{
double sum = f(a) + 4 * f((a + b) / 2) + f(b);
return sum * (b - a) / 3;
}
};
double g(double y)
{
double sum = 1 - y * y + y * y * y;
return sum;
}
int main()
{
int c = 1;
int d = 2;
double y;
DefInt MyInt(c, d, g);
cout << "BySimpson:" << MyInt.BySimpson << endl << endl;
system("pause");
return 0;
}
why is there a error saying 'DefInt::BySimpson': non-standard syntax; use '&' to create a pointer to member?
By the way I ommited a similar DefInt member function,though it is nearly the same as Bysimpson, it works fine and no error occurs. I do not understand why.
I have attched it here.
double ByTrapzold(int n)
{
N = n;
double sum = f(a + (b - a) / N);
for (int i = 2; i <= N; i++)
{
sum = sum + 2 * f(a + (b - a) * i / N);
}
sum = sum + f(a + (b - a) * (N + 1) / N);
return sum * (b - a) / (2 * N);
}
Thanks.
On the line
cout << "BySimpson:" << MyInt.BySimpson << endl << endl;
You probably meant to make a call to BySimpson but your forgot the ()
cout << "BySimpson:" << MyInt.BySimpson() << endl << endl;
The reason you get this misleading error is because pre ISO standarization MyInt.BySimpson would actually mean you wanted the address just like for normal function the function name on its own gives the address of the function. Later however the use of & to take the address of a member was put in the standard as a requirement. So Visual Studio thinks you are still using the old syntax and wants you to use the new syntax.
Related
/*I need to use the result from the (delta) function inside the (sol_ec_II) function for a school assignment.*/
#include <iostream>
#include <ctgmath>
using namespace std;
double delta(double a, double b, double c) {
return (b * b) - (4 * a * c);/* so I need to take this value [(b * b) - (4 * a * c)]
and use it in sol_ec_II in the places where I wrote "delta". */
}
void sol_ec_II(double a, double b, double c) {
if (delta < 0) {//here
cout << endl << "Polinomul NU are solutii.";
}
else {
double x1 = -1 * b - sqrt(delta);//here
double x2 = -1 * b + sqrt(delta);//here
}
}
// I would also need to use the (delta) function inside the (sol_ec_II) so they use the same
a, b, c values like this:
void sol_ec_II(double a, double b, double c) {
delta(a, b, c);
if (delta < 0) {
cout << endl << "Polinomul NU are solutii.";
}
else {
double x1 = -1 * b - sqrt(delta);
double x2 = -1 * b + sqrt(delta);
}
}
//so I don't understand how to get the value that results from delta(a, b, c) and use it inside the if statement and sqrt.
The result "comes out" of the function call at the time you call it. Look, you already know how sqrt works. sqrt is a function! You write sqrt(something) and that calls the function sqrt and it calls the function sqrt with the argument something and then the return value from sqrt gets used in the place where you wrote sqrt(something). e.g. 1 + sqrt(4) calculates 3.
Likewise the return value from delta gets used in the place where you wrote delta(a, b, c). If you want to call delta and then call sqrt (i.e. calculate the square root of the delta) you write sqrt(delta(a, b, c)).
Obviously, just calculating a number is pretty useless. You probably want to do something with the number, like saving it in a variable or printing it. Examples:
cout << "the square root of the delta is " << sqrt(delta(a,b,c)) << endl;
cout << "the delta plus one is " << (delta(a,b,c) + 1) << endl;
double the_delta = delta(a,b,c);
cout << "the delta is " << the_delta << " and the square root of the delta is " << sqrt(the_delta) << endl;
if (delta(a,b,c) < 0)
cout << "the delta is negative" << endl;
else
cout << "the delta isn't negative" << endl;
Note: Every time the computer runs delta(a,b,c) it calls the delta function. It doesn't remember the calculation from last time. You can see this because if you put cout instructions inside the delta function, they get printed every time the computer runs delta(a,b,c).
Of course I will not give you the solution for your program. I hope this helps you understand how functions work.
here you should pass parameters to deleta function in order to execute it:
void sol_ec_II(double a, double b, double c) {
if (delta(a,b,c) < 0) {//here
cout << endl << "Polinomul NU are solutii.";
}
else {
double x1 = -1 * b - sqrt(delta);//here
double x2 = -1 * b + sqrt(delta);//here
}
}
or you could save the result in a new variable called result for example, and after that use it, like that:
void sol_ec_II(double a, double b, double c) {
double result = delta(a,b,c);
if (result < 0) {//here
cout << endl << "Polinomul NU are solutii.";
}
else {
double x1 = -1 * b - sqrt(delta);//here
double x2 = -1 * b + sqrt(delta);//here
}
}
The Same thing for the second function, always to execute a function use parenthesis and pass between them the arguments that the function expects.
To reuse the value you get from calling a function multiple time use a variable:
double delta(double,double,double) { return 1.2; /*ignore this for now*/ }
void sol_ec_II(double a, double b, double c) {
const auto kDelta = delta(a, b, c);
if (kDelta < 0.0) {
// do stuff
} else {
const auto kRootD = sqrt(kDelta); // same idea
const auto x1 = -b - kRootD;
const auto x2 = -b + kRootD;
// use the variables
}
}
I use auto out of habit, you don't need to, double is fine.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I have to find the value of x such that f(x)=C, where f is a monotonically increasing function over the interval [a,b]. It has to have a logarithmic complexity so I've made this function which I believe to be correct:
double search(double a, double b, double c, double (*f)(double x)) {
double pivot;
do {
pivot = abs((a-b)/2);
if (abs((*f)(pivot) - c) < 0.001) { //f(x) == c
return pivot;
} else if ((*f)(pivot) > c) {
b = pivot;
} else {
a = pivot;
}
} while (abs(a-b) != 0);
return 0;
}
which works whenever I call it like this:
int main(void) {
double a = 0.0, b = 10.0, c = 5.0;
cout << search(a, b, c, func1) << endl;
return 0;
}
but if I change it to this (the value of b):
int main(void) {
double a = 0.0, b = 100.0, c = 5.0;
cout << search(a, b, c, func1) << endl;
return 0;
}
I get a segmentation fault when the function is called. What is going wrong here?
(I can see that the program crashes when the function is called when I use the VSCode debugger. Otherwise, when I just compile and run it, it seems to get stuck in an infinite loop or something i.e. it doesn't throw the segmentation fault error).
If needed, func1 simply does return 2 * x.
EDIT:
Here's the full .cpp file if someone needs it to reproduce:
#include <iostream>
using namespace std;
double func1(double x) {
return 2 * x;
}
double func2(double x) {
return x + 0.5;
}
double search(double a, double b, double c, double (*f)(double x)) {
double pivot;
do {
pivot = abs((b-a)/2);
if (abs((*f)(pivot) - c) < 0.1) { //f(x) == c
return pivot;
} else if ((*f)(pivot) > c) {
b = pivot;
} else {
a = pivot;
}
} while (abs(b-a) != 0);
return 0;
}
int main(void) {
double a = 0.0, b = 10.0, c = 5.0;
cout << search(a, b, c, &func1) << endl;
cout << search(a, b, c, func1) << endl;
return 0;
}
where changing the value of b to 100.0 will cause the fault to occur.
pivot = abs((a-b)/2); is wrong it as it computes half of the distance from a to b not the middle point of them, use:
pivot = abs((a-b)/2)+a; // provided that *a* is less than *b*
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am getting the error 'cout does not name a type' (line 36 ie main function) along with a couple of other errors in main().
All code is wrapped well within the functions and I have used
using namespace std;
but I am still getting this error along with other ~identifier~ was not declared in this scope errors in the main function.
Code:
#include<iostream>
#include<cmath>
using namespace std;
double discriminant (double a, double b, double c){
return (pow(b,2) - (4 * a * c));
}
double* compute_roots(double a, double b, double c){
double* x;
double x1,x2;
double d = discriminant(a,b,c);
if (d>0){
cout<<"Two real roots"<<endl;
}
else if (d=0){
cout<<"One unique solution"<<endl;
}
else
{
cout<<"Does not support complex roots";
//throw "Negative roots!";
return x;
}
x1 = (-b + sqrt(d))/(2*a);
x2 = (-b - sqrt(d))/(2*a);
x[0] = x1;
x[1] = x2;
return x;
}
int main{
double a=2.0,b=5,c=3.1;
double* res=compute_roots(a,b,c);
cout<<res[0];
cout<<res[1];
return 0;
}
int main{
That should be
int main() {
Otherwise the compiler thinks you're trying to define an integer variable called main, not a function, and will get very confused by the code that follows.
Also, compute_roots never initializes its local variable x before using its value, so that can't work:
double* x;
// ...
return x;
Another problem:
else if (d=0){
should probably be d == 0 (= is for assignment, not comparison).
the main function misses (). and you are using d = 0 as the condition for else if not an error but it is not what you want. you also have memory leak.
this is probably what you need:
#include<iostream>
#include<cmath>
#include <memory>
using namespace std;
double discriminant(double a, double b, double c) {
return (pow(b, 2) - (4 * a * c));
}
void deleter(double* x) {
delete[] x;
}
shared_ptr<double> compute_roots(double a, double b, double c) {
shared_ptr<double> x;
x.reset(new double[2], deleter);
double x1, x2;
double d = discriminant(a, b, c);
if (d > 0) {
cout << "Two real roots" << endl;
}
else if (d == 0) {
cout << "One unique solution" << endl;
}
else
{
cout << "Does not support complex roots";
//throw "Negative roots!";
return x;
}
x1 = (-b + sqrt(d)) / (2 * a);
x2 = (-b - sqrt(d)) / (2 * a);
x.get()[0] = x1;
x.get()[1] = x2;
return x;
}
int main() {
double a = 2.0, b = 5, c = 3.1;
shared_ptr<double> res = compute_roots(a, b, c);
cout << res.get()[0];
cout << res.get()[1];
return 0;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
What is the fastest way of calculation x in below equation in c++?
sin (a x) + b sin (c x)== d * x + e
I don't need a very exact value of x. the approximation with 0.001 is acceptable. I also know an interwal for the solution [x_0,x_1].
I know Newton method but as I am going to simulate a system and I need to solve if thousand of times, I don't know how to give the first solution
You can reformulate your equation as
sin (a x) + b sin (c x) - d * x - e == 0
Now, this is a root finding problem. Here is a list for root finding algorithms.
Newton's method is very fast and easy to implement, since the derivative of your equation can be calculated analytically.
#include <array>
#include <iostream>
#include <cmath>
template <typename T> double func(const T ¶meter, const double x) {
const auto a = parameter[0];
const auto b = parameter[1];
const auto c = parameter[2];
const auto d = parameter[3];
const auto e = parameter[4];
return sin(a * x) + b * sin(c * x) - (d * x + e);
}
template <typename T> double derivative(const T ¶meter, const double x) {
const auto a = parameter[0];
const auto b = parameter[1];
const auto c = parameter[2];
const auto d = parameter[3];
return a * cos(a * x) + b * c * cos(c * x) - d;
}
template <typename T> double solve(const T ¶meter) {
double x = 0.0;
const double eps = 1e-9;
while (fabs(func(parameter, x)) > eps) {
x = x - func(parameter, x) / derivative(parameter, x);
}
return x;
}
int main() {
const std::array<double, 5> parameter{1.1, 1.2, 0.9, 0.1, 0.1};
const auto x = solve(parameter);
std::cout << "solution is x=" << x << " f(x)=" << func(parameter, x) << '\n';
}
Go from double to float to speed it up, if your desired accuracy allows that.
I assume you are looking for a numerical solution, given the known parameters a,b,c,d,e. An approximate solution can be found by this dirty iteration.
But it is not warrantied to converge for all values of the parameters.
The only way to do that is to give an analytic upper bound and lower bound for the solution and iterate with bisection root finding.
#include<numeric>
#include<iostream>
#include<cmath>
using std::cout;
int main(){
auto a = 1.1;
auto b = 1.2;
auto c = 0.9;
auto d = 0.1;
auto e = 0.1;
auto N = 1000;
auto x = 0.;
for(int n = 0; n != N; ++n)
x = 0.999*x + 0.001*(sin(a*x) + b*sin(c*x) - e)/d;
cout << sin(a*x) + b*sin(c*x) << " == " << d*x + e << '\n';
cout << "solution is x = " << x << '\n';
}
(for simplicity this is C++11)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to write a function that can calculate me inetegral e^(cos x) on range (a,b)
double integral(double(*f)(double x), double a, double b, int n) {
double step = (b - a) / n; // width of each small rectangle
double area = 0.0; // signed area
for (int i = 0; i < n; i ++) {
area += f(a + (i + 0.5) * step) * step; // sum up each small rectangle
}
return area;
}
This is what I have found but I`m new in c++ and I can't work with pointers
if there is another way please help me.
That function you found allows you to integrate any function you want, and that funcion is the first parameter of the integral method. You can just remove the first argument ('double(*f)(double x)' ), that is because the function you want to integrate is known ( e^cos(x)), so you don't need to give it as an argument. Then, in the for loop, you just replace de f function for e^cos(x). The method will look like this:
double integral(double a, double b, int n){
double step = (b - a) / n; // width of each small rectangle
double area = 0.0; // signed area
for (int i = 0; i < n; i ++) {
area += exp(cos(a + (i + 0.5) * step)) * step; // sum up each small rectangle
}
return area;
}
#include <functional>
template<typename T>
T integral(const std::function<T(T)>& f, T a, T b, int n) {
auto step = (b - a) / n; // width of each small rectangle
auto area = static_cast<T>(0); // signed area
for (auto i = 0; i < n; i++)
{
// sum up each small rectangle
area += f(a + (i + static_cast<T>( 0.5)) * step) * step;
}
return area;
}
int main()
{
std::function<float(float)> f_sine = [](float in) { return sin(in); };
auto two = integral(f_sine, 0.0f, 3.14f, 20);
return 0;
}
That will be $3.50