Like Tree4Likes

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

This is a discussion on CAN ANYONE HELP ME to write a c program using only "one for loop" and without using a within the General Discussions forums, part of the Community Boards category; 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,610
    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.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  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
    ...
    Yarin likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    2,370
    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?
    Code:
    namespace life
    {
        const bool change = true;
    }

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,069
    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
    2,292
    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,069
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    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
    21,660
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
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, 07: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, 06:59 AM
  5. Write and read the book called "registry""
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 03-10-2002, 09:45 AM

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