在数组中打印'box'

时间:2013-10-24 15:06:33

标签: c arrays function

因为我的第一年任务所需的子任务,所以我每天都面临脱发。

我需要在初始化期间在坐标[5] [5] /即电路板中心的数字周围打印一个''计划的阶段。

'box'包含覆盖边的“ | ”和坐标[5] [5]处数字顶部和底部的“ _ ” ]

当我执行此程序时,电路板显示但'不显示。 为什么会这样?

以下是代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//FUNCTION: Draw the Board
int drawBoard()
{
    //Declare array size
    int board[9][9];

    //initialize variables
    int rows, columns, randomNumber, flag;

    //random number seed generator
    srand(time(NULL));

        for ( rows = 0 ; rows < 9 ; rows++ )
        {

            for ( columns = 0 ; columns < 9 ; columns++ )
            {
                flag = 0;

                do
               {
                    //generate random numbers from 2 - 8

                    randomNumber = rand() %7 + 2;

                    board[rows][columns] = randomNumber;

                        //Display the 'box' if rows and columns == 5 / i.e - board[5][5]
                        if ( rows == 5 && columns == 5 )
                        {   //Checks for 2 adjacent numbers
                            if  ( board[rows][columns] == board[rows - 1][columns] || board[rows][columns] == board[rows][columns - 1] )
                            {
                                flag = 0;
                                continue;
                            }
                            else
                            {
                                flag = 1;

                                //Print 'box'
                                marker( rows, columns );
                            }
                        }

                    //Checks for 2 adjacent numbers.
                    if  ( board[rows][columns] == board[rows - 1][columns] || board[rows][columns] == board[rows][columns - 1] )
                        {
                            flag = 0;
                            continue;
                        }

                    else
                    //Prints the correct board
                         {
                                flag = 1;
                                printf( "  %d  ", board[rows][columns] );
                         }

                } while ( flag == 0 ); //end outer do-while

            }//end inner for-loop

            printf("\n\n");

        }//end outer for-loop

}//end FUNCTION drawBoard

//FUNCTION: Mark the surrounding of the number with "|" and "_" at board[5][5]
void marker( int x, int y, int** board )
  {
    board[x][y-1] == "\n _ ";
    board[x][y+1] == "\n _ ";
    board[x-1][y] == " |";
    board[x+1][y] == "| ";
  }

int main()
{
    drawBoard();
}

TI

3 个答案:

答案 0 :(得分:0)

void marker( int x, int y, int** board )
  {
    board[x][y-1] == "\n _ ";
    board[x][y+1] == "\n _ ";
    board[x-1][y] == " |";
    board[x+1][y] == "| ";
  }

这不起作用。 '=='用于比较,而不是分配。你需要'='。 但是,board是一个int数组,你不能在int数组中赋值const char*(因为那是"something")。你的董事会应该是一个char数组,然后你可以像这样分配:

void marker( int x, int y, char** board )
  {
    board[x][y-1] = '_';
    board[x][y+1] = '_';
    board[x-1][y] = '|';
    board[x+1][y] = '|';
  }

另一个问题是电路板的中心位于索引[4] [4],即9号表的第五个元素。但这是一个较小的问题......

答案 1 :(得分:0)

我看到你想在这里做什么,这不是我通常想的方式,所以我可能没有帮助:s

首先是一个错误: 在"drawboard": marker( rows, columns )

"marker": void marker( int x, int y, int** board )的原型。我不认为使用旗帜进行编译会起作用(是的,旗帜有用,请尝试"gcc -Wall -Wextra -Werror"

而且,我认为你应该重新思考你的功能“标记”,它无法正常工作。

我认为board[x][y-1] = "\n _ "可以解决这个问题,如果你return(board[x][y-1]),我会再次使用char**,而不是整数制表符,并且有许多短函数(并使用过) write代替printf:p)

答案 2 :(得分:0)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

//FUNCTION: Draw the Board
char tochar(int i)
{
    return i+'0';
}
void marker( int x, int y, char board[][19] );
int check(int x, int y, char board[][19])
{
    if(x>1&&board[2*x+1][2*y+1]==board[2*x-1][2*y+1])
        return 1;

    if(y>1&&board[2*x+1][2*y+1]==board[2*x+1][2*y-1])
        return 1;
   return 0;
}
int drawBoard()
{
    //Declare array size
    char board[19][19];

    memset(board,32, sizeof(board));
    //initialize variables
    int rows, columns, randomNumber, flag;

    //random number seed generator
    srand(time(NULL));

        for ( rows = 0 ; rows < 9 ; rows++ )
        {

            for ( columns = 0 ; columns < 9 ; columns++ )
            {
                flag = 0;

                do
               {
                    //generate random numbers from 2 - 8

                    randomNumber = rand() %7 + 2;
                    board[2*rows+1][2*columns+1] = tochar(randomNumber);

                        //Display the 'box' if rows and columns == 5 / i.e - board[5][5]
                        if ( rows == 4 && columns == 4 )
                        {   //Checks for 2 adjacent numbers
                            if  ( check(rows, columns, board))
                            {
                                flag = 0;
                            }
                            else
                            {
                                flag = 1;

                                marker( 2*rows+1, 2*columns+1, board );
                                //Print 'box'
                            }
                        }
                    else
                    {
                    //Checks for 2 adjacent numbers.
                    if  ( check(rows, columns, board))
                        {
                            flag = 0;
                        }

                    else
                    //Prints the correct board
                         {
                            flag = 1;
//                            printf( "  %c  ", board[2*rows+1][2*columns+1] );
                         }
                    }
                } while ( flag == 0 ); //end outer do-while

            }//end inner for-loop

            printf("\n\n");

        }//end outer for-loop
    for(rows=0;rows<19;rows++)
    {
        for(columns=0;columns<19;columns++)
        {
            printf("%c",board[rows][columns]); 
        }
        printf("\n");
    }
}//end FUNCTION drawBoard

//FUNCTION: Mark the surrounding of the number with "|" and "_" at board[5][5]
void marker( int x, int y, char board[][19] )
  {
    board[x][y-1] = '|';
    board[x][y+1] = '|';
    board[x-1][y] = '_';
    board[x+1][y] = '_';
   /* 
192
191
217
218
*/
  }

int main()
{
    drawBoard();
}