About two weeks ago, I posted a similar question, and I solved my problem. However, after some modifications, my program is failing to work. The instructions are to takes in the perimeter of a shape, drawn in asterisks () by the user, and then fill up the shape with asterisks ().
There are two problems:
1) The program fails to print the full input shape (at end of getArray function)
2) The program does not fill an input shape problem for row > 9 or column > 9.
I would like to know why these problems occur, and how to fix them.
Here is the program code (sorry for the lengthiness):
#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("\nPlease 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");
}
void fill(char array[][20], int row, int column)
{
if(array[row][column] != ' '|| row>20 || row<0 || column>20 || column<0)
{
}
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++)
{
printf("\n");
for(j = 0; j < 20; j++)
{
printf("%c", array[i][j]);
}
}
}
int getRow(void)
{
int row;
printf("\nEnter the row of the point: \n");
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, row = 0, column = 0, num;
char input;
printf("To input the perimeter of your shape please use asterisks(*), and the <enter> key to start a new line.\nEnd the input with the '!' signal\n\n");
for(i=0;i<20;i++)
{
for(j=0;j<20;j++)
{
array[i][j] = ' ';
}
}
i=0;
j=0;
while(true)
{
input=getchar();
if(input=='\n')
{
i++;
j=0;
row++;
column = num;
num = 0;
}
else if(input=='!')
{
if(array[i-1][j] == '*') row--;
break;
}
else
{
array[i][j] = input;
j++;
num++;
}
}
printf("Your input shape is: \n");
for(i=0;i <= row;i++)
{
printf("\n");
for(j=0;j <= column;j++)
{
printf("%c", array[i][j]);
}
}
}
For example:
If the user enters:
**************
* *****
* *
* *
* *
******************
The output will be:
**************
******************
******************
******************
******************
******************
However, for my program:
When the input is re-printed, this comes out:
**************** *
* ***** *
* * *
* * *
* ***
And when the fill is printed:
******************
* ****************
* ****************
* ****************
******************
******************
Help would be much appreciated, thanks :)
The problem is your calculations of the number of columns.
There are two things you have to remember:
The first is that integer division truncates the result. For example, with the input in your question you have column
as 44
and row
as 6
, resulting in 44 / 6
which is 7.3333333...
. However, since it's an integer operation you will get 7
instead.
The second thing is that you simply can't get the number of columns by dividing the number of asterisks with the number of rows. Instead you need a separate counter to count the number of columns for the current row. If it's larger than the current column
value you set the new number of columns.