I want to be able to print a diamond like this when the user enters 5 for the diamond. But also will work for any value that is odd and greater than 0.
There are two things that occur here:
Indent changes (-1 to "mid", +1 "after")
Star count changes (+2 to "mid", -2 "after")
Now, this could be done with two loops (one for the top to "mid" and one "after"), but the values can also be determined with a little math. Let's explore that :-)
Here are the numbers:
s(spaces) x(stars) n(line number) __X 2 1 0 _XXX 1 3 1 XXXXXX 0 5 2 _XXX 1 3 3 __X 2 1 4
Start by noting that s
is symmetrical about the "mid":
And then that x
is related to s
(by 2 as noted in deltas above):
Hope that gives some insight!
Output a diamond in C by asterisk sign
#include<stdio.h>
main()
{
int n, i, j;
printf("Enter a number: ");
scanf("%d",&n);
/* Create up arrow. */
for(i=1; i<=n; i++)
{ /* Left side. */
for(j=i; j<=n+1; j++)
{
printf(" ");
}
for(j=1; j<=1; j++)
{
printf("*");
}
/* Middle. */
for(j=1; j<i-1; j++)
{
printf(" ");
}
/* Right side. */
for(j=1; j<i; j++)
{
printf(" ");
}
for(j=1; j<=1; j++)
{
if(i!=n-(n-1))
printf("*");
}
printf("\n");
}
/* Create down arrow. */
for(i=1; i<=n; i++)
{ /* Left side. */
for(j=1; j<=i+2; j++)
{
printf(" ");
}
for(j=1; j<=1; j++)
{
if(i!=n)
printf("*");
}
/* Middle. */
for(j=i; j<n-2; j++)
{
printf(" ");
}
/* Right side. */
for(j=i; j<n-1; j++)
{
printf(" ");
}
for(j=1; j<=1; j++)
{
if(i<n-1)
printf("*");
}
printf("\n");
}
}
Almost certainly homework so here's a clue.
Count the number of spaces before a line and the number of stars in that line. What you're looking for is a relationsip between the line number and those two values.
Then you can use two consecutive for
loops, one increasing the star count and the other decreasing it.
Within each of those loops would be two more consecutive loops to print out the required number of spaces followed by the required number of stars followed by a newline character.
If you're still having trouble after reading the above, consider this. For an input of (odd, as you state you enforce in your comments) n
, the space count starts at (n - 1) / 2
and the star count at 1
. For each subsequent line, the space count reduces by 1
and the star count increases by 2
.
That works up until the point where the space count reaches 0
, then you turn around and go the other way, making sure you don't print that middle line twice.
Once the star count reaches 0
, you're done.
Now you just have to turn that specification into code :-)
Now, since you've indicated in comments that you're interested in making your own solution rather than just being handed code, I feel comfortable giving you something you can check your own solution against. Here's the pseudo-code I would use:
# Input data, check and init counters.
input n
make sure n is odd and greater than 2
set numspaces to (n-1) / 2
set numstars to 1
# Gradually get wider until just before middle line.
while numspaces > 0:
for i = 1 to numspaces: output " "
for i = 1 to numstars: output "*"
output newline
subtract 1 from numspaces
add 2 to numstars
# Gradually get thinner until end.
while numstars > 0:
for i = 1 to numspaces: output " "
for i = 1 to numstars: output "*"
output newline
add 1 to numspaces
subtract 2 from numstars
And, as a final exercise, you can refactor:
for i = 1 to numspaces: output " "
for i = 1 to numstars: output "*"
output newline
into a separate function, since it's common between the two loops.
And now, since you've got your own code working, here's the Python code I used for proof of concept, included for completeness:
def lineout (sp, st):
s = ""
for i in range (sp): s = "%s "%(s)
for i in range (st): s = "%s*"%(s)
print s
n = 21
numspaces = (n-1) / 2
numstars = 1
while numspaces > 0:
lineout (numspaces, numstars)
numspaces -= 1
numstars += 2
while numstars > 0:
lineout (numspaces, numstars)
numspaces += 1
numstars -= 2
You could probably write it more succinctly in Python if you used the more modern features but that would rather defeat the purpose of quick understanding and easy translation. Just change n
to whatever number you want (odd and greater than two, providing the resultant diamond will fit in your terminal) and enjoy the output :-)
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
*******************
*****************
***************
*************
***********
*********
*******
*****
***
*
#include<stdio.h>
main()
{
int i,j,k,n;
scanf("%d",&n);
n=(n+1)/2;
for(i=1;i<=n;i++)
{
for(j=n;j>=i;j--)
printf(" ");
for(k=1;k<=(2*i-1);k++)
printf("*");
printf("\n");
}
for(i=1;i<=(n-1);i++)
{
for(j=0;j<=i;j++)
printf(" ");
for(k=(2*n-3);k>=(2*i-1);k--)
printf("*");
printf("\n");
}
}