### Hw help! parallelogram and diamond

Hi, I wanted to know where to begin on this. I'm supposed to prompt the user for a number and then it should display a parallelogram and diamond. I've tried using nested for loops but I couldn't figure out the right conditions. Any help would be much appreciated. Thanks.

Here's a sample run.

Enter a number: 5
 ``12345678910111213141516`` `````` ***** ***** ***** ***** ***** * *** ***** ******* ********* ******* ***** *** * ``````

Last edited on
Here's the code for the diamond which is slight more difficult than for the parallelogram. Try to complete that code and post it.
 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include int main() { using std::cin; using std::cout; using std::setw; int n; cout << "Enter a number: "; cin >> n; //...Here was the code for the parallelogram :). //...And here's for the diamond :)). for(int i = 0; i < (n + 1)/2 + 1; ++i) { cout << setw(n - i + 1); for(int j = 0; j < 2*i - 1; j++) cout << '*'; cout << '\n'; } for(int i = (n + 1)/2; i > 0; --i) { cout << setw(n - i + 2); for(int j = 2*i - 3; j > 0; --j) cout << '*'; cout << '\n'; } }``````

The output:
 ``1234567891011`` ``````Enter a number: 10 * *** ***** ******* ********* ******* ***** *** *``````
Thanks condor. I wasn't able to get the parallelogram right. Here's my code.

 ``1234567891011121314151617181920212223`` ``````for(int i=num; i>=0; i--) { for(int j=0; j
Here's the complete code (slight modified):

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include #include using namespace std; int main() { int i, n; char w; cout << "Enter a number: "; // it is nice from 11 to 25. cin >> n; cout << "Used character: "; // any character : a..z, !. -, _, |, \, /, * and so on. cin >> w; int s = (n + 1)/2; //...Here's the code for the parallelogram. for(int i = n; i > 0; --i) { cout << setw(i + 4); for(int j = 0; j < n; ++j) cout << w; cout << '\n'; } //...And here's for the diamond. for(i = 0; i < s + 1; ++i) { cout << setw(s + 5 - i); for(int j = 0; j < 2*i - 1; ++j) cout << w; cout << '\n'; } for(i = s; i > 0; --i) { cout << setw(s + 6 - i); for(int j = 2*i - 3; j > 0; --j) cout << w; cout << '\n'; } // system ("pause"); }``````

Hope this helps.
Output(sample):
 ``12345678910111213141516171819202122232425`` ``````Enter a number: 11 Used character: o ooooooooooo ooooooooooo ooooooooooo ooooooooooo ooooooooooo ooooooooooo ooooooooooo ooooooooooo ooooooooooo ooooooooooo ooooooooooo o ooo ooooo ooooooo ooooooooo ooooooooooo ooooooooo ooooooo ooooo ooo o``````
Last edited on
Thanks, what does it mean when you use setw?
setw(n) sets the field width to be used on output operations. (n = number of characters to be used as field width.) For more info see:

http://www.cplusplus.com/reference/iomanip/setw/?kw=setw

Happy programming! ;)
can it be done without the setwidth?
Topic archived. No new replies allowed.