+ 1
Explanation needed!!! (C++ question)
While solving C++ problems, stuck in this. ——-code—— int func(int a, int b) { return (a*(a>b) + b*(a=<b)); } The solution said that this function is to solve maximum of ‘a’ and ‘b’. Anybody can explain me what is “maximum” and formula????? :((
6 Antworten
+ 5
You're very welcome, well I guess we could say the problem description wasn't really clear, but considering it was a quiz I suppose it happens to be.
P.S. Please I'm not a sir BTW 😁
+ 5
Let's have a test example, shall we ...
Let's say we pass 1 & 2 as arguments, then we'll see what happens in the function ...
int func(int a, int b)
{
return (a*(a>b) + b*(a=<b));
}
Knowing that:
a = 1
b = 2
(a * (a > b) + b * (a <= b))
(a > b) and (a <= b) are evaluated as boolean. (a > b) yields false (0), (a <= b) yields true (1)
(a * (0) + b * (1))
(1 * (0) + 2 * (1))
( 0 + 2 )
Outputs 2, the max of the two values we had in function arguments (1,2).
Hth, cmiiw
+ 3
oh.... maximum means which value is higher... jesus.. the question was showing 4 multiple choices and couple of them was “about maximum of a and b” and “about comparison of a and b”. then I chose the comparison thing. but it was wrong. so I thought that this question would be more difficult than what I thought... like real mathematic question. btw thanks for great explanation sir. appreciated
+ 2
yeah yeah you are right. my bad.
+ 2
so I mean what is the maximum thing? the code was not an issue. I dont get that formula means. Pls help !
+ 2
haha thank you ‘bro’ :)