Search code examples
carrayscharfill

Recursive shape filling program in C


I have to make a program that takes in the perimeter of a shape, drawn in asterisks () by the user, and then fill up the shape with asterisks ().
It compiles fine but my problem is that when I re-print the perimeter of the shape, it prints funky symbols like:

1ÿu♤☺ x { ⇦@ ☺

Another problem is that once fill is executed the asterisks are just all printed on one line?

Here is my code(it's rather long, sorry)

#include <stdio.h>
#include "simpio.h"
#include "genlib.h"

void getArray(char array[][20]);
int getRow(void);
int getColumn(void);
void fill(char array[][20], int row, int column);
void dispArray(char array[][20]);
void dispMsg(void);

main()
{
      char array[20][20];
      int row, column;

      dispMsg();
      getArray(array);
      printf("Please enter an interior point from which the program starts filling.\n");
      row=getRow();
      column=getColumn();
      fill(array, row, column);
      dispArray(array);

      getchar();
}

void dispMsg(void)
{
      printf("This program will ask you to input the outline of a shape, and it will fill the shape up.\n");
      printf("To input the perimeter of your shape please use asterisks(*), and the <enter>     key to start a new line.\n");
}

void fill(char array[][20], int row, int column)
{
     if(array[row][column]!=' '||row>20||row<20||column>20||column<20)
     {
     }
     else
     {
         array[row][column]=='*';
         fill(array, row, column+1);
         fill(array, row+1, column);
         fill(array, row, column-1);
         fill(array, row-1, column);
     }
}

void dispArray(char array[][20])
{
     int i, j;

     for(i=0;i<20;i++)
     {
              for(j=0;j<20;j++)
              {
                               printf("%c", array[i][j]);
              }
     }
}

int getRow(void)
{
      int row;

      printf("Enter the row of the point: ");
      row=GetInteger();
      return(row);   
}

int getColumn(void)
{
      int column;

      printf("Enter the column of the point: ");
      column=GetInteger();
      return(column);   
}

void getArray(char array[][20])
{
      int i, j;
      char input;

      for(i=0;i<20;i++)
      { 
                       for(j=0;j<20;j++)
                       {
                                        array[i][j] = ' ';
                       }
      }
      while(true)
      {
                 input=getchar();
                 if(input=='\n')
                 {
                    i++;
                    j=0;
                 }
                 else if(input=='!')
                 {
                     break;
                 }
                 else
                 {
                    array[i][j]=input;
                    j++;
                 }
      }
      printf("Your input shape is: \n");
      for(j=0;j<20;j++)
      {
                       for(i=0;i<20;i++);
                       {
                               printf("%c", array[i][j]);
                       }
      }
}

Thank you very much :)


Solution

  • Another problem is that once fill is executed the asterisks are just all printed on one line?

    You never print a NL character:

    void dispArray(char array[][20])
    {
         int i, j;
    
         for(i=0;i<20;i++)
         {
                  for(j=0;j<20;j++)
                  {
                                   printf("%c", array[i][j]);
                  }
                  printf("\n");
         }
    }
    

    Effectively fill() seems weird:

     // what if row and column are equals to 5 by example?
        void fill(char array[][20], int row, int column) 
        {
             // If row = 5, then this condition is true, same for column = 5.
             // The array will never be filled so ! 
             if(array[row][column]!=' '||row>20||row<20||column>20||column<20)
             {
             }
             else
             {
                 array[row][column] = '*'; // By the way, you should use the assigment operator instead of comparison one.
                 fill(array, row, column+1);
                 fill(array, row+1, column);
                 fill(array, row, column-1);
                 fill(array, row-1, column);
             }
        }
    

    You should change the condition. You should maybe use the iterative way, which is more simpler.