First, the program is taking an argument from the command line.
Therefore must be executed from the command line.
1 2
|
if (argc != 2)
exit(1);
|
Else it would exit with value 1.
|
n = atoi(argv[1]);//The value entered during command line is controlling the size of the Christmas tree!
|
Possibly you don't know for-loops, so I am explaining it. If you do, then just look for what you need.
The below is a simple for loop.
First argument assigns i=0. Only during initial use of for-loop
Second argument compares "i" to "n". Compares every complete cycle.
Third argument increments i by 1 every cycle.
1 2 3 4 5 6 7 8
|
for (i = 0; i < n; i++)//
{
for (j = 0; j < n+i+2; j++)//same as above, this time, j compares to i plus n plus 2
{
putchar(chars[(j>=n-i)+(j==n+i+1)]);//put char draws a character at array index [BOOL(j>n-i) + BOOL(j==n+i+1)]
//BOOL will be 0 or 1; chars[(0 or 1 + 0 or 1)]
}
}
|
This persons algorithm is drawing blanks, asterisks, and newline depending on his index_algorithm, in the chars[?].
The first for-loop controls the y axis.
The second for-loop controls the x axis.
The second for-loop prints either ' ','*','\n'.
Third for-loop prints the trunk of the tree.
-----Second for-loop
putchar (chars[?]); printing out the character in the character_array chars, at index ?.
j>n-1 is going to be a value of 0 or 1
j==n+i+1 is going to be a value of 0 or 1
Add them together:
? minimum value is 0. (0+0)
? possible value is 1. (0+1) or (1+0)
? maximum value is 2. (1+1)
chars[0] == ' ';
chars[1] == '*'
chars[2] == '\n'
---third-loop similar to second loop
Hope this helps.