I've been working on this code for the past couple of days, and probably just need a fresh pair of eyes to look at my code. I almost have the triangle with spaces solved, but it looks like this:
-----*
-----**
-----***
-----****
-----*****
instead of what i want it to look like, which is this:
-----*
----**
---***
--****
-*****
******
Also, I have no clue where to even start with making a diamond shape.. any help, or start up of the code would be helpful.
Here's the code i have so far:

#include <iostream>
#include <math.h>
#include <string.h>
using namespace std;
int main()
{
{
// Make triangle shaped like so(- stands for blank space)
	// ----*
	// ---**
	// --***
	// -****
	// *****
	int rowSize3;
	cout << "Please enter another row size for a different triangular shape: ";
		cin >> rowSize3;
		for (int r=0;r<=rowSize3;r++) // Number of lines to display
		{
			for (int c=0;c<rowSize3;c++)
			{ 
				cout << " ";
			}
			for (int c=0;c<r;++c)
			{
				cout << "*";
			}
			cout << endl;
		}
}
{
// Make a Diamond shape like so
//    *
//   ***
//  *****
// *******
//  *****
//   ***
//    *
	int rowSize4;
	cout << "Please enter another row size for a diamond: ";
	cin >> rowSize4;
	for (int r=0;r<rowSize4;r++)
	{
		for (int c=0;c<r+1;c++)
		{
			cout << "*";
		}
		cout << endl;
	}
}


	
system("pause");
return 0;
}

Recommended Answers

All 2 Replies

Break the diamond into two and draw them seperately.

--*
-***
*****

This is the 'upper triangle' of the diamond.
In this the number of '*'s in line l are:

2l + 1
where l starts from 0 onwards.(i.e. first line is line 0).

and the number of spaces before a star in a line l are:

n - (l + 1)
where n is the total number of lines in the upper triangle. Here again l starts from 0.


These things will help you in drawing the upper triangle. Note that the lower triangle is just the reversed form of the upper triangle and will(in this case) look like:

-***
--*

and can be similarly drawn.


And in the pattern:

---*
--**
-***
****

if line numbers are considered from 1 the number of spaces before the star in a line are:

n - l
where n is the total number of lines.
Make sure that you write the loop to print spaces before the loop to print stars.

:icon_smile:

I'll help you with the tri-angle... but you need to apply the logic and put them together to build the diamond...

{

		// Make triangle shaped like so(- stands for blank space)
		// ----*
		// ---**
		// --***
		// -****
		// *****
		int rowSize3;
		
		cout << "Please enter another row size for a different triangular shape: ";
		
		cin >> rowSize3;

		for (int r = 1; r <= rowSize3; r++){ // Number of lines to display

			for (int c = 0; c < (rowSize3 - r); c++){

				cout << " ";
			}
			for (int c = 0; c < r; ++c){

				cout << "*";
			}
			cout << endl;
		}
	}
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.