C/C++ Help with C++ for Loops & Patterns

AI Thread Summary
The discussion focuses on generating three specific patterns using nested for loops in C++. The first two patterns are successfully printed using the provided code, which consists of two sets of for loops. The first set creates an ascending triangle of asterisks, while the second set generates a descending triangle. The user seeks assistance in creating a third pattern that features right-aligned descending rows of asterisks. A suggested solution involves using an additional loop to print spaces before the asterisks, ensuring proper alignment. The proposed code snippet demonstrates how to implement this by adjusting the number of spaces based on the current row index, followed by printing the remaining asterisks.
ranger
Gold Member
Messages
1,685
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;
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top