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.
I have a code that works to make a diamond for user input of 5 but won't work for all odd number inputs..
half = (size/2)+1;
for (a=1; a <= half ; a++) /*top to mid row of diamond*/
{
for (b=a; b<half;b++)
{
printf(" ");
}
for (c= size -2* a; c <= half; c++)
{
printf("*");
}
printf("\n");
}
for (a = 1; a < half;a++)
{
for (b = a; b>0;b--)
{
printf(" ");
}
for (c = size-2*a; c >0 ;c--)
{
printf("*");
}
printf("\n");
}
return 0;
}
Any help would be greatly appreciated.Thank you.
Mike
You are probably taking introductory C class right now, and as everyone else I would discourage you from copying, but for reference here is an answer:
To make the answer simple, all you need to know is how many spaces and stars to print.
The sequence is following: [print spaces] [print stars] [print spaces] [print '\n'].
Observe that in the middle row we would need: (0) spaces, (numStars) stars, (0) spaces.
In the row above and below, we would need: (1) space, (numStars - 2) stars, (1) space.
An so on...
This should give you the feeling that to count number of spaces, you need to count the distance from the middle row. Also note that with each row away from the middle, number of stars decreases by 2.
Since we are calculating the distance between the current row and the middle one, we need to know the absolute value. abs function is standard C function to do that.
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 at1
. For each subsequent line, the space count reduces by1
and the star count increases by2
.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:
And, as a final exercise, you can refactor:
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:
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 :-)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:
Start by noting that
s
is symmetrical about the "mid":And then that
x
is related tos
(by 2 as noted in deltas above):Hope that gives some insight!