网格中的空心方块
Hollow Square in Grid
所以我一直在尝试在网格内部绘制一个空心正方形。我最初使用宽度为 10 和高度为 5 的二维数组用“*
”的字符制作了网格。当从给定的特定坐标更改每个数组值时,我从正方形左上角的坐标绘制正方形.问题是当我打印出来时,它只完成了外面的一半。
重写部分网格数组时,我是漏掉了一个条件还是条件太多了?谢谢你的帮助。
int main(){
char grid[5][10];//initialize array that will be row and column
for(int i=0;i<5;i++){
for(int j=0;j<10;j++){
grid[i][j]= '*';
}//for
}//for loop to fill grid with asterisk
cout << "GRID" << endl << endl;
for(int i=0;i<5;i++){
for(int j=0;j<10;j++){
cout << grid[i][j] ;
}//for
cout << endl;
}//for loop to print grid no shapes inside
cout << endl << endl;
int x = 2;
int y = 3;
int size = 3;
char c = 'o';
//will have a condition here to check if it can fit inside the
//grid but this is just to test it will be a member function.
for(int n=x-1;n<x+size-1; n++){
for(int p=y-1;p<y+size-1; p++){
if (n == x-1 || n==x+size-1 || p == y-1 || p== y+size-1 ){
grid[n][p] = c;
}
else
grid[n][p] = '*';
}//for
}//for loop to rewrite specific array coordinates with new c
cout << "Shape inside grid." << endl;
for(int n=0;n<5;n++){
for(int p=0;p<10;p++){
cout << grid[n][p];
}//for
cout << endl;
}//for loop to print new grid
return 0;
}
/*
This is my output:
**********
**ooo*****
**o*******
**o*******
**********
This is what I need:
**********
**ooo*****
**o*o*****
**ooo*****
**********
*/
问题出在你设置为 'o'
正方形边框的双 for
中。
for(int n=x-1;n<x+size-1; n++){
for(int p=y-1;p<y+size-1; p++){
if (n == x-1 || n==x+size-1 || p == y-1 || p== y+size-1 ){
grid[n][p] = c;
}
else
grid[n][p] = '*';
}
}
你的意图,如果我理解得很好的话,是遍历正方形的点(for(int n=x-1;n<x+size-1; n++)
和for(int p=y-1;p<y+size-1; p++)
),检查点是否是边界点(if (n == x-1 || n==x+size-1 || p == y-1 || p== y+size-1 )
)和 (a) 在边界情况下,设置 c
(即 'o'
),(b) 否则设置 '*'
.
很好。
但是你失败了,因为 n
和 p
的范围从 x-1
和 y-1
到 x+size-1
和 y+size-1
], 排除.
所以n
不可能是x+size-1
:上限(含)是x+size-2
。而p
不能是y+size-1
:上限(含)是y+size-2
.
结论:你的测试应该是
if (n == x-1 || n==x+size-2 || p == y-1 || p== y+size-2 )
p.s.: 对不起我的英语不好
所以我一直在尝试在网格内部绘制一个空心正方形。我最初使用宽度为 10 和高度为 5 的二维数组用“*
”的字符制作了网格。当从给定的特定坐标更改每个数组值时,我从正方形左上角的坐标绘制正方形.问题是当我打印出来时,它只完成了外面的一半。
重写部分网格数组时,我是漏掉了一个条件还是条件太多了?谢谢你的帮助。
int main(){
char grid[5][10];//initialize array that will be row and column
for(int i=0;i<5;i++){
for(int j=0;j<10;j++){
grid[i][j]= '*';
}//for
}//for loop to fill grid with asterisk
cout << "GRID" << endl << endl;
for(int i=0;i<5;i++){
for(int j=0;j<10;j++){
cout << grid[i][j] ;
}//for
cout << endl;
}//for loop to print grid no shapes inside
cout << endl << endl;
int x = 2;
int y = 3;
int size = 3;
char c = 'o';
//will have a condition here to check if it can fit inside the
//grid but this is just to test it will be a member function.
for(int n=x-1;n<x+size-1; n++){
for(int p=y-1;p<y+size-1; p++){
if (n == x-1 || n==x+size-1 || p == y-1 || p== y+size-1 ){
grid[n][p] = c;
}
else
grid[n][p] = '*';
}//for
}//for loop to rewrite specific array coordinates with new c
cout << "Shape inside grid." << endl;
for(int n=0;n<5;n++){
for(int p=0;p<10;p++){
cout << grid[n][p];
}//for
cout << endl;
}//for loop to print new grid
return 0;
}
/*
This is my output:
**********
**ooo*****
**o*******
**o*******
**********
This is what I need:
**********
**ooo*****
**o*o*****
**ooo*****
**********
*/
问题出在你设置为 'o'
正方形边框的双 for
中。
for(int n=x-1;n<x+size-1; n++){
for(int p=y-1;p<y+size-1; p++){
if (n == x-1 || n==x+size-1 || p == y-1 || p== y+size-1 ){
grid[n][p] = c;
}
else
grid[n][p] = '*';
}
}
你的意图,如果我理解得很好的话,是遍历正方形的点(for(int n=x-1;n<x+size-1; n++)
和for(int p=y-1;p<y+size-1; p++)
),检查点是否是边界点(if (n == x-1 || n==x+size-1 || p == y-1 || p== y+size-1 )
)和 (a) 在边界情况下,设置 c
(即 'o'
),(b) 否则设置 '*'
.
很好。
但是你失败了,因为 n
和 p
的范围从 x-1
和 y-1
到 x+size-1
和 y+size-1
], 排除.
所以n
不可能是x+size-1
:上限(含)是x+size-2
。而p
不能是y+size-1
:上限(含)是y+size-2
.
结论:你的测试应该是
if (n == x-1 || n==x+size-2 || p == y-1 || p== y+size-2 )
p.s.: 对不起我的英语不好