find out logic for the program

This is a discussion on find out logic for the program within the C Programming forums, part of the General Programming Boards category; required output: Code: 1 2 2 3 3 3 can any one please tell me the logic for the required ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    13

    find out logic for the program

    required output:
    Code:
      1 
     2 2
    3 3 3
    can any one please tell me the logic for the required output ?????
    Last edited by cooldude; 09-08-2009 at 11:04 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    This is the first and probably last time I'm going to do someone's homework:
    Code:
    #include <stdio.h>
    void /* To fail even harder */ main()
    {
      printf("  1\n 2 2\n3 3 3\n");
    }

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cooldude View Post
    can any one please tell me the logic for the required output ?????
    Yes, you must print three lines like this:

    Code:
      1 
     2 2
    3 3 3
    @EVOEx: the return type of main() should be int, not void
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You must know, since you're able to do it, right? How'd you make that triangle of digits for your post?

    Maybe you took the first number (1) and divided the width of the base of the triangle (which is not even drawn yet), by two.

    Idea's in pseudo-code.

    Code:
    begin outer loop: (maybe for(i = 0; i < 4; i++) {
    
      begin loop for each row: 
    
        spacesBetweenDigits = SizeofBaseOfTriangle / (numberToPrint+1)
    
        print spacesBetweenDigits number of spaces. 
    
        then print numberToPrint and increment numberYouPrinted by one.  
    
      loop until numberYouPrinted == numberToPrint
    
      print newline
    
    } loop until all the rows have been printed
    Once you get down the steps you can use, in simple terms, then it's easy to start changing it into code, although you'll undoubtedly change it as you go. Thing is, you'll have a starting point, for your program.
    Last edited by Adak; 09-08-2009 at 11:23 AM.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    13
    k thanks a lot guys.........

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by MK27 View Post
    @EVOEx: the return type of main() should be int, not void
    That was a joke... right?

  7. #7
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Actually, it is a good practice to make the return type of main() int:

    Cprogramming.com FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by MTK View Post
    Yes, but that's why I put the comment "To fail even harder" in there. Trust me, if I do someone's homework, I make sure they'll fail as badly as they can.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Okay, I really did it for you this time. 109 chars. Anybody think they can do better? I dare you all ;-).

    Code:
    c;main(a,b){a-1||(b=getchar()-48);printf("%*d",b--,c=a);while(--c)printf(" %d",a);puts("\n");b&&main(a+1,b);}
    (note: input through stdin. DONT try to pass it as a parameter ;-)

    Edit: 103 (I'm bored)

    Code:
    c;main(a,b){b=a-1?b:getchar()-48;for(printf("%*d",b--,c=a);c--;printf(c?" %d":"\n",a));b&&main(a+1,b);}
    And last for 101
    Code:
    c;main(a,b){for(printf("%*d",b=a-1?b:getchar()-48,c=a);c--;printf(c?" %d":"\n",a));--b&&main(a+1,b);}
    I just HAD to break the 100-score. So for 99:
    Code:
    b,c;main(a){for(printf("%*d",b=a-1?b:getchar()-48,c=a);c--;printf(c?" %d":"\n",a));--b&&main(a+1);}
    Come on, nobody else daring a game of programming golf?
    Last edited by EVOEx; 09-08-2009 at 03:15 PM.

  10. #10
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Quote Originally Posted by EVOEx View Post
    Yes, but that's why I put the comment "To fail even harder" in there. Trust me, if I do someone's homework, I make sure they'll fail as badly as they can.
    Oh, I didn't get that at first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  2. Please find an error in my noob program
    By alexpos in forum C Programming
    Replies: 2
    Last Post: 10-23-2005, 02:55 PM
  3. Program does not find any matching file
    By tilex in forum Windows Programming
    Replies: 1
    Last Post: 10-01-2005, 09:59 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 4
    Last Post: 08-15-2002, 11:35 AM

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