简体   繁体   English

搜索数组并打印出来

[英]Searching an Array, and printing it out

I have a 2d array. 我有一个二维数组。 In there is a character U . 其中有一个字符U My code searches for the character and then outputs the array, but only showing the U and every character around it. 我的代码搜索字符,然后输出数组,但仅显示U及其周围的每个字符。 Somewhere my code goes wrong and the U seems to be output just opposite of where it should be. 我的代码出了问题的某个地方, U输出似乎恰好相反。

The original array is gameboard[] , it is read in fine. 原始数组是gameboard[] ,可以很好地读取它。 Also we can't use vectors, pointers, just what i have already used 而且我们不能使用向量,指针,也不能使用我已经使用的

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;
 }

original array: 原始数组:

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

** Wanted output: **想要的输出:

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

here is a example of a random output i get: http://tinypic.com/r/2ujo8/6 这是我得到的随机输出的示例: http : //tinypic.com/r/2ujo8/6

Try something like this 试试这个

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  

       }
   }
}

Thought about switching rows and columns for printing? 想切换行和列以进行打印吗?

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;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM