0

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...

7
Contributors
7
Replies
9
Views
8 Years
Discussion Span
Last Post by WaltP
Featured Replies
  • 1
    WaltP 2,905   5 Years Ago

    4 resurrections... Great. [QUOTE=mharicz;]looping statements if n=5, output ******** ****** **** *** * plz answer :([/QUOTE] ... and with a [I]gimme the code[/I] question -- how lazy. Closed. Read More

0

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

Edited by mike_2000_17: Fixed formatting

0

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!!!!

Edited by mike_2000_17: Fixed formatting

-1

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

Comments
Don't bump old threads
0

#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");

}

-2

#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 :)

1

4 resurrections... Great.

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

plz answer :(

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

Closed.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.