i got stuck on this problem code and couldn't figure out how and why it worked, please help explaining it as soon as you see it
i'm a beginner in C++ and it's the first time i see 2 return statements in a function and just my_fun looks weird to me.
thanks in advance.
using namespace std;
int my_fun(int perfect)
int perfect = 0;
return ((perfect – 1) * (perfect – 1));
for (int i = 0; i < 4; i++)
cout << my_fun(i) << " ";
-1 0 1 4
1 1 1 1
1 0 1 4
0 0 0 0
the right answer turns out to be B
Please help, i have a midterm tmrw and couldn't figure it out
Post a Reply
|Oldest Newest Rating|
· March 25, 2015
Please repaste your code using the code tags. Honestly, I can't tell if some of the syntax is right or if it simply malformed. The ? is usually used in ternary expressions like y=(x==2?3:4).
A ternary expression is more compact syntax for an if else statement. Let me show that ternary expression in a more logical form.
As for why it is 1 1 1 1. Like I said, unless the code is placed within code tags or the ? is done properly, I can't really help. If the ? is supposed to be there by itself like perfect?1, I have no idea.
Used in many types of software including music players, video games, and many large scale applications.
|Bucky Roberts Administrator|