首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在二维数组中赋值

如何在二维数组中赋值
EN

Stack Overflow用户
提问于 2017-11-16 21:30:46
回答 3查看 4.6K关注 0票数 1

创建平铺图案

代码语言:javascript
复制
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大小的数组,并尝试使用循环来分配数组中的字符。但由于某些原因,它不起作用。

我知道我必须使用循环,所以我在纸上画出了模式,写下了数组的位置,并尝试使用循环赋值。

EN

回答 3

Stack Overflow用户

发布于 2017-11-16 22:33:31

你的程序中有很多错误。让我列出一些:

  • j = 00赋值给jj == 0比较j是否等于0.
  • You没有更新内部while循环中的ij
  • 如果您想坚持图片中显示的确切模式,则必须从printf("%c \n", t1[i][j]);.
  • printf("%c "), t1[i][j];中消除<代码>D15不打印t1[i][j]。但printf("%c ", t1[i][j]);可以。
  • 您没有在数组中的任何位置分配@字符。

让我们重写你的代码:

初始化数组:

代码语言:javascript
复制
char t1[11][11];

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

代码语言:javascript
复制
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] = ' ';
        }
    }
}

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

代码语言:javascript
复制
for (int i = 0; i < 11; i++){
    for (int j = 0; j < 11; j++){
        printf("%c\t", t1[i][j]);
    }
    printf("\n");
}

请参阅运行live here的代码。

票数 3
EN

Stack Overflow用户

发布于 2017-11-16 22:33:18

当您使用

代码语言:javascript
复制
printf("%c \n", t1[i][j]);

您正在从数组中编写一个字符,并在其后面添加一个换行符,这将打破您的模式。

尝试为这个更改打印循环

代码语言:javascript
复制
 for (j = 0; j <= 10; ++j)  
 {  
   printf("%s\n" ,t1[j]);  
 }

至于写'@‘字符,我会做一个这样的循环

代码语言:javascript
复制
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] = '@';
  }
}
票数 1
EN

Stack Overflow用户

发布于 2017-11-16 23:28:24

使用更大的2D数组,您可以使代码更短:

代码语言:javascript
复制
#define VSIZE 13
#define HSIZE 25

char t1[VSIZE][HSIZE];

for(int i = 0; i < VSIZE ; ++i) {
    for(int j = 1; j < HSIZE - 2; ++j) {
        // default value for odd columns:
        char c = ' ';
        // but change it to something else in an even column:
        if(!(j%2)) {
            if(i == 0 || i == VSIZE - 1)
                c = '-';
            if(i == 1 || i == VSIZE - 2 || 2 * i == j)
                c = '@';
        }
        t1[i][j] = c;
    }
    t1[i][0] = t1[i][HSIZE - 2] = '|';
    t1[i][HSIZE - 1] = '\0';   // turn the row to a string closing it with '\0'

    printf("%s\n", t1[i]);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47330977

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档