如何在2维数组中赋值

时间:2017-11-16 13:30:46

标签: c arrays multidimensional-array 2d

创建图块模式

char t1[11][11];
int i=0;
int j=0;

int size = 10;
//Putting "|" for tile1 (0,0) to (11,0)
for(i=0;i<11;i++)
{
    while(j=0)
    {
        t1[i][j] = '|';
    }
}

//Putting "-" for tile1 (0,0) to (0,11)
for(j=1;j<10;j++)
{
    while(i=0)
    {
        t1[i][j] = '-';
    }
}

//PRINTING  "|" for tile1 (0,0) to (11,0)
for(i=0;i<11;i++){
    while(j=0){
        printf("%c \n",t1[i][j]);
    }
    printf("\n");
}

//PRINTING "-" for tile1 (0,0) to (0,11)
for(j=1;j<10;j++)
{
    while(i=0)
    {
        printf("%c "), t1[i][j];
    }
}

所以基本上我希望输出就像给出的图像一样。 我声明了一个11,11大小的数组,我试图使用循环来分配数组中的字符。但由于某种原因它不起作用。

我知道我必须使用循环,所以我在纸上绘制了图案并记下了数组位置并试图分配使用循环。

3 个答案:

答案 0 :(得分:3)

您的计划中有很多错误。让我列出一些:

  • j = 00分配给jj == 0比较j是否等于0

  • 您尚未更新内部i循环中的jwhile

  • 如果您想要坚持图片中显示的确切模式,则必须从\n中删除printf("%c \n", t1[i][j]);

  • printf("%c "), t1[i][j];无法打印t1[i][j]。但printf("%c ", t1[i][j]);确实如此。

  • 您没有在任何地方为数组分配@字符。

让我们重新编写代码:

初始化数组:

char t1[11][11];

现在,在循环中,根据模式为此11 x 11数组中的每个元素分配一个字符。

for (int i = 0; i < 11; i++){
    for (int j = 0; j < 11; j++){

        // If its first or last column, then place a | at t1[i][j]
        if (j == 0 || j == 10){
            t1[i][j] = '|';
        }
        // Else If its the first or last row, then place a - at t1[i][j]
        else if (i == 0 || i == 10){
            t1[i][j] = '-';
        }
        // Else if its the second or second to last row/column
        // then place @ at t1[i][j]
        else if (i == 1 || i == 9 || j == 1 || j == 9){
            t1[i][j] = '@';
        }
        // Else if its the diagonal, then place a @ at t1[i][j]
        else if (i == j){
            t1[i][j] = '@';
        }
        // All remaining places would have a space.
        else{
            t1[i][j] = ' ';
        }
    }
}

在此之后,数组中的每个元素都将初始化为某个适当的值。现在,打印整个数组。

for (int i = 0; i < 11; i++){
    for (int j = 0; j < 11; j++){
        printf("%c\t", t1[i][j]);
    }
    printf("\n");
}

请参阅此代码运行live here

答案 1 :(得分:1)

使用时

printf("%c \n", t1[i][j]);

您正在从数组中编写一个字符,并使用换行符附加该字符,这会破坏您的模式 尝试更改此打印循环

 for (j = 0; j <= 10; ++j)  
 {  
   printf("%s\n" ,t1[j]);  
 }

至于撰写&#39; @&#39;我会做那样的循环

for (i = 1; i < 10; ++i)  
{  
  for (j = 1; j < 10; ++j)    
  {  
    if ((j == 1 || i == 1 || j == 9 || i == 9) || j == i)
      t1[i][j] = '@';
  }
}

答案 2 :(得分:0)

使用更大的2D阵列,您可以缩短代码:

Sending build context to Docker daemon  43.12MB
Step 1/17 : FROM python:3.6-alpine
 ---> a6beab4fa70b
 ---> Using cache
 ---> 3ad8815438e3
Step 3/17 : ENV PATH :$PATH
 ---> Using cache
 ---> 5b193bb0782c
Step 4/17 : ADD . /app
 ---> Using cache
 ---> d68569825af4
Step 5/17 : WORKDIR /app
 ---> Using cache
 ---> eb6cad48bc0d
Step 6/17 : RUN echo "===> Installing sudo to emulate normal OS 
behavior..."
 ---> Using cache
 ---> cd3b9a162fdd
Step 7/17 : RUN apk --update add sudo
 ---> Using cache
 ---> 29c02ac6379d
Step 8/17 : RUN apk --update add libxml2-dev libxslt-dev
 ---> Using cache
 ---> 484e19727ba8
Step 9/17 : RUN apk --update add python py-pip openssl ca-certificates
 ---> Using cache
 ---> 4f8b2f211bbe
Step 10/17 : RUN apk --update add --virtual build-dependencies 
python3-dev libffi-dev openssl-dev build-base gfortran
 ---> Using cache
 ---> acffb3024164
Step 11/17 : RUN apk --update add libpulse-dev
 ---> Running in 2f381aef57d1
fetch http://dl-
cdn.alpinelinux.org/alpine/v3.4/main/x86_64/APKINDEX.tar.gz
fetch http://dl-
cdn.alpinelinux.org/alpine/v3.4/community/x86_64/APKINDEX.tar.gz
ERROR: unsatisfiable constraints:
  libpulse-dev (missing):
    required by: world[libpulse-dev]
The command '/bin/sh -c apk --update add libpulse-dev' returned a non-
zero code: 1
相关问题