Help with C++ for Loops & Patterns

In summary, the conversation involved the request for help with creating a third pattern using for loops. The first two patterns were provided and the code used to print them was shown. The third pattern involved printing asterisks in a specific pattern, which was not properly displayed in the conversation. The expert suggested using an 'if' statement within the loop to print the required number of spaces before the asterisks. The code for the third pattern was also provided.
  • #1
ranger
Gold Member
1,687
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

  • third pattern.doc
    19.5 KB · Views: 243
Last edited:
Technology news on Phys.org
  • #2
Use [ code ] ... [ /code ] tags to preserve the formatting of your text.

- Warren
 
  • #3
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.
 
  • #4
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;
}
 

1. What are for loops in C++?

For loops are programming constructs used to repeat a block of code for a specific number of times. They are commonly used for tasks that require iteration or repetition, such as printing patterns or performing calculations.

2. How do I write a for loop in C++?

A for loop in C++ has three parts: initialization, condition, and update. The syntax is as follows:
for (initialization; condition; update) {
// code to be repeated
}

3. How can I print patterns using for loops in C++?

To print patterns using for loops, you can use nested for loops and manipulate the print statements to achieve the desired pattern. For example, to print a right triangle, you can use a for loop for the rows and a nested for loop for the columns, printing a star for each iteration.

4. Can I use other control structures inside a for loop?

Yes, you can use other control structures such as if-else statements and switch statements inside a for loop. This allows for more complex and dynamic iteration based on different conditions.

5. Are there any common mistakes to avoid when using for loops in C++?

Some common mistakes when using for loops include forgetting to initialize or update the loop control variable, using the wrong comparison operator in the condition, and creating an infinite loop. It is important to carefully review the code and test it to avoid these errors.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
23
Views
2K
Replies
10
Views
958
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top