顺利打印二维数组

Printing 2D Array smoothly

我有一个二维整数数组 twoDArr[8][8],如下所示:

代码:

int ci, cj;
    for (ci = 0; ci < 8; ci++)
    {
        for (cj = 0; cj < 8; cj++)
        {
            printf("%d ", twoDArr[ci][cj]);
        }
        printf("\n");
    }

我正在尝试做的是以这种方式打印数组:

我遇到的问题是,我不知道该怎么做。 我一直在尝试大约 50 种不同的 for 循环,但仍然无法以那种特定的方式获得它。 是否有任何站点/生成器/任何人的代码可以帮助我实现这一目标或任何其他排列的二维数组?它看起来很容易,但它是如此令人沮丧,以至于我尝试了几天而没有结果。 提前致谢。

注:未测试

const char *line = "+----+----+----+----+----+----+----+----+----+";
const char *head = "|    |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |";

puts(line);
puts(head);
puts(line);
for(int k = 0; k < 8; ++k)
{
    printf("|  %c ", 'A'+k);
    for(int l = 0; l < 8; ++l)
    {
        printf("|  %d ", twoDArr[k][l]);
    }
    puts(" |");
    puts(line);
}

我能得到的最接近 OP 的描述:

#include <stdio.h>

int main() {
    int ARRAY[][3] = {
        {0,1,0,},
        {0,0,4,},
        {0,0,0,},
        {0,0,3,},
    };
    
    size_t size_row = 3;
    size_t size_col = 4;
    
    /* print header */
    for(int r = 0; r < size_row; ++r){
        printf("+----");
    }
    printf("+\n");
    for(int r = 0; r < size_row; ++r){
        printf("|%4d", r+1);
    }
    printf("|\n");
    
    /* print rows */
    for(int c = 0; c < size_col; ++c){
        for(int r = 0; r < size_row; ++r){
         printf("+----");
        }
        printf("+\n");
        for(int r = 0; r < size_row; ++r){
         if(0 == ARRAY[c][r])
          printf("|    ");
         else
          printf("|%4d", ARRAY[c][r]);
        }
        printf("|\n");
    }
    for(int r = 0; r < size_row; ++r){
     printf("+----");
    }
    printf("+\n");
    
    return 0; }

输出为:

+----+----+----+                                                                                                                
|   1|   2|   3|                                                                                                                
+----+----+----+                                                                                                                
|    |   1|    |                                                                                                                
+----+----+----+                                                                                                                
|    |    |   4|                                                                                                                
+----+----+----+                                                                                                                
|    |    |    |                                                                                                                
+----+----+----+                                                                                                                
|    |    |   3|                                                                                                                
+----+----+----+

如有疑问,请使其完全*可配置并过度设计...

#include <stdio.h>
#include <stdbool.h>

void printHorizontalLine(int repeat, int width, char lineChar, char edgeChar)
{
    for (int r = 0; r < repeat; ++r)
    {
        printf("%c", edgeChar);
        for (int w = 0; w < width; ++w)
        {
            printf("%c", lineChar);
        }
    }
    printf("%c\n", edgeChar);
}

void printHeader(int repeat, int width, char barChar, char fillChar, int skipAtBeginning)
{
    for (int header = 0; header < repeat; ++header)
    {
        printf("%c", barChar);
        if (header < skipAtBeginning)
        {
            printf("%*c", width, fillChar);
        }
        else
        {
            printf("%c%*d%c", fillChar, width - 2, header, fillChar);
        }
    }
    printf("%c\n", barChar);
}

void printLine(int repeat, int width, char barChar, char fillChar, char headerChar, int* data)
{
    for (int count = 0; count < repeat; ++count)
    {
        printf("%c", barChar);
        if (count == 0)
        {
            printf("%c%*c%c", fillChar, width - 2, headerChar, fillChar);
        }
        else
        {
            printf("%c%*d%c", fillChar, width - 2, data[count - 1], fillChar);
        }
    }
    printf("%c\n", barChar);
}

#define DIMENSION 8
#define BARCHAR '|'
#define EDGECHAR '+'
#define LINECHAR '-'
#define FILLCHAR ' '
#define CELLWIDTH 4

int main()
{
    int a[DIMENSION][DIMENSION];
    for (int i = 0; i < DIMENSION * DIMENSION; ++i)
    {
        a[i / DIMENSION][i % DIMENSION] = i;
    }

    printHorizontalLine(DIMENSION + 1, CELLWIDTH, LINECHAR, EDGECHAR);
    printHeader(DIMENSION + 1, CELLWIDTH, BARCHAR, FILLCHAR, 1);
    for (int y = 0; y < DIMENSION; ++y)
    {
        printHorizontalLine(DIMENSION + 1, CELLWIDTH, LINECHAR, EDGECHAR);
        printLine(DIMENSION + 1, CELLWIDTH, BARCHAR, FILLCHAR, (char)('A' + y), a[y]);
    }
    printHorizontalLine(DIMENSION + 1, CELLWIDTH, LINECHAR, EDGECHAR);

    return 0;
}

示例:

+----+----+----+----+----+----+----+----+----+
|    |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |
+----+----+----+----+----+----+----+----+----+
|  A |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |
+----+----+----+----+----+----+----+----+----+
|  B |  8 |  9 | 10 | 11 | 12 | 13 | 14 | 15 |
+----+----+----+----+----+----+----+----+----+
|  C | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 |
+----+----+----+----+----+----+----+----+----+
|  D | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 |
+----+----+----+----+----+----+----+----+----+
|  E | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
+----+----+----+----+----+----+----+----+----+
|  F | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 |
+----+----+----+----+----+----+----+----+----+
|  G | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 |
+----+----+----+----+----+----+----+----+----+
|  H | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 |
+----+----+----+----+----+----+----+----+----+
#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#
/     ./.   1./.   2./.   3./.   4./.   5./.   6./
#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#
/.   A./.   0./.   1./.   2./.   3./.   4./.   5./
#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#
/.   B./.   6./.   7./.   8./.   9./.  10./.  11./
#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#
/.   C./.  12./.  13./.  14./.  15./.  16./.  17./
#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#
/.   D./.  18./.  19./.  20./.  21./.  22./.  23./
#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#
/.   E./.  24./.  25./.  26./.  27./.  28./.  29./
#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#
/.   F./.  30./.  31./.  32./.  33./.  34./.  35./
#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#~~~~~~#

*是的,我没有为列字符定义,但它只使用一次,所以很容易改变。