Thread: 2 Qns on for loop

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    2 Qns on for loop

    i need help on the 2 questions below. Thanks a lot.


    What is printed out by the following C code fragment?
    int i, j, k=0;
    for (i = 1; i <= 100; i*=2)
    for (j = 1; j < i; j++)
    k++;
    printf("%i\n", k);
    (A) 0
    (B) 120
    (C) 127
    (D) 128
    (E) None of the above.


    What is printed out by the following C code fragment?
    int i = -16;
    for (i++; i++; i++)
    i++;
    printf("%i\n", i);
    (A) 0
    (B) 1
    (C) 2
    (D) Infinite loop.
    (E) None of the above.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Write a program for each snippet and find out.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    do u mean adding printf in between?

    but these 2 questions are so difficult to do so

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    i shd add that i know the ans but dont know the reasoning

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. Yes, then the idea is to trace the program such that you can understand what it does. Using printf statements is one way, but a possibly easier way is to use a debugger.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > i shd add that i know the ans but dont know the reasoning
    Just wait until you have to logically deduce what's going on later on - the practice now will serve you well.
    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.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Any time I need to logically deduce something, I make a post on the internet asking people to do it for me. Works every time!


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    i know this isn't relevant.... but this post is an utter fail.

    Here's an example that would have made just as much sense:

    "Hey do you know?"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with simple qns.
    By sKeL` in forum C Programming
    Replies: 1
    Last Post: 09-23-2009, 02:45 AM
  2. Some qns to Bubba on MTW terrain engine
    By Frederic in forum Game Programming
    Replies: 11
    Last Post: 12-20-2008, 01:23 PM
  3. Compiler qns??
    By charisma in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 12:44 PM
  4. newbie qns
    By monkeymon in forum Windows Programming
    Replies: 1
    Last Post: 10-24-2002, 07:14 AM