CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: C Test

  1. #1
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    335

    C Test

    Recently I was questioned about a test in C that I couldn´t find the answer. Can anybody Help me to find it?

    Code:
    ...
    int i,num = 42;
    for (i=0 ; i < num ; i--)
      printf("*");
    ...
    The intent is print 42 *. the question is : "Can you fix this code changing just one character? You can insert OR remove OR change just one character of code. "
    i++ would solve the problem ,so certainly it has something with boolean operators... As it was not enougth the problem pointed that there were 3 solutions , how many can you see?
    thank you.

  2. #2
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: C Test

    Make num -42

    int i, num = -42;

    then starting at zero and counting down to -42 will give you 42 *'s

    edit: nevermind, I just saw the operator is < and not !=
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  3. #3
    Join Date
    May 2005
    Location
    Bangalore,India
    Posts
    29

    Re: C Test

    1. for(i=0; -i<n; i--)

    2. for(i=0; i+n; i--)

    3. for(i=0; i<n; n--)

  4. #4
    Join Date
    Sep 2004
    Location
    Israel
    Posts
    101

    Talking Re: C Test

    I found 2 of 3 options to solve this problem :
    1). add - to the 42 value
    so it'll be -42
    2). change the < to >

    yes yes yes.....
    BabyGuru

  5. #5
    Join Date
    Apr 2002
    Location
    PA, USA
    Posts
    1,658

    Re: C Test

    clever rajesh, nice... 1 and 2 will work just fine, but 3 doesn't work, given the variable name. If the variable name matters ,then that's changing too much. But we'll have to hear back from Rabelo to find out.
    =--=--=--=--=--=--=--=--=--=--=--=--=--=
    Please rate this post to show your appreciation for those that helped you.

    Before You Post A Question, Please Read This: How & When To Ask Your Question
    =--=--=--=--=--=--=--=--=--=--=--=--=--=

    -eli
    http://www.toad-software.com
    http://www.dailymission.com - Do It Daily

  6. #6
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: C Test

    change i-- to n--
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  7. #7
    Join Date
    Feb 2003
    Location
    Brazil
    Posts
    335

    Re: C Test

    wow rajesh_krec !!! Congratulations!!! (the variable name dosen't matter)

    Baby Guru: ... ( Just one character....)
    Eli Gassert: The first I tried. Doesn´t work because 0 > -42...
    sunnypalsingh : Nice shot!!!

    I need read more about boolean operators... Anyway I think that it is not a C test, it fits well on an assembler test because i++ would solve the problem without transform the program in a mess for non boolean experts.

    Thank you !!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured