I need help with a program using a repetition structure which displays a pattern of asterisks.

Here is what the program requires:

Complete the program by entering the instructions to display the following pattern of asterisks (9 asterisks, 8 asterisks, 7 asterisks, 6 asterisks, 5 asterisks, 4 asterisks, 3 asterisks, 2 asterisks, and 1 asterisk).

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

This is the code i have so far...


//Ch8ConEx6.cpp-displays a pattern of asterisks

#include <iostream>

using std::endl;
using std::cout;

int main()
{
for (int x = 1; x < 6; x = x + 1)
{
for (int y = 0; y < 9; y = y + 1)
cout << "*";
//end for
cout << endl;
} //end for

return 0;
}
//end of main function

I don't understand how to display different numbers of asterisks per each line.
This is one of the few times where recursion actually makes perfect sense.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void PrintAstrix(int n) {
    if (n > 0) {
      for (int i = 0; i < n; i++)
           cout << '*';

           cout << endl;
           PrintAstrix(n-1);
     }
}

int main() {

     PrintAstrix(9);
	
     return 0;
}




If you are doing this for school you should take the time to understand what is happening here. As long as the initial number of asterisks is > 0 than I will print out that number and than call the function again to print 1 less until n becomes 0 and then the recursion stops since the if statement is not satisfied.

If that if statement was not there the recursion would never stop. You must always have a base case that happens at some point or the loop will never end.

So if you replaced the 9 with 0 it would print nothing but if you changed it to 50 it would look like this.

**************************************************
*************************************************
************************************************
***********************************************
**********************************************
*********************************************
********************************************
*******************************************
******************************************
*****************************************
****************************************
***************************************
**************************************
*************************************
************************************
***********************************
**********************************
*********************************
********************************
*******************************
******************************
*****************************
****************************
***************************
**************************
*************************
************************
***********************
**********************
*********************
********************
*******************
******************
*****************
****************
***************
**************
*************
************
***********
**********
*********
********
*******
******
*****
****
***
**
*
Last edited on
While the recursive solution works perfectly, it is perhaps more intuitive to look at the non-recursive (iterative) ways.

So we want two nested loops:
- the outer one for keeping track of the number of lines
- the inner one for keeping track of the number of asterisks being printed

and we start with:

1
2
3
4
5
6
7
for (int i = 0; i < VALUE_A; i++)
{
  for (int j = 0; j < VALUE_B; j++)
    cout << '*';
  
  cout << endl;
}


Our aim here is to figure out what VALUE_A and VALUE_B are.

For VALUE_A, it is the number of lines going to appear; it would be 9.
For VALUE_B, it is the number of asterisks going to appear in line i, so it depends on i!
To construct the formula for VALUE_B, think about the math:
- in the i=0 line, we need 9 asterisks
- in the i=1 line, we need 8 asterisks
- in the i=8 line, we need 1 asterisks

- in line i, we need 9 - i asterisk(s)
So, VALUE_B is 9 - i:

1
2
3
4
5
6
7
for (int i = 0; i < 9; i++)
{
  for (int j = 0; j < 9 - i; j++)
    cout << '*';
  
  cout << endl;
}


Of course, if you need to do the same thing for an arbitrary number of lines, replace the 9 by a variable.

p.s.
It is fine that loops serving simple counting purposes in such situations start the counting from 1 instead of 0, for example,
for (int i = 1; i <=9; i++)
though be prepared to count from 0 in other circumstances like using arrays or vectors. It may be confusing at first that by the first line we mean the i = 0 line, and be careful what you mean by saying the i-th line. It sounds unnatural, but when in Rome, do as the Romans do.
Another simple explanation that may help is to read for loops more like you would speak to someone.

1
2
3
4
5
6
7
8
9
10
// for each row
for (int x = 1; x < 6; x = x + 1)
{
    // do this
    for (int y = 0; y < 9; y = y + 1)
    {
        cout << "*";
    }
    cout << endl;
}


Topic archived. No new replies allowed.