pattern

This is a discussion on pattern within the C Programming forums, part of the General Programming Boards category; How i can generate the followning pattern by using for loop and one printf! please help! im stuck!! the pattern ...

  1. #1
    Unregistered
    Guest

    pattern

    How i can generate the followning pattern by using for loop and one printf! please help! im stuck!!

    the pattern

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Hint:

    You need two for-loops to do this.

    Code:
    for (line = 0; line < nr_of_lines; line++)
        for (nr_of_stars = 0; nr_of_stars < line; nr_of_stars++)
            /* end of hint */

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I believe this has the output that you want.
    Code:
    x=6,_x=7,x_=7;main_(x,_x)
    {return(x>0?x_--,main_(x-
    printf("%c",_x),_x):x_);}
    main(){x>0?main_(main_(x,
    32),42),x_+=_x+=1,printf
    ("%c",'\n'),--x,main():0;}
    Enjoy

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274

    Smile be nice and dont tease

    here is the code u want, dont listen to prelude!

    Code:
    int main ()
    {
       int line, star;
       for(line=1; line<=10; line++)
       {
          for(star=1; star<=line; star++)
          {
             printf("*");
          }
          printf("\n");
       }
       system("PAUSE");
       return 0;
    }

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >here is the code u want
    I can guarantee that the formatting didn't allow spaces in the OP's post. The triangle is supposed to look like this:
    Code:
          *
         ***
        *****
       *******
      *********
     ***********
    So it's not the code he/she wants.

    >dont listen to prelude!
    Do listen to Prelude if you want to actually learn something instead of simply copy/pasting code to turn in for homework. I can guarantee that if you decipher the program I posted you'll learn quite a bit more about C than if you simply solve the problem at hand and forget about it. And please don't try to solve homework problems, you'll only encourage dependency on others more.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    i thought he was making a game, cause i made a game like that once, oops, didn't think it would be homework.
    anyways what u wrote is impossible to read for someone who just started programming.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >what u wrote is impossible to read for someone who just started programming
    So think about how much they could learn by taking the time to figure it out.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest
    Thank u all for helping me!
    Dont worry Prelude! When i posted my question, i was expecting only a hint to lead me to the solution, i didnt want anyone to do my homework to me!

    Now, i will try to write the program by myself and to read your code!! ( i guess it will take me a million years to write a code like that!!)

    ps: what does system("pause") do?!

  9. #9
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    prints:
    "please enter a key to continue"
    you press a key it continues with program,
    its called system because its a system command (i think)

  10. #10
    Unregistered
    Guest
    thanx for the new info!!

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    Would you mind explaining your code to me, Prelude?
    Code:
    x=6,_x=7,x_=7;main_(x,_x)
    {return(x>0?x_--,main_(x-
    printf("%c",_x),_x):x_);}
    main(){x>0?main_(main_(x,
    32),42),x_+=_x+=1,printf
    ("%c",'\n'),--x,main():0;}
    I think I got it, except I'm having trouble understanding the ternary operators. For example, I don't get how the fricking ',' in line 2 works? If x>0, is the return value supposed to be x_-- or main_(x-printf("%c",_x)? Arggggh! Not to mention that horrible construct of Satan in the second to last line!

  12. #12
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I believe they have a name for this type of code...
    unmaintainable. Quite difficult to look at and understand, what I do is spread it out, read it then compact it again.
    example:
    Code:
    x=6,_x=7,x_=7;main_(x,_x)
    {return(x>0?x_--,main_(x-
    printf("%c",_x),_x):x_);}
    main(){x>0?main_(main_(x,
    32),42),x_+=_x+=1,printf
    ("%c",'\n'),--x,main():0;}
    Code:
    //spread out as much as possible
    
    int x=6,_x=7,x_=7;
    main_(x,_x)
    {
    return(x>0?x_--,main_(x- printf("%c",_x),_x):x_);
    }
    main()
    {
    x>0?main_(main_(x,32),42),x_+=_x+=1,printf("%c",'\n'),--x,main():0;}
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  13. #13
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    It's generally called obfuscated code, there are competitions with this kind of stuff too.
    I don't imagine prelude writes her everday code like this( at least I hope not!!! ).
    I suspect she was just showing off
    Last edited by C_Coder; 05-04-2002 at 03:10 PM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  14. #14
    Unregistered
    Guest
    Personally I love doing other peoples homework and I don't care whether or not they learn anything.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I think I got it, except I'm having trouble understanding the ternary operators. For example, I don't get how the fricking ',' in line 2 works?
    The ternary operator works like an if..else statement with some restrictions. For example, you can't use continue, break, or return from a ternary expression and under normal circumstances only one operation can be performed in each segment:

    condition ? one operation : one operation;

    This is where the comma operator comes in. By using the comma operator you can place a sequence point in the middle of an expression where otherwise the only way to place a sequence point would be an end of statement semicolon. So with the comma operator you can perform any number of operations in the ternary expression. It's a neat trick that you should avoid except when writing obfuscated code:

    condition ? operation, operation, operation : operation, operation;

    If x>0, is the return value supposed to be x_-- or main_(x-printf("%c",_x)? Arggggh!
    This is a tricky move. If the condition returns true then the function will recurse and perform the proper decrements. Notice that the function does not return if the condition is met. If the condition returns false then a value is truly returned. Think of it like this:
    Code:
    if ( x > 0 ) {
      x_--;
      main_ ( x - printf ( "%c", _x ), _x )
    }
    else
      return x_;
    Not to mention that horrible construct of Satan in the second to last line!
    I assume you mean this?

    x>0?main_(main_(x,32),42),x_+=_x+=1,printf("%c",'\ n'),--x,main():0;

    That part I enjoyed making. I went out of my way to get main_ to return the correct value so that it could be used as an argument to itself.

    >Quite difficult to look at and understand, what I do is spread it out, read it then compact it again.
    It also helps to make unique names so that it's easier to follow what variable has what value. The choice of main_ for the other function was no accident.

    >I don't imagine prelude writes her everday code like this( at least I hope not!!! ).
    No, I like my job.

    >I suspect she was just showing off
    Sort of. This is the code I use to answer the triangle homework question on IRC.

    -Prelude
    My best code is written with the delete key.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visitor / Decorator Pattern
    By MarkZWEERS in forum C++ Programming
    Replies: 9
    Last Post: 05-16-2009, 11:53 AM
  2. Hmm.. Ai? Finding the pattern in number squences?
    By Zeusbwr in forum C++ Programming
    Replies: 8
    Last Post: 04-02-2005, 05:13 PM
  3. Abstract Factory pattern
    By Just in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2005, 09:58 AM
  4. (pattern *) pat & pattern * pat
    By siubo in forum C Programming
    Replies: 1
    Last Post: 04-08-2003, 10:03 PM
  5. text pattern recognition
    By mtsmox in forum C++ Programming
    Replies: 5
    Last Post: 02-27-2002, 07:38 AM

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