Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Write a program that uses for statements to print the following patterns separately, one below the other. Use for loops to generate the patterns. All asterisks (*) should be printed by a single statement of the form cout << '*';.
[Hint: The last two patterns require that each line begin with an appropriate number of blanks.]

What I have tried:

I haven't tried anything since i don't know what do to
Posted
Updated 24-Oct-17 21:12pm
Comments
Rick York 24-Oct-17 13:28pm    
It's rather difficult to know how to begin when we can't see the patterns you are supposed to print.
Richard Deeming 24-Oct-17 15:28pm    
Your homework will be based on the topics you have recently covered in your class.

If you don't know where to start, and you can't work it out from your course notes, then talk to your teacher.

We sometimes help if you've made a start and just need a nudge in the right direction. But we're not going to do your homework for you.
nv3 25-Oct-17 1:59am    
Start by looking at the patterns. What do you see? In the first line there is one group of 10 stars, then a space and another group of 10 stars, then a space and a single star. Now look at the next line. The first two groups have shrunk by one star each, the third group has grown by one star. And in the following line that rule is continued.

So your program should contain a big loop for the line doing 10 rounds. Inside that loop you need three mode loops, the first two of which start out with 10 rounds and the third one starting with one round. Then every iteration of the outer loop the loop counts of the inner loops must be adjusted accordingly.

Got it?

1 solution

The solution of nv3 is correct, you need an outer loop, which controls the inner loops. A bit of code

C++
int first = 10;
for( int line = 0; line < 10; line++ ) {
  //inner loops
  for( int i = 0; i < first; i++ ) {
    cout << '*';
  }
  // loop control at end
  first--;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900