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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        ***** 
       ***** 
      ***** 
     ***** 
    ***** 

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

Post your code(good/bad) please to see and correct it.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <iomanip>

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:
1
2
3
4
5
6
7
8
9
10
11
Enter a number: 10
          
         *
        ***
       *****
      *******
     *********
      *******
       *****
        ***
         *
Thanks condor. I wasn't able to get the parallelogram right. Here's my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
for(int i=num; i>=0; i--)
		{
			for(int j=0; j<num; j++)
				{
					if(j<i)
					{
						cout << " ";
					}
					else
					{
						cout << "*";
					}
				}
			cout<<endl;
		}
	for(int i=0; i<num; i++)
		{
			for(int j=i; j<num; j++)
				{
					cout << "*";
				}
			cout << endl;
		}
Here's the complete code (slight modified):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <iomanip>

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):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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.