【问题标题】:Searching an Array, and printing it out搜索数组并打印出来
【发布时间】:2013-04-04 23:44:28
【问题描述】:

我有一个二维数组。里面有一个字符U。我的代码搜索字符,然后输出数组,但只显示U 及其周围的每个字符。我的代码在某个地方出错了,U 的输出似乎与它应该在的位置相反。

原始数组是gameboard[],可以正常读取。我们也不能使用向量,指针,就像我已经使用过的一样

int boardSizeRow;
    int boardSizeCol;
    inputFile.open("C:\\Users\\Michael\\Desktop\\fileboard1.txt");
    inputFile >> boardSizeRow;
    inputFile >> boardSizeCol;
    inputFile.get();
    char gameBoard[21][21];
    for (int row = 0; row <= boardSizeRow; row++)
    {
        for (int col = 0; col <= boardSizeCol; col++)
        {
            gameBoard[row][col] = inputFile.get();
        }
        //inputFile.get();
    }
    for (int row = 0; row <= boardSizeRow; row++)
    {
        for (int col = 0; col <= boardSizeCol; col++)
        {
            cout << gameBoard[row][col];
        }
        //cout << endl;
    }
bool toPrint[21][21] = {false}; 
for (int i = 0; i < boardSizeRow; i++ )
{
    for (int j = 0; j < boardSizeCol; j++)
    {
        if (gameBoard[i][j] == 'U')
       {
           toPrint[i][j] = true; 
           toPrint[i][j-1] = true; //West
           toPrint[i][j+1] = true; //East
           toPrint[i-1][j] = true;  //North
           toPrint[i+1][j] = true; //South  

       }
   }
}
for (int i = 0; i < boardSizeRow; i++ )
{
    for (int j = 0; j < boardSizeCol; j++)
    {
       if (toPrint[i][j] == true)
       {            
           cout << gameBoard[i][j];
       }
       else
       {
           cout << "0";
       }
    }
    cout<<endl;
 }

原始数组:

WWWWWWWWWW
W WW GO  W
W WW WWW W
W   W   GW
WPWG  WW W
WWWDWK  WW
W  GW W  W
W WW  KWAW
W   SW  UW
WWWWWWWWWW

** 想要的输出:

0000000000
0000000000
0000000000
0000000000
0000000000
0000000000
00000000A0
00000000UW
00000000W0

这是我得到的随机输出示例:http://tinypic.com/r/2ujo8/6

【问题讨论】:

  • 打印顺序应为:北、西、U、东、南。这是你所期望的吗?
  • 你确定你认为它应该在哪里就是它真正在哪里吗?构建一个完整的、可运行的示例,显示您认为不正确的行为。
  • 如果gameBoard[0][0] = 'U',那么负索引将用于toPrint数组。
  • 这样安全吗? toPrint[i][j+1] = true; //East 我的意思是当j= boardSizeCol 你将在阵列外访问。我认为这应该给你错误
  • 完全偏离主题,但“if (toPrint[i][j] == true)”有点重复......

标签: c++ arrays for-loop multidimensional-array nested-loops


【解决方案1】:

试试这样的

for (int i = 0; i < boardSizeRow; i++ )
{
    for (int j = 0; j < boardSizeCol; j++)
    {
        if (gameBoard[i][j] == 'U')
       {

           toPrint[i][j] = true; 
           if(j!=0)
            toPrint[i][j-1] = true; //West
           if(j!=(boardSizeCol-1))
            toPrint[i][j+1] = true; //East
           if(i!=0)
           toPrint[i-1][j] = true;  //North
           if(i!=(boardSizeRow -1))
           toPrint[i+1][j] = true; //South  

       }
   }
}

【讨论】:

  • 这仍然不安全。你需要检查j &lt; (boardSizeCol-1)和i &lt; (boardSizeRow-1)
  • 不要介意我之前的评论。考虑到 for 循环退出条件,它是等效的。
【解决方案2】:

考虑过切换行列进行打印吗?

for (int j = 0; j < boardSizeCol; j++)
{
    for (int i = 0; i < boardSizeRow; i++ )
    {
       if (toPrint[i][j] == true)
       {            
           cout << gameBoard[i][j];
       }
       else
       {
           cout << "0";
       }
    }
    cout<<endl;
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 2012-09-25
    相关资源
    最近更新 更多