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.
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.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
int num, middle, spaceCount, starCount;
printf("Enter a num:");
scanf("%d",&num);
middle = (num-1)/2;
for (int i = 0; i < num; i++){
spaceCount = abs(middle - i);
starCount = num - 2*abs(middle - i);
for(int c = 0; c < spaceCount; c++)
printf(" ");
for(int c = 0; c < starCount; c++)
printf("*");
for(int c = 0; c < spaceCount; c++)
printf(" ");
printf("\n");
}
return 0;
}
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.
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");
}
I don't want to give you the code, you should learn.
The input number will be the width of the diamond, so make sure to make a relation between the spaces and asterisks that does not surpass this number.
The main idea would be to start at 1 asterisk, and increment by two each time until the input number is reached.
Also, do something to prevent an error if the user enters an even number.
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
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;
}
static void Main(string[] args)
{ //check this out more optimized code hope it will be help full.
for (int row=-2;row<=2;row++)
{
for (int col=-2;col<=2;col++)
{
if (Math.Abs(row)+Math.Abs(col)<=2)
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
Console.WriteLine();
}
Console.ReadKey();
}