1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ Mid-term exam tomorrow! Please help!

Discussion in 'BlackHat Lounge' started by Designerdude13, Oct 3, 2012.

  1. Designerdude13

    Designerdude13 Power Member

    Joined:
    Nov 22, 2010
    Messages:
    646
    Likes Received:
    190
    Gender:
    Male
    Code:
    
    #include <iostream>
    using namespace std;
    
    int f(int x);
    int g(int x);
    
    int main() 
    {
      int x = 1;
      cout << f(x) + g(x);
      return 0;
    }
    
    int f(int x)
    
    {
      x = x + 1;
     cout << x;
     return g(x);
    }
    
    int g(int x)
    
    {
      x = x + 1;
     cout << x;
     return x;
    }
    Okay I don't see how this prints out 2325

    I've started out by doing this
    f(x)
    1 + 1 equals 2...then I sent the value to g(x)

    now for g(x) x equals 2 + 1 which is 3
    ....now i returned 3 back to main...but i dont see how 5 is coming into play with this? I also thought that you can't return values to main???

    Very confused please help
     
    Last edited: Oct 3, 2012
  2. umerjutt00

    umerjutt00 Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 28, 2011
    Messages:
    3,650
    Likes Received:
    1,910
    Occupation:
    Ninja
  3. zoulou

    zoulou Newbie

    Joined:
    Oct 29, 2008
    Messages:
    20
    Likes Received:
    2
    In have in the order :

    in the main :
    from f(x) -> cout << x -> which gives 2
    then it goes to g(x) from f(x) where you have cout<< x -> which gives 3
    at this moment bear in mind that f(x) return 3

    you're now back in the main :
    from g(x) -> cout << x -> which gives 2
    and g(x) return 2

    and finally you have cout << f(x) + g(x) that gives 3 + 2 = 5

    So the output is 2325.

    The idea is to understand that from the main, you evaluate f(x) (and in this f(x) you have also a g(x)), then g(x) and then cout<<f(x) + g(x).
    I hope it's not too confusing.
     
  4. safex

    safex Jr. VIP Jr. VIP

    Joined:
    Dec 28, 2009
    Messages:
    3,433
    Likes Received:
    518
    Occupation:
    Search Engine Optimization
    Location:
    BLCVA.com
    Home Page:
    Better watch some Video Tutorial about C++ to get proper solution of your problem..
     
  5. Designerdude13

    Designerdude13 Power Member

    Joined:
    Nov 22, 2010
    Messages:
    646
    Likes Received:
    190
    Gender:
    Male

    YES! Thank you :D
     
  6. 0scan0

    0scan0 Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 9, 2010
    Messages:
    166
    Likes Received:
    42
    Location:
    1.1.3
    How is the exam structured? Home exam? In that case you could just hire someone over at fiverr or craigslist to do the work for you.
     
  7. Designerdude13

    Designerdude13 Power Member

    Joined:
    Nov 22, 2010
    Messages:
    646
    Likes Received:
    190
    Gender:
    Male
    Lol no im in college at a university...plus i would never do that because it will come to bite me back in the end