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.
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");
}
}
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");
}
}
I struggled with this a bit. Also sorry, my final code is C++, not strict C, as I haven't used C in 10 years. Here's a more wordy explanation for those trying to understand how to work this out for yourself. Before I begin, one thing you can do to help yourself out is start with a declaration like:
int numStars = 1, numSpaces = 10;
numSpaces doesn't really matter as long as it's small enough to fit on your screen... you can plug in any number and test it later. Then, work out your algorithm, and later on you can work on handling input.
Also, the number of spaces AFTER the stars is irrelevant for this problem.
Then, solve the problem of how to output the top portion of the diamond. I used a counter loop to print spaces until I'd printed the number of spaces for the first row, then a second loop (not nested) to print the number of stars needed for that row. We know that in row 1, we need to print a space 10 times and a star one time.
Once the row has the number of spaces and stars required, you go to the newline ("\n" or endl). But the next row you need to print 2 more stars and 1 less spaces. So you must increment/decrement appropriately.
When you get to the row before the middle row, you'll stop. In other words, the top loop needs to continue while numSpaces is greater than 0. At the end of thefinal pass in the loop, numSpaces is decremented to 0, but it won't print the line with 0 spaces and n stars (2 stars for every space you started with). That happens in your next loop...
Then, you can run the second loop that reverses your increment/decrement at the end, but is otherwise almost the same. Only this time, it should print 0 spaces and 10n stars. Then the newline, and finally decrease the numStars by 2 and increase numSpaces by 1.
If you're not using functions yet or not allowed to in implementation, that's just about it. You could easily call a function, though, passing it numSpaces/numStars, for each of the loops to eliminate duplicate code.
Here's my code in C++:
#include <iostream>
using namespace std;
int main()
{
int numStars = 1,
numSpaces = 10;
while (numSpaces > 0)
{
for (int i = 0; i < numSpaces; i++)
{
cout << " ";
}
for (int i = 0; i < numStars; i++)
{
cout << "*";
}
cout << "\n";
numStars += 2;
numSpaces--;
}
while (numStars > 0)
{
for (int i = 0; i < numSpaces; i++)
{
cout << " ";
}
for (int i = 0; i < numStars; i++)
{
cout << "*";
}
cout << "\n";
numStars -= 2;
numSpaces++;
}
return 0;
}
Hi i got to best solution for this problem in less no of lines
int b=0;
for(int a=0;b<=50;a=(b<=25) ? a+2 : a-2 ){
b+=2;
for(int b=25-a;b>0;b-=2){
printf(" ");
}
for(int c=0;c<=a;c++){
printf("*");
}
printf("\n");
}
here the code with matlab
clear all, clc
n=input('Input an Odd Number ');
for i=1:(n+1)/2
for m=1:(n+1)/2-i
fprintf(' ');
end
for j=1:(2*i)-1
fprintf('*');
end
fprintf('\n');
end
for k=i-1:-1:1
for m=1:(n+1)/2-k
fprintf(' ');
end
for j=1:(2*k)-1
fprintf('*');
end
fprintf('\n');
end