1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ and for loops

  1. Mar 21, 2006 #1

    ranger

    User Avatar
    Gold Member

    I need some help. So far I have some for loops that print the following patters:

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

    and


    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *
    Here is the code:
    Code (Text):
    for(int rows=1;rows<=10;rows++)
              {
                for(int asteriskcount=0;asteriskcount<rows;asteriskcount++)
                         {
                          cout<<"*";
                         }
                         cout<<endl;
              }

              cout<<endl<<endl;

    for(int rows_2=1;rows_2<=10;rows_2++)
              {
                         for(int asteriskcount_2=11;asteriskcount_2>rows_2;asteriskcount_2--)
                         {
                                    cout<<"*";
                         }
                         cout<<endl;
              }

                 cout<<endl<<endl;
    The first pair of for loops prints the first pattern and the second prints the second pattern. I need some help with a third pattern though. It should look smoething like this:

    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *
    EDIT: It seems the third pattern is not posting the way I typed it. So I have uploaded a MS Word file containg it.

    --thank you.
     

    Attached Files:

    Last edited: Mar 21, 2006
  2. jcsd
  3. Mar 21, 2006 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Use [ code ] ... [ /code ] tags to preserve the formatting of your text.

    - Warren
     
  4. Mar 22, 2006 #3

    siddharth

    User Avatar
    Homework Helper
    Gold Member

    For the third pattern, you could use an 'if' statement in your innermost loop to print the the required number of spaces (ie, " ") for each row and then, print * for the rest of the row.
     
  5. Mar 23, 2006 #4
    Code (Text):
    for (int i=0; i<=9; i++)
    {
       for (int j=0; j<i; j++)
           cout << " ";
       for (int j=i; j<=9; j++)
            cout << "*"
       cout << endl;
    }
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: C++ and for loops
  1. C while loop issue (Replies: 26)

  2. How to Loop this C++ (Replies: 2)

  3. For loop in c (Replies: 3)

  4. Loop in c (Replies: 1)

Loading...