functions and maths. correct my code, please

Nov 18, 2020 at 11:30am
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 Nov 18, 2020 at 3:25pm
Nov 18, 2020 at 11:40am
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).
Nov 18, 2020 at 12:31pm
@lastchance the literal translation of it is solve or calculate, don't know what else it might mean :]
Nov 18, 2020 at 12:38pm
@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.




Nov 18, 2020 at 12:43pm
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
Nov 18, 2020 at 12:46pm
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
Nov 18, 2020 at 1:26pm
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 Nov 18, 2020 at 1:35pm
Nov 18, 2020 at 3:28pm
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
Nov 18, 2020 at 5:19pm
Isn't that what the above codes have calculated?
Nov 18, 2020 at 5:58pm
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 Nov 18, 2020 at 6:21pm
Nov 18, 2020 at 8:04pm
@seeplus, it is, thats why i said some, and it's okay:) thanks for everything !
Topic archived. No new replies allowed.