如何打印像网格一样的数组?

时间:2017-12-09 21:03:35

标签: c arrays counter

您好我是编程新手,我需要像网格一样打印一个数组(必须是1维)。我现在试了好几个小时,但仍然没有得到我想要的结果。 该数组有16个值,因此网格将是这样的:

unlist(str_split(df$x, "\\•"))

我的代码:

 ___________
|0 |1 |2 |3 |
|4 |5 |6 |7 |
|8 |9 |10|11|
|12|13|14|15|
–––––––––––––

显示:

#include <stdio.h>

void display_grid(void)
{
  int i;
  int j;
  int cell[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

  for (i = 0; i < 4; i++)
    {
      printf("|");
      for (j = 0; j < 16; j++)
      {
        printf("%d|",cell[j]);
      }
        printf("\n");
    }
}

int main()
{
  display_grid();
  return 0;
}

2 个答案:

答案 0 :(得分:1)

我觉得这样可行:我将cell[j]更改为cell[4 * i + j]

#include <stdio.h>

void display_grid(void)
{
  int i;
  int j;
  int cell[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

  for (i = 0; i < 4; i++)
    {
      printf("|");
      for (j = 0; j < 4; j++)
      {
        printf("%d|",cell[4 * i + j]);
      }
        printf("\n");
    }
}

int main()
{
  display_grid();
  return 0;
}

这有帮助吗?

答案 1 :(得分:0)

您可以使用余数运算符%来确定何时在新行上打印。这是一个带有display_grid()函数的示例程序,它将int数组作为参数,数组的大小和每行的所需大小。如果数组可以均匀地分成所需列数的行,则返回零。否则返回非零值。这允许您以各种网格格式显示1d数组;如果阵列无法均匀分割,则不会打印任何内容。

目前,此打印网格包含最多2位数字(或1位数字和符号)。修改它以打印更多数字或更多涉及修改以便显示宽度适合输入所需的最大宽度将很简单。

#include <stdio.h>

#define ARR_SZ  16

int display_grid(int *arr, size_t arr_sz, size_t row_sz);

int main(void)
{
    int array[ARR_SZ];
    for (size_t i = 0; i < ARR_SZ; i++) {
        array[i] = i;
    }

    size_t row_sz = 4;
    if (display_grid(array, ARR_SZ, row_sz) != 0) {
        fprintf(stderr, "Array size not evenly divisible by %zu\n", row_sz);
    }

    row_sz = 8;
    if (display_grid(array, ARR_SZ, row_sz) != 0) {
        fprintf(stderr, "Array size not evenly divisible by %zu\n", row_sz);
    }

    row_sz = 2;
    if (display_grid(array, ARR_SZ, row_sz) != 0) {
        fprintf(stderr, "Array size not evenly divisible by %zu\n", row_sz);
    }

    row_sz = 1;
    if (display_grid(array, ARR_SZ, row_sz) != 0) {
        fprintf(stderr, "Array size not evenly divisible by %zu\n", row_sz);
    }

    row_sz = 16;
    if (display_grid(array, ARR_SZ, row_sz) != 0) {
        fprintf(stderr, "Array size not evenly divisible by %zu\n", row_sz);
    }

    row_sz = 6;
    if (display_grid(array, ARR_SZ, row_sz) != 0) {
        fprintf(stderr, "Array size not evenly divisible by %zu\n", row_sz);
    }

    row_sz = 32;
    if (display_grid(array, ARR_SZ, row_sz) != 0) {
        fprintf(stderr, "Array size not evenly divisible by %zu\n", row_sz);
    }

    return 0;
}

int display_grid(int *arr, size_t arr_sz, size_t row_sz)
{
    int ret_val = arr_sz % row_sz;

    if (ret_val == 0) {
        /* Print top border */
        putchar(' ');
        for (size_t i = 1; i < 3 * row_sz; i++) {
            putchar('_');
        }
        putchar('\n');

        /* Print rows of grid */
        for (size_t i = 0; i < arr_sz; i++) {
            printf("|%-2d", arr[i]);
            if ((i + 1) % row_sz == 0) {
                printf("|\n");
            }
        }

        /* Print bottom border */
        for (size_t i = 0; i < 3 * row_sz + 1; i++) {
            putchar('-');
        }
        putchar('\n');
    }

    return ret_val;
}

节目输出:

 ___________
|0 |1 |2 |3 |
|4 |5 |6 |7 |
|8 |9 |10|11|
|12|13|14|15|
-------------
 _______________________
|0 |1 |2 |3 |4 |5 |6 |7 |
|8 |9 |10|11|12|13|14|15|
-------------------------
 _____
|0 |1 |
|2 |3 |
|4 |5 |
|6 |7 |
|8 |9 |
|10|11|
|12|13|
|14|15|
-------
 __
|0 |
|1 |
|2 |
|3 |
|4 |
|5 |
|6 |
|7 |
|8 |
|9 |
|10|
|11|
|12|
|13|
|14|
|15|
----
 _______________________________________________
|0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |10|11|12|13|14|15|
-------------------------------------------------
Array size not evenly divisible by 6
Array size not evenly divisible by 32