Another Drawing Program
Here is a follow-up drawing program similar to the draw X program. After looking at these two programs, try scaling another letter.
/* Draw a scaled Y on the console or terminal window.
Takes input value of length of the arms of the Y.
C Tutorials for the home ed student
Ron R.
August 2005
Consider an Y which is 9 characters high and 9 characters wide
row column
0 123456789
1 \ /
2 \ /
3 \ /
4 \ /
5 Y
6 |
7 |
8 |
9 |
Note that:
the \ character appears where row = column
the / character appears where (row + column) = (2 * (arm + 1))
the Y appears where column = (arm + 1)
the | appears for arm length rows where column = (arm + 1)
therefore we can divide the character into 2 sections:
the first is the upper arms
the second is the center and lower arm
*/
#include
int main()
{
int arm; // length of arm
int width; // overall width (and height) of X
int row; // current row in printing
int col; // current column in printing
/* this is a good example of the use of a do/while
we want to collect the value from the user before
we test to see if it is within an acceptable range
so that the drawn x will fit in the screen
*/
do
{
printf(“Enter the length of the arm of the X (1-12, 0=Exit): “);
scanf(“%i”,&arm);
} while(arm < 0 || arm > 12);
// only draw if they have not entered a 0
if(arm > 0)
{
width = 2 * (arm + 1);
// give it some spacing
printf(“\n”);
// loop through each row of the top
for(row=1;row<=arm;row++)
{
// this will be repeated for each row
for(col=1;col
// check for back diagonal
if(row == col)
{
printf("\\");
}
// check for forward diagonal
else if((row + col) == width)
{
printf("/");
}
// print an empty space
else
{
printf(" "); // try replacing the space with a period
}
}
// goto a new line
printf("\n");
}
// now draw leg (including centre) of Y
// change initial value to 0 to get extra row for center Y
for(row=0;row<=arm;row++)
{
// this will be repeated for each row
// change initial value to 0 to get extra column
for(col=0;col<=arm;col++)
{
// time to print a character?
if(col == arm)
{
if(row == 0)
{
printf("Y");
}
else
{
printf("|");
}
}
// print an empty space
else
{
printf(" "); // try replacing the space with a period
}
}
// goto a new line
printf("\n");
}
}
return 0;
}