Help with C++ for Loops & Patterns

  • Context: C/C++ 
  • Thread starter Thread starter ranger
  • Start date Start date
  • Tags Tags
    C++ For loops Loops
Click For Summary

Discussion Overview

The discussion revolves around creating C++ for loops to print specific patterns of asterisks. Participants are sharing code snippets and seeking assistance for generating a third pattern that includes leading spaces before the asterisks.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant shares code for two patterns of asterisks, detailing the structure of the for loops used to generate them.
  • Another participant suggests using [ code ] tags to maintain formatting in the shared code snippets.
  • A different participant proposes using an 'if' statement within the innermost loop to print spaces before the asterisks for the third pattern.
  • A subsequent reply provides a code snippet that attempts to implement the suggested approach for the third pattern, including loops for both spaces and asterisks.

Areas of Agreement / Disagreement

There is no explicit consensus on the best approach for the third pattern, as participants are offering different suggestions and code implementations.

Contextual Notes

The discussion includes varying levels of detail in code formatting and implementation, with some participants noting the importance of preserving formatting in code snippets.

ranger
Gold Member
Messages
1,687
Reaction score
2
I need some help. So far I have some for loops that print the following patters:

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

and **********
*********
********
*******
******
*****
****
***
**
*
Here is the code:
Code:
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.
 

Attachments

Last edited:
Technology news on Phys.org
Use [ code ] ... [ /code ] tags to preserve the formatting of your text.

- Warren
 
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.
 
Code:
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;
}
 

Similar threads

Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
8K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K