functions and maths. correct my code, please

Q: given real numbers X and Y, solve(or calculate) f(x,5) + f(y,5) + max(f(x^3 - y^3, 3), x^3), where f(a,b)=(a+b)^2 + (a^2/b^2).

so it's like f(x,5)=(x+5)^2 + (x^2/5^2) etc

i just dont know the right way to find max and how to use function.

my friend tried to help me but eh, he wasnt sure either xD

could you please correct my code so it works properly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cmath>
#include<algorithm>

using namespace std;

int  math(float a, float b, float t) 
{
	float q = pow(( a - b), 2);
	float w = (pow(a,2)) / (pow(b,2));
	t = q + w;
	return  0;
}

int main() {
	float x, y, temp1, temp2, temp3 ;
	float t;
	cout << "enter x, y\n==> ";
	cin >> x >> y;
	math(x, 5 );
	temp1 = t;
	math(y, 5 );
	temp2 = t;
	max(math((pow(x, 3)) - (pow(y, 3)), 3), (pow(x, 3)) );
	temp3 = t;
	cout << "result = " << temp1 + temp2 + temp3 << endl;
	system("pause");
	return 0;
}
Last edited on
Q: given real numbers X and Y, solve f(x,5) + f(y,5) + max(f(x^3 - y^3, 3), x^3), where f(a,b)=(a+b)^2 + (a^2/b^2).


What do you mean by "solve"? That isn't an equation (as it stands).
@lastchance the literal translation of it is solve or calculate, don't know what else it might mean :]
@laura fidarova,

You have defined a (composite) function. You haven't said what that function is equal to. So it is not an EQUATION: it is (currently) the definition of a function.

It's like saying
"solve x+3"

That is meaningless. However, if you said
solve x+3 = 0
then you would have an EQUATION (note the "equals" sign) that could be solved.




Why not writing it like suggested:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>
#include<algorithm>

using namespace std;

double f(double a, double b) 
{
	return (a + b) * (a + b) + ((a * a) / (b * b));
}

int main() {
	double x, y;
	cout << "enter x, y\n==> ";
	cin >> x >> y;
	cout << "result = " << f(x, 5) + f(y, 5) + std::max(f(x * x * x - y * y * y, 3), x * x * x);
	system("pause");
	return 0;
}
Not tested
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

double f(double a, double b)
{
    return (a+b)*(a+b) + (a*a)/(b*b);
}

double max(double a, double b)
{
    if(a > b)
        return a;
    else
        return b;
}

int main()
{
    std::cout << f(3,4) << '\n';
    
    double x{5};
    double y{6};
    std::cout << f(x,5) + f(y,5) + max( f(x*x*x - y*y*y, 3), x*x*x ) << '\n';

    return 0;
}



49.5625
8887.55
Program ended with exit code: 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
	const auto p2 {[](auto a) {return a * a; }};
	const auto p3 {[&p2](auto a) {return p2(a) * a; }};
	const auto f {[&p2](auto a, auto b) {return p2(a + b) + p2(a) / p2(b); }};
	double x, y;

	cout << "Enter x y: ";
	cin >> x >> y;

	const auto x3 {p3(x)};

	cout << "result = " << f(x, 5) + f(y, 5) + max(f(x3 - p3(y), 3), x3);
}



Enter x y: 5 6
result = 8887.55

Last edited on
guess some of you misunderstood me. so i added some info. but thank you for your codes

Q: given real numbers X and Y, solve(or calculate) f(x,5) + f(y,5) + max(f(x^3 - y^3, 3), x^3), where f(a,b)=(a+b)^2 + (a^2/b^2).

so it's like f(x,5)=(x+5)^2 + (x^2/5^2) etc
Isn't that what the above codes have calculated?
That does seem to be what all three codes calculate.

It was only I who misunderstood. I got thrown by the word "solve" in the original post. I guess I should have looked closer at the OP's code and less at the question.
Last edited on
@seeplus, it is, thats why i said some, and it's okay:) thanks for everything !
Topic archived. No new replies allowed.