Ok thanks for helping me now I just have to fix the errors.
The errors are from ROW and COL, you have to define them.
Ok thanks for helping me now I just have to fix the errors.
The errors are from ROW and COL, you have to define them.
How would I define them? I can't make them constants because they can change.
How?
Well you can use 1d array to represent 2d array. Or you can create a
2d array at runtime, or you can use 2d array of vectors. Show your code as
of now and let see.
Here is what I have now.
#include <stdio.h>
#include "simpio.h"
printf("no. rows: ");
int row=GetInteger();
printf("\nno. columns: ");
int column=GetInteger();
void getshape(char shape[row][column]);
void fill(char shape[row][column]);
int main()
{
int r, c;
char shape[row][column];
getshape(shape, row, column);
fill(shape, row, column);
getchar();
return 0;
}
void getshape(char shape[row][column])
{
char end='.';
printf("Enter a shape and when your done enter '.'\n");
while ((end=getchar()) != '.')
{
shape=getchar();
}
}
void fill(char shape[row][column])
{
int r, c;
printf("Enter the interior point(where the program will start filling)\n");
printf("Enter the row number\n");
r=GetInteger();
printf("Enter the column number\n");
c=GetInteger();
if (r < 0 || r > row || c > column || c < 0) return;
if (shape[r][c] == '*' || shape[r][c] == '#') return;
fill[r][c] = '*';
fill(shape,r+1,c);//down
fill(shape,r-1,c);//up
fill(shape,r,c-1);//left
fill(shape,r,c+1);//right
}
This
printf("no. rows: ");
int row=GetInteger();
printf("\nno. columns: ");
int column=GetInteger();
Is not going to work. Have Row and Col be a big number.
Then ask the user to input row and col and use that.
For example :
int main()
{
const int MAX_ROW = 200;
const int MAX_COL = 200;
char Array[MAX_ROW][MAX_COL] = {0};
int ROW = 0;
int COL = 0;
cout << "Enter row and column : ";
cin >> ROW >> COL;
//now use Array, thinking that ROW and COL are its limit.
}
ok.
These are the errors I am getting now:
`void fill(char (*)[200], int, int)':
pointer to a function used in arithmetic
assignment of read-only location
cannot convert `char' to `void ()(char (*)[200], int, int)' in assignment
This is my code:
#include <stdio.h>
#include "simpio.h"
const int MAX_ROW = 200;
const int MAX_COL = 200;
void getshape(char shape[MAX_ROW][MAX_COL], int row, int column);
void fill(char shape[MAX_ROW][MAX_COL], int row, int column);
int main()
{
int r, c;
char shape[MAX_ROW][MAX_COL];
int row=0, column=0;
printf("Enter row and column : ");
row=GetInteger();
column=GetInteger();
getshape(shape, row, column);
fill(shape, row, column);
getchar();
return 0;
}
void getshape(char shape[MAX_ROW][MAX_COL], int row, int column)
{
char end='.';
printf("Enter a shape and when your done enter '.'\n");
while ((end=getchar()) != '.')
{
shape[row][column]=getchar();
}
}
void fill(char shape[MAX_ROW][MAX_COL], int row, int column)
{
int r, c;
printf("Enter the interior point(where the program will start filling)\n");
printf("Enter the row number\n");
r=GetInteger();
printf("Enter the column number\n");
c=GetInteger();
if (r < 0 || r > row || c > column || c < 0) return;
if (shape[r][c] == '*' || shape[r][c] == '#') return;
fill[r][c] = '*';
fill(shape,r+1,c);//down
fill(shape,r-1,c);//up
fill(shape,r,c-1);//left
fill(shape,r,c+1);//right
}
Change the name from fill to fillArray and see what happens.
Okay I did that and I'm getting the same errors.
#include <stdio.h>
#include "simpio.h"
const int MAX_ROW = 200;
const int MAX_COL = 200;
void getshape(char shape[MAX_ROW][MAX_COL], int row, int column);
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column);
int main()
{
int r, c;
char shape[MAX_ROW][MAX_COL];
int row=0, column=0;
printf("Enter row and column : ");
row=GetInteger();
column=GetInteger();
getshape(shape, row, column);
fillArray(shape, row, column);
getchar();
return 0;
}
void getshape(char shape[MAX_ROW][MAX_COL], int row, int column)
{
char end='.';
printf("Enter a shape and when your done enter '.'\n");
while ((end=getchar()) != '.')
{
shape[row][column]=getchar();
}
}
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column)
{
int r, c;
printf("Enter the interior point(where the program will start filling)\n");
printf("Enter the row number\n");
r=GetInteger();
printf("Enter the column number\n");
c=GetInteger();
if (r < 0 || r > row || c > column || c < 0) return;
if (shape[r][c] == '*' || shape[r][c] == '#') return;
fillArray[r][c] = '*';
fillArray(shape,r+1,c);//down
fillArray(shape,r-1,c);//up
fillArray(shape,r,c-1);//left
fillArray(shape,r,c+1);//right
}
this is your problem :
fillArray[r][c] = '*';
that should be
shape[r][c] = '*';
Okay now the errors are gone however, the program is repeating this process over and over.
printf("Enter the interior point where the program will start filling\n");
printf("Enter the row number\n");
r=GetInteger();
printf("Enter the column number\n");
c=GetInteger();
it doesn't end.
Put that code inside your main, before calling the fillArray function.
Then pass that onto the fill Array function like so :
int main(){
//....
cin >> startX >> startY
fillArray(Array,startX,startY);
//...
}
ok
ok
Make sure that you check if startX and startY is within the boundaries
of the array, and also that its not equal to the wall.
ok.
I figured out why my program was repeating that same process And it turns out that i don't need to ask for interior points that is what row and column is for. Also when i looked at the code that fills in the array the fillarray functions only work in four direction. Nevertheless, My program now crashes.
#include <stdio.h>
#include "simpio.h"
const int MAX_ROW = 200;
const int MAX_COL = 200;
void getshape(char shape[MAX_ROW][MAX_COL]);
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column);
int main()
{
int r, c;
char shape[MAX_ROW][MAX_COL];
int row=0, column=0;
printf("Enter the interior point where the program where start filling\n");
row=GetInteger();
column=GetInteger();
getshape(shape);
shape[MAX_ROW][MAX_COL] = '*';
fillArray(shape, row, column);
getchar();
return 0;
}
void getshape(char shape[MAX_ROW][MAX_COL])
{
char end='.';
printf("Enter a shape and when your done enter '.'\n");
while ((end=getchar()) != '.')
{
shape[MAX_ROW][MAX_COL]=getchar();
}
return;
}
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column)
{
if (shape[row][column] == '*' || shape[row][column] == '#') return;
else
{
shape[row][column] = '*';
fillArray(shape,row+1,column);//down
fillArray(shape,row-2,column);//up
fillArray(shape,row,column-1);//left
fillArray(shape,row,column+2);//right
}
}
>> shape[MAX_ROW][MAX_COL] = '*';
That is wrong. Using MAX_ROW and MAX_COL is wrong. It is 1 passed the
index, and does not contain any valid data. Just leave that code out.
Okay however, it's still crashing.
#include <stdio.h>
#include "simpio.h"
const int MAX_ROW = 200;
const int MAX_COL = 200;
void getshape(char shape[MAX_ROW][MAX_COL]);
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column);
int main()
{
int r, c;
char shape[MAX_ROW][MAX_COL];
int row=0, column=0;
printf("Enter the interior point where the program where start filling\n");
row=GetInteger();
column=GetInteger();
getshape(shape);
fillArray(shape, row, column);
getchar();
return 0;
}
void getshape(char shape[MAX_ROW][MAX_COL])
{
char end='.';
printf("Enter a shape and when your done enter '.'\n");
while ((end=getchar()) != '.')
{
shape[MAX_ROW][MAX_COL]=getchar();
}
return;
}
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column)
{
if (shape[row][column] == '*' || shape[row][column] == '#') return;
else
{
shape[row][column] = '*';
fillArray(shape,row+1,column);//down
fillArray(shape,row-2,column);//up
fillArray(shape,row,column-1);//left
fillArray(shape,row,column+2);//right
}
}
Inside your fill function, check if row and col is out of bounds.
Sorry to bring back an old thread however, the code for the fill function seems to be incorrect because since the program is recursive it isn't getting past the first function
fillArray(shape,row+1,column);//down
Also the program is crashing. Here is my current program.
#include <stdio.h>
#include "simpio.h"
const int MAX_ROW = 200;
const int MAX_COL = 200;
void getshape(char shape[MAX_ROW][MAX_COL]);
void getsize(int r, int c, char shape[MAX_ROW][MAX_COL]);
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column);
void result(char shape[MAX_ROW][MAX_COL], int r, int c);
int main()
{
char shape[MAX_ROW][MAX_COL];
int row=0, column=0;
printf("Enter the interior point where the program where start filling\n");
row=GetInteger();
column=GetInteger();
getshape(shape);
int r, c;
getsize(r, c, shape);
fillArray(shape, row, column);
result(shape, r, c);
getchar();
return 0;
}
void getshape(char shape[MAX_ROW][MAX_COL])
{
char end='.';
printf("Enter a shape and when your done enter '.'\n");
while ((end=getchar()) != '.')
{
shape[MAX_ROW][MAX_COL]=getchar();
}
return;
}
void getsize(int r, int c, char shape[MAX_ROW][MAX_COL])
{
int i, j;
int columns = 1000, rows = 1000;
for(i=0;i<rows;i++)
{
for(j = 0; j <columns; j++)
{
if(shape[i][j] == '\0' && shape[i-1][j] == '\0')
{
columns = j;
break;
}
if(shape[i][j] == '\0' && shape[i][j-1] == '\0'){
rows = i;
break;
}
}
if(rows != 1000 && columns != 1000) break;
}
}
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column)
{
if (shape[row][column] == '*' || shape[row][column] == '#') return;
else
{
shape[row][column] = '*';
fillArray(shape,row+1,column);//down
fillArray(shape,row-2,column);//up
fillArray(shape,row,column-1);//left
fillArray(shape,row,column+2);//right
}
}
void result(char shape[MAX_ROW][MAX_COL], int r, int c)
{
for (int rs=0; rs<r; r++)
{
printf("\n");
for (int cs=0; cs<c; c++)
{
printf("%c", shape[rs][cs]);
}
}
}
This is wrong :
void getshape(char shape[MAX_ROW][MAX_COL])
{
char end='.';
printf("Enter a shape and when your done enter '.'\n");
while ((end=getchar()) != '.')
{
shape[MAX_ROW][MAX_COL]=getchar();
}
return;
}
shape[MAX_ROW][MAX_COL] is invalid memory access.
This is also wrong :
void fillArray(char shape[MAX_ROW][MAX_COL], int row, int column)
{
if (shape[row][column] == '*' || shape[row][column] == '#') return;
else
{
shape[row][column] = '*';
fillArray(shape,row+1,column);//down
fillArray(shape,row-2,column);//up
fillArray(shape,row,column-1);//left
fillArray(shape,row,column+2);//right
}
}
I don't know why you are moving column-2 or column+2. Move only
1 step.
Well not to be mean but think about it for a second. If I only use one step for the functions its going to go down then go back to the beginning then go left and back to the beginning. As for getting the shape that is really confusing me.
Well not to be mean but think about it for a second. If I only use one step for the functions its going to go down then go back to the beginning then go left and back to the beginning.
The function fills the bottom row first, until it hits the wall.
When it hits the wall it fills the top row, then again when it hits the wall it fills the
left column, and then it fills the right column. When you subtract or
add by two, you might have invalid memory access. If it bothers you
that much, you can always change the orientation of how the
recursive function is called, for example you can call it like so :
fillArray(shape,row+1,column);//down
fillArray(shape,row,column-1);//left
fillArray(shape,row-2,column);//up
fillArray(shape,row,column+2);//right
As for your getShape function, I cannot help you because
I don't know what the requirements are.
Oh sorry about that I guess that I didn't think about it. About the getshape function prototype I was not told of any requirements.
What does it supposed to do, make the user specify the shape of the
function? For example, is the input supposed to be something like this :
#####
# #
# #
#####
Should the user input a hallow box?
Oh sorry about that I guess that I didn't think about it. About the getshape function prototype I was not told of any requirements.
What exactly is your intent for getShape()? I'm guessing it is intended to select a fill character. But the net result of the function currently seems to be nothing except attempting to store a character outside the limits of your "shape" array, which is probably the source of your crashes.
getshape is supposed to get any shape from the user. The user can input a hallow box but the program needs to work with any shape.
If that's the case, your function is definitely not correct. You need to find a way to traverse the entire array. Currently, all your inputs are going to one element/coordinate that is out-of-bounds.
How do you traverse a 1 dimensional array? Take that and extrapolate it to the 2-dimensional array that you have.
Personally, I would not want to be the one to test this program, I do not like the idea of entering 40,000+ characters every time just to see if the recursion actually works as intended.
Could you clarify that a little more I'm not sure I understand.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.