L-12 MCS 275 Mon 5 Feb 2001
Below are the listings for the programs we discussed in class.
/* L-12 MCS 275 drawing patterns on the screen */
/* for width = 16, we create the following pattern:
x x x x
x x x x
x x x x
x x x x
x x x x
x x x x
x x x x
x x x x
x x x x
x x x x
x x x x
x x x x
x x x x
x x x x
x x x x
x x x x
*/
#include <stdio.h>
#define SYMBOL 'x'
void draw (int width);
/* draws a pattern on a grid of size width times width on the screen */
void make_pattern (int width, char grid[width][width],
int frame_start, int frame_end);
/* creates a pattern in the array grid, for the frame starting
at position frame_start and ending at frame_end-1 */
int main()
{
int width;
printf("Drawing a pattern on the screen.\n");
printf(" Give screen width : "); scanf("%d", &width);
draw(width);
return 0;
}
void draw (int width)
{
char pattern[width][width]; /* allocate space for pattern */
int i,j;
for (i=0; i < width; i++) /* initialize the pattern */
for (j=0; j < width; j++)
pattern[i][j] = ' ';
make_pattern(width,pattern,0,width);
for (i=0; i < width; i++) /* print the pattern */
{
for (j=0; j < width; j++)
putchar(pattern[i][j]);
printf("\n");
}
}
void make_pattern (int width, char grid[width][width],
int frame_start, int frame_end)
{
int i;
int frame = frame_end-frame_start;
if (frame > 1)
{
for (i=0; i < frame; i++) /* mark diagonal on grid */
grid[frame_start+i][frame_end-1-i] = SYMBOL;
/* create pattern left of the diagonal */
make_pattern(width,grid,frame_start,frame_start+frame/2);
/* create pattern right of the diagonal */
make_pattern(width,grid,frame_start+frame/2,frame_end);
}
}