Thread: CAN ANYONE HELP ME to write a c program using only "one for loop" and without using a

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    1

    CAN ANYONE HELP ME to write a c program using only "one for loop" and without using a

    1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 100

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I'm declaring this the worst question of the month, and it's only the first day!

    "...and without using a..."
    You put the entire question in the title, but it lost key information at the end.
    Using a what?
    - Power Drill
    - Sheet folder
    - copy of TurboC

    > 1 2 4 3 6 9 4 8 12
    I'm guessing there is some formatting going on here, but who the hell would know from your hasty puke on the forum.


    Here, read this.
    How To Ask Questions The Smart Way
    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.

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    If I had to take a shot in the dark, I'd say they were going for "without using a goto".

    EDIT: A like for anyone who decipher those numbers
    Last edited by Yarin; 09-01-2013 at 12:57 PM.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Look at it this way and it becomes obvious.
    Code:
    1
    2 4
    3 6 9
    4 8 12 16
    ...
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    My guess is this.
    Code:
    1 
    2 4 
    3 6 9 
    4 8 12 16 
    5 10 15 20 25 
    6 12 18 24 30 36 
    7 14 21 28 35 42 49 
    8 16 24 32 40 48 56 64 
    9 18 27 36 45 54 63 72 81 
    10 20 30 40 50 60 70 80 90 100
    My answer is 2 lines of code inside a single for loop.
    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.

  6. #6
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I'm just not as smart as you guys.

    Code:
    #!/usr/bin/perl
    
    #1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 
    #16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 
    #100
    
    #argh. I can't see a pattern...oh well.
    for my $i (1 .. 55){
      if($i == 3){
        print '4';
      }elsif($i == 4){
        print '3';
      }elsif($i == 5){
        print '6';
      }elsif($i == 6){
        print '9';
      }else{ #etc...
        print $i;
      }
    }

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Salem View Post
    My answer is 2 lines of code inside a single for loop.
    care to share a hint about how you solved this with two lines of code?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    care to share a hint about how you solved this with two lines of code?
    I have no idea how Salem did it, but I was able to make it happen using some comma-operator trickery.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    PM'ed code to Elkvis and Matticus
    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.

  10. #10
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Salem View Post
    PM'ed code to Elkvis and Matticus
    May I have take a gander at that too, please?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Matticus View Post
    I have no idea how Salem did it, but I was able to make it happen using some comma-operator trickery.
    Well, if you use the ternary operator, you can easily do it with a single instruction (in the body of the loop).

    The body of my loop:
    Code:
           fprintf(stdout,"%d ", k);
    Output:
    Code:
    1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 7 14 21 28 35 42 49 8 16 24 32 40 48 56 64 9 18 27 36 45 54 63 72 81 10 20 30 40 50 60 70 80 90 100
    * HINT: Don't forget you can do as many assignments as you need in the loop instruction.
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I did not even think of the ternary operator before when I was trying to figure this out. I used ... other methods, to achieve the two-line result.

    Also, my solution incorporated newlines per post #5.

  13. #13
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Effectively you can consider commas and the ternary operator to be cheating as they would both be evaluating multiple instructions in one statement. Though, admittedly, I can't see the math I think I need to eliminate the condition. With the way I'm using the ternary operator putting the newline characters is trivial as the loop maintains both the base term of each line and a sequential figure. Suffice it to say there is more than one way to cook this egg. Mine might just have a bit too much salt.
    Last edited by SlyMaelstrom; 09-03-2013 at 04:48 PM.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I was about to figure out what Salem did, but then I stopped caring and tried to do it even shorter in python.
    Code:
    for k in [[j*i for i in range(1, j+1)] for j in range(1, 11)]: print(k)

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like there has still been no clarification. In the absence of clarification, I'd ask: why on earth would anyone need a loop to print a single string once? Unless you want to format the string over multiple lines, a correct solution here is a one-liner, not a two-liner, and a loop is unnecessary complication.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-22-2009, 10:31 PM
  2. Need "if","for loop",&"else" source codes
    By dn_angel_07 in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2009, 10:01 PM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  5. Write and read the book called "registry""
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 03-10-2002, 10:45 AM