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; 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 ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    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,062
    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.
    laserlight and Yarin like this.
    Sent from my iPadŽ

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

  4. #19
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    Eh. I guess it could be fixed. I suspect people would get bored of me though.

Page 2 of 2 FirstFirst 12
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