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

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You make a fair point.

    Code:
    print('\n'.join(str(tier) for tier in [' '.join(str(i*j) for i in range(1, j+1)) for j in range(1, 11)]))
    Last edited by whiteflags; 09-04-2013 at 10:00 PM. Reason: typo

  2. #17
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by laserlight View Post
    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.
    I see your point, but then using one for loop was about the only thing that was clarified, so I think most of us assumed it was mandatory. My alternative suggestion, before everyone started talking about their solutions for printing that string was to post something like this:

    Code:
    #include <stdio.h>
    
    int main() {
       int i;
       
       for (i=0; !i; i++)
    
       return 0;
    }
    This might, in fact, be a more correct solution to the OP's question if they meant quite literally:
    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
    Any use of one of those numbers might be considered inaccurate. In fact, I chose to be even more pedantic by using !i as every positive digit from 1-9 is present by itself in the OP's sequence which one could argue disallows any number that contains them. Though, of course, there are other ways of do a for loop without using any of those numbers, as well. I wasn't actually sure if a blank for(;;) was legal by the C standard or if it was just the compiler I was using, but I probably would have preferred that if it was.
    Last edited by SlyMaelstrom; 09-04-2013 at 10:03 PM.
    Sent from my iPadŽ

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, in that case, I would propose this two line C99 program:
    Code:
    int main(void) {
        for (; 0;); }
    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

  4. #19
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yeah that's about as compact as it gets. I should really feel silly for leaving in my compiler's templated #include <stdio.h> in a program that wasn't calling any library functions.

    Quote Originally Posted by whiteflags View Post
    Code:
    print('\n'.join(str(tier) for tier in [' '.join(str(i*j) for i in range(1, j+1)) for j in range(1, 11)]))
    I accept that you abandoned the "using C" requirement, but I can't forgive you for technically using more than one for.
    Last edited by SlyMaelstrom; 09-04-2013 at 10:13 PM.
    Sent from my iPadŽ

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Eh. I guess it could be fixed. I suspect people would get bored of me though.

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