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

Dani AI

Generated

A concise, robust way to print the left‑aligned "diamond" shown by is to run a single outer loop for all rows (1 through 2*n-1) and compute the star count for each row with a small formula. This avoids printing the peak line twice (the common off-by-one bug) and still uses for loops as requested. ' nested-loop idea is perfectly valid; the single-loop variant below is just a compact alternative.

#include <iostream>

int main() {
    int n;
    if (!(std::cin >> n) || n <= 0) return 0;

    for (int row = 1; row <= 2 * n - 1; ++row) {
        int stars = (row <= n) ? row : 2 * n - row;
        for (int i = 0; i < stars; ++i) std::cout << '*';
        std::cout << '\n';
    }
}

How it works: total rows = 2*n - 1; for the first n rows stars increase (1..n), then decrease (n-1..1). That formula cleanly eliminates the duplicate peak that caused the double "***" in ' sample (common when both ascending and descending loops include the max row). For a centered diamond (as in ' posts) prepend spaces: `std::cout << std::string(n - stars, ' ') << std::string(stars, '') << '\n';`.

Notes and tips: treat nonpositive n as no output; prefer '\n' over std::endl in loops for performance; for very large lines std::string(stars, '*') is simpler and faster than an inner loop; test with small n values (1,2,3) to confirm boundaries. For readers like examining older multi-variable examples, the core ideas are (a) count rows, (b) compute stars per row, and (c) avoid printing the peak twice.

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.