sample questions

This is a discussion on sample questions within the C Programming forums, part of the General Programming Boards category; Hey,,, I was answering some sample questions and came across this problem. Its based on simple 'for' loop. Code: int ...

  1. #1
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151

    sample questions

    Hey,,,
    I was answering some sample questions and came across this problem. Its based on simple 'for' loop.

    Code:
    int i, n = 20;
    for(i = 0;i < n;i--)
    printf(“-“);
    I have to change only one character to make this program work. The solution says there are 3 ways to solve this problem.

    I have tried to work around this. But I need to atleast to change 2 characters.

    Please help
    Last edited by Ron; 06-21-2008 at 03:54 PM.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    You failed to mention what the problem was or what you were expecting it to do?
    Is the problem something along the lines of that illegal double quote character in the printf (“)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    as seen in the code....
    the code will run an infinite loop because with i--, i will always be less than n

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    Oh no, it probably won't be an infinite loop. It will stop after a while, I'm guessing, but probably not as you want.
    Last edited by Elysia; 06-21-2008 at 04:09 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    One way: add a semicolon to the end of the for loop. The compiler should be able to optimise that loop out, assuming you enable optimisations.

    Another way: initialize i to 90 instead of 0, by adding a "9". The loop will never execute. (I know this is adding instead of changing, but still . . . .)

    You could always change the "<" to ">", to get the loop to never execute.

    But I think the answer they're looking for is to change "i--" to "n--". That's your way, Elysia.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    It's amazing how such apparent solutions can be transparent when you aren't the one writing the code. And even if you, they can still be transparent.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I only thought of it by chance. I was trying to devise a way to get a truly infinite loop, and I thought that the only way to get that would be to prevent i from underflowing, and the only way to do that would be to change another variable instead, i.e. n. By coincidence, that makes the loop execute 20 instead of infinite times.

    [edit] I think neither of us noticed it immediately because that's not the way for loops usually work. I guess it's a good question in that it makes you think about that. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    that was smart changing i-- to n--

    But initializing i to 90 wont work as i<n (n is 20)
    Code:
    for(i = 0;i < n;i--)
    OK the purpose of this program is to print 20 dashes.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    But initializing i to 90 wont work as i<n (n is 20)
    It depends on what you mean by "work". I stated that it would make the loop never execute, which it does.

    Code:
    for(i = 0;i < n;i--)
    OK the purpose of this program is to print 20 dashes.
    Right, that's what we guessed . . . but stating that at the beginning would have made responses to your post much more useful.

    Anyway . . . I can't see what the other two solutions might be at the moment.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    FOSS Enthusiast
    Join Date
    Jun 2008
    Location
    Germany
    Posts
    64
    The second solution is
    Code:
    int i, n = 20;
    for(i = 0; i + n; i--)
    this for loop will be executed while (i + n) != 0 (integral promotion!)

    I just figured a third solution out: Though it prints just 19 hypens:
    Code:
    int i, n = 20;
    for(i = 0; ~i < n; i--)
    that'd be bitwise complement. But I dont know if this is okay, because it laks one hypen.
    Last edited by mkruk; 06-21-2008 at 07:00 PM. Reason: added 3rd solution

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    3rd solution
    Code:
    for (i=0; i % n; i--)
    printf("-");
    I suppose it works, if there is no problem with negatives and %

  12. #12
    FOSS Enthusiast
    Join Date
    Jun 2008
    Location
    Germany
    Posts
    64
    Quote Originally Posted by C_ntua View Post
    3rd solution
    Code:
    for (i=0; i &#37; n; i--)
    printf("-");
    I suppose it works, if there is no problem with negatives and %
    nope. on the first execution this will be 0 / 20, which is 0 and a remain of 0.
    because of integral promotion (0 is FALSE, 1 is TRUE), this will evaluate FALSE and stop the loop.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,039
    Code:
    int i, n = 20;
    for(i = 0;-i < n;i--)
    printf(“-“);
    Last edited by King Mir; 06-21-2008 at 08:10 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.

  14. #14
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    I dont think 2nd anbd 3rd soltion are rght. THese are repetitive loops that will never end.

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    mkruk's first solution and King Mir's solutions both work, I think.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM
  5. Looking for sample MC test questions for a job
    By garp in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 05:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21