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.
#include <iostream>
usingnamespace 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>
usingnamespace 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;}
}
}
#include <iostream>
usingnamespace 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.
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?
#include <iostream>
usingnamespace 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;
}
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
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.