hw help

Im lost on this home work i have the first part of it done but i have no clue of how to do the second part of it here is the problem would be awesome if you could help!
"In this exercise, you create a program that uses two nested loops to display two different patterns of
asterisks: Pattern 1 contains nine rows of asterisks as follows: nine asterisks, eight asterisks, seven
asterisks, six asterisks, five asterisks, four asterisks, three asterisks, two asterisks, and one asterisk.
Pattern 2 contains nine rows of asterisks as follows: one asterisk, two asterisks, three
asterisks, four asterisks, five asterisks, six asterisks, seven asterisks, eight asterisks, and nine asterisks.
Use whatever type of loops you feel would be most appropriate."

[code]
#include <iostream>
using namespace std;

int main()
{
int rows;

cout << "Enter number of rows: ";
cin >> rows;

for (int i = 0;i <= 9;i++)
{

for (int j = 0;j <= i;j++)
{
cout << "*";
}
cout << endl;
}


system("pause");
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int main()
{
    // int rows;
    // cout << "Enter number of rows: ";
    // cin >> rows;

    for (int i = 0; i < 9; ++i ) // i<9 print 9 rows 0, 1, 2, ... 7, 8
    {
        for (int j = 0; j <= i; ++j ) // in each row i, print i+1 '*'
        {
            cout << '*' ;
        }
        cout << '\n' ;
    }


    for (int i = 0; i < 9; ++i) // i<9 print 9 rows 0, 1, 2, ... 7, 8
    {
        for (int j = 9; j > i ; --j ) // in each row i, print 9-i '*'
        {
            cout << '*' ;
        }
        cout << '\n' ;
    }
}


Or, for number of rows from user input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

int main()
{
    int rows;
    std::cout << "Enter number of rows: ";
    std::cin >> rows;

    for (int i = 0; i < rows; ++i ) // i<9 print 9 rows 0, 1, 2, ... rows
    {
        for (int j = 0; j <= i; ++j ) // in each row i, print i+1 '*'
        {
            std::cout << '*' ;
        }
        std::cout << '\n' ;
    }

    for (int i = 0; i < rows; ++i) // i<9 print 9 rows 0, 1, 2, ... rows
    {
        for (int j = rows; j > i ; --j ) // in each row i, print rows-i '*'
        {
            std::cout << '*' ;
        }
        std::cout << '\n' ;
    }
}
Last edited on
You've done (mostly) the second part:
Pattern 2 contains nine rows of asterisks as follows: one asterisk, two asterisks, three asterisks, four asterisks, five asterisks, six asterisks, seven asterisks, eight asterisks, and nine asterisks.
although your pattern consists of ten rows going from 1 to ten asterisks, which is one row too many.

The first part, "Pattern 1 contains nine rows of asterisks as follows: nine asterisks, eight asterisks, seven asterisks, six asterisks, five asterisks, four asterisks, three asterisks, two asterisks, and one asterisk. " is almost identical to the second.

Forget about the patterns for a moment. Just think about counting up from 1 to 9. Then think about counting down from 9 to 1.
 
    for (int i = 1; i <=9; i++)

Here, the start value is 1, the end value is 9, and the variable i is incremented each time.

To count down again, you need: start value is 9, the end value is 1, and the variable i is decremented each time. You will also need to change the <= to >=.
This doesn't satisfy the requirement for "two nested loops" but it is concise:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int main()
{
    for (int i = 9; i >= -9; i--)
        if (i) 
            cout << string(abs(i), '*') << '\n';           
}

This doesn't satisfy the requirement for "two nested loops"

However, on each iteration the loop body has access to number abs(i).
It should not be a huge leap to use that value in an inner loop.
This is probably two levels of nesting short to do your homework ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

void line( int n )
{
   if ( n > 0 ) line( n - 1 );
   cout << string( abs( n ), '*' ) << endl;
   if ( n < 0 ) line( n + 1 );
}

int main()
{
   line( -9 );
   line(  9 );
}

Last edited on
Topic archived. No new replies allowed.