Hi guys i've been stuck by this program for a while...
Although you might find it easy,, but i'm still a beginner on c++ so please please
help me on this program..

Write a program that will display a pattern depending on the value of n entered by the user.
ex. if n = 3, display
*
**
***
**
*
(diamond shaped)

whoever has some time please help... tnx!!!:)

and please do it with a for loop...

Recommended Answers

All 7 Replies

In times like this I'd either write pseudo-code or a process of how I want the code to work.

such as...

//The maximum amount of stars printed is the argument the user/programmer decides
//the stars stack onto each other until they reach max value, then descend to zero

knowing this, you should be able to write code to display the asteriks.

For the ascending cycle...

int VALUE = /*enter value here*/;

for(int i = 0; i < VALUE; i++)
{
   cout << "*" << flush;
}

but this isn't enough, we're simply printing the asterik out VALUE times. We need to make it seem like a stack.

Notice that the diamonds increment in a dimensional manner--

(0)
* (1)
** (now 3)
*** (now 6)
**** (now 10)

for each "row" there are an equal amount of asteriks.

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

     cout << "" << endl;
}

This would solve the ascending problem. Now for descending you'd have to do something similar, but in reverse. Plus you can't use the less-than-VALUE expression because you can't print the max row twice so...

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

    cout << "" << endl;
}

would complete the diamond

In times like this I'd either write pseudo-code or a process of how I want the code to work.

such as...

//The maximum amount of stars printed is the argument the user/programmer decides
//the stars stack onto each other until they reach max value, then descend to zero

knowing this, you should be able to write code to display the asteriks.

For the ascending cycle...

int VALUE = /*enter value here*/;

for(int i = 0; i < VALUE; i++)
{
   cout << "*" << flush;
}

but this isn't enough, we're simply printing the asterik out VALUE times. We need to make it seem like a stack.

Notice that the diamonds increment in a dimensional manner--

(0)
* (1)
** (now 3)
*** (now 6)
**** (now 10)

for each "row" there are an equal amount of asteriks.

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

     cout << "" << endl;
}

This would solve the ascending problem. Now for descending you'd have to do something similar, but in reverse. Plus you can't use the less-than-VALUE expression because you can't print the max row twice so...

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

    cout << "" << endl;
}

would complete the diamond

wow!! a million thanks!!!!

Hi,
I want a pseudocode for this pattern like this:

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

I am unable to do it as 2 times '4 *' is displayed on 2 different rows..

Can you plz help??

Thanks

commented: Don't bump old threads -1

#include <iostream.h>
main()
{
cout << "Enter a digit : ";
int a, b, c, e, i, j, k, m, n;
cin >> n;
for (a = e = m = 1; n >= a; e++, m++, n--) {
for (b = 1; n > b; b++)
cout << " ";
for (c = 1; 2*e - 1 >= c; c++)
cout << "*";
cout << endl;
}

for (i = 2; e > i; i++, m--) {
for (j = 2; i >= j; j++)
cout << " ";
for (k = 1; (2*(m-2) - 1) >= k; k++)
cout << "*";
cout << endl;
}
system("pause");

}

#include <iostream.h>
main()
{
cout << "Enter a digit : ";
int a, b, c, e, i, j, k, m, n;
cin >> n;
for (a = e = m = 1; n >= a; e++, m++, n--) {
for (b = 1; n > b; b++)
cout << " ";
for (c = 1; 2*e - 1 >= c; c++)
cout << "*";
cout << endl;
}

for (i = 2; e > i; i++, m--) {
for (j = 2; i >= j; j++)
cout << " ";
for (k = 1; (2*(m-2) - 1) >= k; k++)
cout << "*";
cout << endl;
}
system("pause");

}

==> CAN you please explain those codes that you use and how did it loop??
please! thx :)

looping statements
if n=5, output
********
******
****
***
*

plz answer :(

4 resurrections... Great.

looping statements
if n=5, output
********
******
****
***
*

plz answer :(

... and with a gimme the code question -- how lazy.

Closed.

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.