Results 1 to 11 of 11

Thread: how to do this ?

  1. #1
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default how to do this ?

    I am trying to write a program that does this:

    *
    **
    ***
    ****
    *****
    ******
    *******
    I have most of the code. I can't figure how to produce the spaces. Can I see some code to produce it ?
    The code I have so far is:
    Qt Code:
    1. for(t=1;t<12;t++)
    2. {
    3. //* LOPE CODE FOR SPACES
    4. for(n=12;n>t;--n)
    5. cout << "*";
    6. cout <<endl;
    7. }
    8.  
    9. cout<<endl;
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to do this ?

    What spaces?

  3. #3
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to do this ?

    please post a more complete description of the problem.

  4. #4
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to do this ?

    It didn't print right upon the first posting.
    See image below:

    How would you produce those spaces ?
    Last edited by Petr_Kropotkin; 5th February 2010 at 03:23.

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to do this ?

    Use a third loop to print the spaces

  6. #6
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to do this ?

    Where
    I tried a third loop it printed it wrong.
    Like this:
    Qt Code:
    1. for(t=1;t<12;t++)
    2. {
    3. for(j=1;j<=t;++j)
    4. cout<< " ";
    5. for(n=12;n>t;--n)
    6. cout << "*";
    7. cout <<endl;
    8. }
    9.  
    10. cout<<endl;
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to do this ?

    Your second loop is 1 to t inclusive. So it would print out 1 space, then 2 spaces, then 3 spaces, etc, so it would be the upside down version of your image. Is that correct?

  8. #8
    Join Date
    Sep 2009
    Location
    Finland
    Posts
    63
    Thanks
    1
    Thanked 22 Times in 19 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: how to do this ?

    This:

    Qt Code:
    1. const int chCnt = 6;
    2. for (int ch = 1; ch <= chCnt; ch++)
    3. qDebug() << QString(" ").repeated(chCnt - ch) + QString("*").repeated(ch);
    To copy to clipboard, switch view to plain text mode 

    gives:

    Qt Code:
    1. " *"
    2. " **"
    3. " ***"
    4. " ****"
    5. " *****"
    6. "******"
    To copy to clipboard, switch view to plain text mode 

    But your post is under General Programming so maybe this solution is not good for you, but I decided to post it any way

  9. The following user says thank you to tsp for this useful post:

    pitonyak (9th February 2010)

  10. #9
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to do this ?

    I like this particular solution because it uses the repeated method... and I was unaware of the repeated method.

  11. #10
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to do this ?

    neat I like it

    I solved it shortly after.

  12. #11
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to do this ?

    @tsp
    really the Qt(cute) way to do it !!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.