Thread: Math/Programming Puzzle: Use of functions

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    71

    Math/Programming Puzzle: Use of functions

    This little *puzzle* I made are for those C/C++ programmers who are good with math and such. In the following source code, I will make a bunch of functions that will take a constant numeric variable and perform mathematical operations with that constant variable. Now, the purpose of the puzzle is for you programmers to use those functions to reach a certain number. You could use the function as many times as you'd like.

    Here is an example:
    const variable= 9;
    goal to reach= 63;

    Code:
    #include <iostream>
    
    using std::cout;
    
    int a( int );
    int b( int );
    
    int main( int argc, char *argv[] )
    {
       const int cNum= 9;
       int tempNum;
    
       
       tempNum= a( cNum );
       tempNum= b( cNum ) + a( tempNum );
    
    
       cout << tempNum; 
    
       cin.get();
       return 0;
    }
    
    
    int a( int Number )
    {
       return( Number * 2 );
    }
    
    int b( int Number )
    { 
       return( Number * 3 );
    }
    I hope you understand the concept. I will post the first *mission* next.
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    You cannot make a new function. You cannot use a pre-made function from your compiler's include folder. You could set up a new variable that will help you also. Here is the first *mission*.

    const variable: 6;
    goal: 27;

    Code:
    #include <iostream>
    
    using std::cout;
    
    int a( int );
    int b( int );
    
    int main( int argc, char *argv[] )
    {
       const int cNum= 6;
    
       // Do your *stuff* here
       // Yep.....
    
       cin.get();
       return 0;
    }
    
    int a( int Number )
    {
       return( Number * 3 );
    }
    
    int b( int Number )
    {
       return( Number / 2 );
    }
    When you call my functions you have to use cNum as a parameter(or another variable that is somehow equal to cNum. ex: int c= cNum * 9; c= b( c ) + cNum; something like that).
    Last edited by KingZoolerius66; 10-03-2003 at 04:14 PM.
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    10
    Am I the only one who thinks he is trying to trick us into doing his homework?

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    Yeah, you're the only one. I don't have any computer science classes at my school, nor do I have anything related to this at my school.

    This isn't homework. This is a fun little puzzle I came up with and just thought I'd post it at the cboard.
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by procyon4476
    Am I the only one who thinks he is trying to trick us into doing his homework?
    Yes, but I don't think this "challenge" is interesting enough to be worth it.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Interesting puzzle. I got an answer for your first one.

    By the way, your code needs a using directive for cin.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    no it doesn't.

    I never used cin so I don't need an using directive.
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  8. #8
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You're right. cin.get( ) doesn't use cin at all.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  9. #9
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    Oops, I feel like an idiot. If you're able to solve that simple puzzle then construct your own puzzle for the next poster to solve and then so on.

    Keep the ball rolling
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    10
    frist: tempNum = a(b(cNum))+cNum;

    second: tempNum = b(a(a(cNum)));

    i'll come up with one in a minute...
    ???

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    10
    using the functions from the second one:

    Code:
    int a( int Number )
    {
       return( Number * 3 );
    }
    
    int b( int Number )
    {
       return( Number / 2 );
    }
    source = 51

    goal = 7
    ???

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    10
    also, see what's the least number of function calls you can get them down too. for the one i posted, i got it down to 5 so far.
    ???

  13. #13
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    I got it down to 6 on my first try but then tried again and got it.

    Code:
    int main( int argc, char *argv[] )
    {
         // goal: 7
         const int cNum= 51;
         int TempNum;
    
    
         TempNum= a(a(cNum));
         TempNum= b(b(b(b(b(b(TempNum))))));
    
    
         cout << TempNum;
    
         cin.get();
         return 0;
    
    }
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  14. #14
    Registered User
    Join Date
    Sep 2003
    Posts
    71
    Heres one:
    You may use any standard C++ functions from any header. You may use the utility function provided. The '=' character must appear exactly once (when you assign to fillme). You may declare extra POD-type variables.

    Code:
    // Source: 4;
    // Goal: 20;
    
    
    // utility function:
    template <class T, size_t N> 
    T* a(T(&b)[N]) 
    { 
       return b+N;
    }
    
    int main()
    {  
       const int num = 4;
       int fillme;    
       // (insert code here)  
    
       cout << fillme; 
       cin.get();    
       return 0;
    }
    Name: Eric Lesnar
    Learning: C/C++, SDL, WinAPI, OpenGL, and Python
    Compiler: Dev-C++ 4.9.0
    Current Game Project: Acoznict

  15. #15
    Registered User
    Join Date
    Aug 2003
    Posts
    10
    for the one i posted, here is a way to do it with 6 function calls, i can't remember how i did it last night with 5 right now =/. I'll have to come up with it again.

    b(b(b( (b(b(b(cNum)))+cNum) )))
    ???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  3. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  4. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM