incremental pattern with nested loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ampapp
    New Member
    • Oct 2007
    • 1

    #1

    incremental pattern with nested loops

    Hello. I am in a beginner C++ programming class. This is my first class I have ever done any programming in my whole life. I'll also mention that though it's a college freshman class, there are many parts to the programs that we have to write that I just do not understand. The current project that I am working on involves printing 4 different patterns to the screen(like the ones I have provided below)
    1st
    *
    **
    ***
    ****
    *****

    2nd
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *

    3rd: like 1 but w/ setw(9)
    4th: like 2 but w/ setw(9)

    I can get the 2nd one to work with the following program:
    [CODE=cpp]
    int(main)
    {
    int i, j, k;

    for(i=1; i<=20; i++)

    {

    cout<<"\n";

    for(j=i; j<=20; j++)
    {
    cout<< '*';
    }
    }
    cout << endl;[/code]

    but I am not sure how to go about getting the others to work, for what I have tried has given me infinite asterisks. Below is what I have tried.
    [CODE=cpp]
    for(k=i; k<=20; k--)
    {
    cout << " ";
    cout << setw(9) << fixed << '*';
    }[/code]

    Any help, hints, or suggestions would be GREATLY appreciated.

    Thanks,
    Amber
    Last edited by Ganon11; Oct 6 '07, 06:32 AM. Reason: Fixing [CODE] tags.
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Originally posted by ampapp
    for(k=i; k<=20; k--)
    Hi, Amber.
    Your problem is in the above line of code.
    Each time through the loop, you're subtracting 1 from k.
    Your loop won't terminate as long as k <= 20.
    k will never be greater than 20 if you subtract from it each time through.
    Fix either your loop termination condition or your loop update.
    Hope this helps.

    Comment

    Working...