Thread: interesting problem

  1. #1
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342

    interesting problem

    hi, its been some time since i have posted anything on this forum.. anyway, here goes:

    Code:
    
     void abc(void)
       {
    
         < TO BE FILLED UP >
      
      }
    
     int main()
       {
         int i=10;
         abc();
         i=1;
         printf(" i = %d\n",i);
        return 0;
       }
    This program should print

    Code:
      
       i=10

    The problem is to write the function abc() to get the required output

    The function abc() takes in no arguments and returns void..

    **** WE ARE NOT ALLOWED TO USE "printf()" INSIDE abc() ****


    This is what i could think of :
    Instructions are stored in a contiguous manner.. If I can cleverly modify Program Counter to skip the statement
    Code:
     
       i=1;
    and continue execution from the statement ,
    Code:
        printf(" i = %d\n",i);
    The problem can be solved..

    I have no idea how to actually do it.. Can someone help me? Or is there a better method?
    In the middle of difficulty, lies opportunity

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I have no idea. if abc() could take arguements, then I could think of a that could sort of work (change i after i=1, but before the printf call). If abc() can be a macro you write another function that takes the address of i as a parammiter and uses that. eg:

    Of course the are other ways to print to the screan besides printf(). But I assume that's not what you're asking.
    Last edited by King Mir; 08-25-2006 at 11:16 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    >Of course the are other ways to print to the screan besides printf(). But I assume that's not what you're asking
    yeah.. we are not allowed to actually use calls to prinf stuff onto the screen.. and
    abc() is given to be a function..
    In the middle of difficulty, lies opportunity

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    If abc() has to be called before i=1; then I deem this impossible. Otherwise you could make i a global variable and just have abc() set it to 10.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    If you make i a global variable, then you could make a seperate thread change i to 10 several times over. Chances are it will be 10 before printf() is called.

    The other idea I have is if you could ask windows for a pointer the the program stack, where i is stored. By I don't know if that's possible.

    But in short, there is no standard way to skip a C command like that.
    Last edited by King Mir; 08-25-2006 at 11:23 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Here's a thought: Somewhere I read that static variables are stored in memmory just before stack variables. If this is true, you could create a static variable inside abc(). Get the address, increment it, and you have the adress of i. The rest is the same as above.

    I have real doubts that that would work, but it's a thought.

    Edit: yeah, this did not work on my compiler.
    Last edited by King Mir; 08-25-2006 at 11:39 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    well, this appeared in an interview of a famous software company.. I guess the problem is not unsolvable..
    What we could think of was : Is there anyway we can get the address where "main()" starts? Does anyone know how many bytes are allocated for each instruction? Is it one "word" per instruction?
    Then we can somehow create a skip as required.. I am not able to get anything concrete out of this though..
    > If you make i a global variable
    " i " is declared inside main() as per the pproblem description.

    >If abc() has to be called before i=1;
    Yes that unfortunately is the case..
    In the middle of difficulty, lies opportunity

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The intended answer most likely makes you define undefined behavior, so it's a bad idea to learn it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    hmmm.... so, is there really a way out of this?
    In the middle of difficulty, lies opportunity

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Yeah, there is definately no standard way to do this. If this is part of an interview then it is a question about the language, not about your knowledge of how to get around it.

    So the correct answer is that there is way to skip a command in C like that.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    >>So the correct answer is that there is way to skip a command in C like that.
    duh?? was that a typo? or am i too sleepy?
    In the middle of difficulty, lies opportunity

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Maybe I mis-skimmed some of the early replies. Perhaps this question is looking for proprocessor abuse. Something like:
    Code:
    void abc(void) 
    {
       #define printf(x,y) puts(" i = 10")
    }
    Or maybe,
    Code:
    void abc(void)
    {
       puts(" i = 10"), exit(1);
    }
    If so, what are they teaching you instead of how to code?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Quote Originally Posted by Dave_Sinkula
    Maybe I mis-skimmed some of the early replies. Perhaps this question is looking for proprocessor abuse.
    Code:
    void abc(void)
    {
       #define printf(x,y) puts(" i = 10")
    }
    If so, what are they teaching you instead of how to code?
    Wouldn't that macro only be available inside abc()?

    edit: Seems not. :|
    Last edited by OnionKnight; 08-26-2006 at 12:27 AM.

  14. #14
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    yeah, something like that does work after all.. But I think I had written that
    " we are not allowed to actually use calls to print stuff onto the screen.."
    so, we have to mess around with the mem to get it done I guess
    In the middle of difficulty, lies opportunity

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Not this stupid exercise again

    > Or is there a better method?
    Find a better book, or find a better teacher.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting problem of qsort()
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2008, 12:09 PM
  2. Interesting Problem with IOCP and AcceptEx() :: Winsock
    By kuphryn in forum Networking/Device Communication
    Replies: 0
    Last Post: 10-07-2003, 09:16 PM
  3. Interesting number theory problem
    By Zach L. in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-20-2003, 07:45 AM
  4. Interesting virtual function problem.
    By Sebastiani in forum C++ Programming
    Replies: 12
    Last Post: 09-02-2003, 10:08 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM