Hi, im confuse making this program,.. i need a little help from expert here in dream in code so i decided to post it in here,..


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 << '*'; (this causes the asterisks to print side by side). [Hint: The last two patterns require that each line begin with an appropriate number of blanks. Extra credit: Combine your code from the four separate problems into a single program that prints all four patterns side by side by making clever use of nested for loops.]

1.

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

2.

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

3.

**********
..*********
....********
......*******
........******
......... *****
............****
..............***
................**
..................*

4.

...................*
.................**
...............***
.............****
...........*****
.........******
.......*******
.....********
...*********
.**********

I already made 1 and 2 .. my problem is figure number 3 and 4, please help me using c++..(nevermind the dots in 3 and 4)
Here is the code i made so far in 1 and 2, i need 3 and 4 thanks i really appreciate your help...
CODE

#include<iostream>
using namespace std;
int main()
{
    int value = 11;
    for(int i = (value - 1); i > 0; i--)
{
for(int j = 0; j < i; j++)
cout << "*" << flush;

cout << "" << endl;
}
return 0;
}

A little help is a million thanks.....

Recommended Answers

All 6 Replies

You're on the right track:

for(int i = (value - 1); i > 0; i--)
{
for (int j = 0;  j < i;  j++)
cout << "*" << flush;

cout << "" << endl;
}

You have a loop set up to display the asterisks. Have a loop set up right before then to display spaces:

for(int i = (value - 1); i > 0; i--)
{
    for (int j = ?; ?; ?)
        cout << " ";  // display blank space

    for (int j = 0; j < i; j++)
        cout << "*" << flush;

    cout << "" << endl;
}

You just have to write that one line in red and that's it.

You're on the right track:

for(int i = (value - 1); i > 0; i--)
{
for (int j = 0;  j < i;  j++)
cout << "*" << flush;

cout << "" << endl;
}

You have a loop set up to display the asterisks. Have a loop set up right before then to display spaces:

for(int i = (value - 1); i > 0; i--)
{
    for (int j = ?; ?; ?)
        cout << " ";  // display blank space

    for (int j = 0; j < i; j++)
        cout << "*" << flush;

    cout << "" << endl;
}

You just have to write that one line in red and that's it.

Thanks for reply Mr. VernonDozier, but what do i need to put in there??

for (int j = ?; ?; ?)
        cout << " ";  // display blank space

this is a loop to compute spaces you need. Now look at the outer for loop and try to figure out how you can use that to set up the for statement in the inner loop.

In a different example if you need 4 spaces, you may do.

numspaces = 4;
for (int j = 0; j < numspaces; j++)
        cout << " ";  // display blank space

What would a flowchart look for this?

Assuming there are no spaces between the *s on any given line and the font you use has the width of a space equal to the width of a *, then a table of spaces and *s would look like

0 0 10
1 1 9
2 2 8
3 3 7
etc.

where the left hand column is the row number (assuming it is zero based numbering), the second column is the number of spaces in that row, and the thrid column is the number of *s in that row.

I assume that's what you meant by a flow chart. If you want some fancy boxes with lines drawn here and there then you're on your own. I'll let you come up with a verbal description of the relationship between the row number, the number of spaces per row and the number of *s per row.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.