I'm trying to write a program in C that gets a shape outlined with asterisks from the user, and returns that shape filled with asterisks. It will also get the coordinates of a point inside the shape from which to start filling. I need to use recursion here. for example, to be clear:
input: (ignore the line, think of that as blank space)
*****
*___*
*****
coordinates are (2,1) /*its an array so numbering will start from 0*/
output
*****
*****
*****
my program compiles but for some reason everything in the shape and past its boundaries get filled with asterisks. it gets filled to the maximum size i've set the array to, even if the shape isn't that big. its like it completely ignores when it gets to a boundary of asterisks when it's filling.
here is the program i've written, its rather long...
input: (ignore the line, think of that as blank space)
*****
*___*
*****
coordinates are (2,1) /*its an array so numbering will start from 0*/
output
*****
*****
*****
my program compiles but for some reason everything in the shape and past its boundaries get filled with asterisks. it gets filled to the maximum size i've set the array to, even if the shape isn't that big. its like it completely ignores when it gets to a boundary of asterisks when it's filling.
here is the program i've written, its rather long...
Code:
#include "simpio.h" #include <stdio.h> #include "genlib.h" #define size 20; void GetArray(char arr[][20]); void fill(char arr[][20], int row, int col); void disp(char arr[][20]); main() { char arr[20][20]; int row, col; GetArray(arr); printf("\nEnter row of interior point: "); row = GetInteger(); printf("Enter column of interior point: "); col = GetInteger(); fill(arr, row, col); disp(arr); getchar(); system("pause"); } void GetArray(char arr[][20]) { char input; int i,j; for(i=0;i<20;i++) { for(j=0;j<20;j++) { arr[i][j] = ' '; } } printf("Enter the closed shape using asterisks and spaces. Keep the number of rows and columns under 20\n"); printf("To signal the end of the input, type '!'. Use the 'enter' key to move down rows\n\n"); i = 0; j = 0; while(TRUE) { input = getchar(); if(input == 'r') i++; else if(input == '!') break; else { arr[i][j] = input; j++; } } i=0; j=0; printf("\n\nThe input shape is:\n"); for(i=0;i<20;i++) { for(j=0;j<20;j++) { printf("%c",arr[i][j]); } } } void fill(char arr[][20], int row, int col) { if(arr[row][col]!=' '|| row>=20 || col>=20 || row<0 || col<0) {} else { arr[row][col] ='*'; fill(arr,row+1,col); fill(arr,row-1,col); fill(arr,row,col+1); fill(arr,row,col-1); } } void disp(char arr[][20]) { int i,j; printf("\nThe filled shape is:\n"); for(i=0;i<20;i++) { for(j=0;j<20;j++) { printf("%c",arr[i][j]); } printf("\n"); } }
Comment