Please help, i have a midterm tmrw and couldn't figure it out

0 Yaz Qattan · March 25, 2015
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.

#include <iostream>

using namespace std;

int my_fun(int perfect)
{
    {
        int perfect = 0;
        return ((perfect – 1) * (perfect – 1));
    }
    return perfect;
}

int main()
{
    for (int i = 0; i < 4; i++)
    {
        cout << my_fun(i) << " ";
    }
    return 0;
}



Answers:
A. 
-1 0 1 4


B. 
1 1 1 1


C. 
1 0 1 4


D. 
0 0 0 0

the right answer turns out to be B

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · 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.

if(x==2)
y=3;
else
y=4;

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. 
  • 1

C++

106,968 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator