rhombus made of asterixes

Hello. Here I'm trying to write an algorithm that displays a rhombus with n (cin>> by user) lines. I used the algorithm from the topic I previously posted about making a triangle out of numbers.
I managed to create the right half of the figure, now I have to figure out how to do the other half. I'll update the topic with the progress I make.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;
main ()
{ int n, i, j;
cin>>n;

for(i=1; i<=n; i++)
{for(j=1;j<=i;j++)       //this section displays the upper-right quarter of the rhombus
{cout<<"*";}
cout<<endl;}


for(i=n-1;i>=1;i--)
{for(j=i;j>=1;j--)       //this section displays the lower-right quarter of the rhombus
{cout<<"*";}
cout<<endl;

}
}
Okay, that variant wasn't good, because the two halves (left and right)must be at the same level. The next code makes the top half of the rhombus but it needs to be arranged so that the top of the triangle is above the middle of the bottom segment.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
main ()
{ int n, i, j;
cin>>n;

for(i=1; i<=n; i++)
{for(j=1;j<=2*i+1;j++)       //this section displays the upper-right quarter of the rhombus
{cout<<"*";}
cout<<endl;}

}
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
main ()
{ int n, i, j;
cin>>n; cout<<endl;

for(i=1;i<=n;i++)
{for(j=1;j<=2*i-1;j++)       //this section displays the upper-half of the rhombus
{cout<<"*";}
cout<<endl;
}

for(i=n-1;i>=1;i--)
{for(j=2*i-1;j>=1;j--)       //this section displays the lower-half of the rhombus
{cout<<"*";}
cout<<endl;
}
}


This program returns all the required elements for the rhombus, it's just that they are not arranged to look like a rhombus (each line need to be centered).
To give an example: n=3

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

So what I have to do now is to move each line, except the central line, like this: for n=3
the first line must be moved 2 spaces, the second one 1 space, middle 0, third 1 and last 2.
Last edited on
Here's my first question. At each i line, i have to make n-i spaces appear. So I wanted to do something like if(i) cout<<" "*(n-i); I know the code is wrong, and there's no such thing but is there something alike, that could do the same thing?
Last edited on
found the answer =D
ive amused myself with this heres the code
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
#include <iostream>
using namespace std;
int main ()
{ int n, i, k, j; char space=0x20; //the char space is equal to " "

while (n>0) {
cin>>n; cout<<endl;

for(i=1;i<=n;i++)
{
	for (k=n-i;k>=0;k--)
	{ cout << space;}
for(j=1;j<=2*i-1;j++)       //this section displays the lower-right quarter of the rhombus
{cout<<"*";}
cout<<endl;
}

for(i=n-1;i>=1;i--)
{
	for (k=0;k<(n-i)+1;k++)
	{ cout << space;}
for(j=2*i-1;j>=1;j--)       //this section displays the lower-right quarter of the rhombus
{cout<<"*";}
cout<<endl;
}} 
system("pause");
return 0;
}
Last edited on
just one more thing. n cant be higher then 39 or the diamond thing is fucked up.
so add something like:if (n>39) cout << "your number must be lower then 40" << endl;

by the way i have added a loop so the program doesnt stop unless u click the red button
Haha! very well done :D Thank you again!
I have a single question: from char space=0x20; what is the meaning of 0x20?

And also system("pause") doesn't compile, it's seen like some kind of variable (the error says it was not declared in this scope).
Last edited on
0x20 is the ascii code for a space.

Of course it's simpler if you just use the space literal instead of expecting people to know that:

 
char space = ' ';


Or get rid of space completely and just use ' ' instead.
Ok, guys, another problem solved. Thanks to all responders. I won't add that if() condition, because the only reason the rhombus is messed up is because that window isn't wide enough to display it normally, so I'll just add a message before entering n.
Topic archived. No new replies allowed.