最初整个数组将填充0 当有人进入士兵的位置时,它的值应为1。 最后它应该打印最终的数组。
我试过这个:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rows,columns,i,n,j,srow,scolumn;
printf("Enter size of the board : \t");
scanf("%d%d",&rows,&columns);
int board[rows][columns] ;
board[rows][columns] = 0;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
printf("%d \t",board[rows][columns]);
}
printf("\n");
}
printf("How many soldiers do you want to place ? : \t");
scanf("%d" , &n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\n Enter position of the soldier :\t" );
scanf("%d%d",&srow,&scolumn);
board[srow][scolumn] = 1;
}
}
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
printf("%d \t",board[rows][columns]);
}
printf("\n");
}
return 0;
}
答案 0 :(得分:3)
你的两个循环都有一个错误:
for(i=0;i<rows;i++) {
for(j=0;j<columns;j++) {
printf("%d \t",board[rows][columns]);
}
printf("\n");
}
应该是:
for(i=0;i<rows;i++) {
for(j=0;j<columns;j++) {
printf("%d \t",board[i][j]);
}
printf("\n");
}
修复此问题后,您还会注意到您没有正确初始化阵列。
答案 1 :(得分:3)
这应该适合你:
(如果您看到可以编写的代码更好&#39;,那么让代码恢复得比您找到的更好!)
#include <stdio.h>
#include <stdlib.h>
int main() {
//Variables
int rows = 0, columns = 0, soldiers = 0, soldierRow = 0, soldierColumn = 0;
int rowCount = 0, columnCount = 0, soldierCount = 0;
//Get Board size rows * columns
printf("Enter size of the board(rows and column):\n>");
scanf("%d %d", &rows, &columns);
int board[rows][columns];
//Initialize board
for(rowCount = 0; rowCount < rows; rowCount++) {
for(columnCount = 0; columnCount < columns; columnCount++)
board[rowCount][columnCount] = 0;
}
//Show the board
for(rowCount = 0; rowCount < rows; rowCount++) {
for(columnCount = 0; columnCount < columns; columnCount++)
printf("%2d\t", board[rowCount][columnCount]);
printf("\n");
}
//Check that no more soldiers is entered that there is space
do {
//Error Message
if(soldiers > (rows*columns))
printf("\nThere isn't enough space for so many soliders!\n");
//Read in Soldier
printf("How many soldiers do you want to place?\n>");
scanf("%d" , &soldiers);
} while(soldiers > (rows*columns));
//Check that it's a valid position on the board
for(soldierCount = 0; soldierCount < soldiers; soldierCount++) {
do {
//Read in the position
printf("\nEnter position of the soldier(row and column):\n>" );
scanf("%d %d" ,&soldierRow, &soldierColumn);
//Error Message
if((soldierRow < 1 || soldierRow > rows || soldierColumn < 1 || soldierColumn > columns))
printf("\nThe soldier position is out of the board size!");
} while(soldierRow < 1 || soldierRow > rows || soldierColumn < 1 || soldierColumn > columns);
board[soldierRow-1][soldierColumn-1] = 1;
}
//Print the board again
for(rowCount = 0; rowCount < rows; rowCount++) {
for(columnCount = 0; columnCount < columns; columnCount++)
printf("%d \t",board[rowCount][columnCount]);
printf("\n");
}
return 0;
}
可能的输入/输出:
Enter size of the board(rows and column):
>4 4
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
How many soldiers do you want to place?
>17
There isn't enough space for so many soliders!
How many soldiers do you want to place?
>2
Enter position of the soldier(row and column):
>23 23
The soldier position is out of the board size!
Enter position of the soldier(row and column):
>2 2
Enter position of the soldier(row and column):
>3 3
0 0 0 0
0 1 0 0
0 0 1 0
0 0 0 0
答案 2 :(得分:0)
您正在尝试打印未经注册的值,此处
board[rows][columns] = 0;
您要访问数组之外的元素,因为数组的最后一个元素是
board[rows - 1][columns - 1] = 0;
并且上一行只会初始化为0
最后一个元素,它肯定没有打印0
,是吗?它将所有元素初始化为0
这样做
memset(board, 0, columns * rows * sizeof(int));
上一行,也可以是
memset(board, 0, sizeof(board));
请在此行前检查srow < rows
和scolumn < columns
board[srow][scolumn] = 1;
还有另一个问题,这里
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
printf("%d \t",board[i /*i <-- rows */][j /*j <-- columns */]);
}
printf("\n");
}