Monday, March 16, 2009

ecp1026 t5

#include < stdio.h >
/* This program lets you specify a 65x32 text file representing a
* picture and 2 numbers representing coordinates and then does a
* recursive area fill. (Note that 65 includes the carriage return
* so actually it looks like just 64 characters wide.) */
#define XMAX 65
#define YMAX 32
char screen[XMAX][YMAX];
void printScreen(void);
void fill(int, int);
int main(int argc, char *argv[])
{ FILE *in;
int x, y;
if (argc!= 4)
{ fprintf(stderr,"Usage:\n\t%s <65x32 text file> \n",argv[0]);
exit(1);
}
in = fopen(argv[1],"r");
if (in == NULL)
{ printf("Error in opening input file.");
exit(1);
}
/* Read the 65x32 text file */
for (y=0; y< YMAX; y++)
{ for (x=0; x< XMAX; x++)
screen[x][y] = fgetc(in);
}
if (fclose(in))
{ printf("Error in closing input file.");
exit(1);
}
printScreen();
x = atoi(argv[2]);
y = atoi(argv[3]);
fill(x,y);
printScreen();
}
void printScreen(void)
{ int x,y;
for (y=0; y< YMAX; y++)
for (x=0; x< XMAX; x++)
putchar(screen[x][y]);
}

void fill (int x, int y)
{
if (screen[x][y]!=' ')
return;
screen[x][y]='X';
fill(x, y+1);
fill(x, y-1);
fill(x+1, y);
fill(x-1, y);
}




asd t5.c 65 32

No comments: