如何打印“整洁”的C中的2D数组 [英] How to print a "neat" 2D array in C

查看:77
本文介绍了如何打印“整洁”的C中的2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够将2D阵列打印到输出文件中,但是它不是统一的。我希望2D阵列在打印出来时均匀分布。我对使用C进行编程非常陌生,因此将不胜感激!

I am able to print out a 2D array into an output file but it is not uniform. I would like to have the 2D array evenly spaced when it is printed out. I am fairly new at programing in C so any help would be greatly appreciated!

我的代码:

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

/**
 * Driver method for program
 */

int main()
{
    int nums[200];
    int i = 0;
    int j = 0;
    FILE * in_file;
    FILE * out_file;
    srand(time(0)); //seed for random generator

    /**
     * if loop reads text file and stores numbers into array
     */
    if (in_file = fopen("Data.txt", "r")) {
        while (fscanf(in_file, "%d", &nums[i]) != EOF) {
            i++;
        }

        int numbsinfile = i;
        int random = randomNumber(2, 12);

        int matrix1[5][random];
        int matrix2[random][5];
        out_file = fopen("out.txt", "w");

        fprintf(out_file, "Matrix 1: \n");
        fprintf(out_file, "Rows = 5 \n");
        fprintf(out_file, "Columns = %d \n\n", random);

        for(i = 0; i < 5; i++ ){
            for(j = 0; j < random; j++){
                int rand = randomNumber(0, numbsinfile);
                matrix1[i][j] = nums[rand];
                fprintf(out_file, "%d \t\t", matrix1[i][j]);
            }
            fprintf(out_file, "\n");
        }
        fclose(in_file);
        fclose(out_file);
    }

    return 0;

}

/**
 * Generates and prints random
 * numbers in range [lower, upper].
 */

int randomNumber(int lower, int upper) {

    int num = (rand() % (upper - lower + 1)) + lower;
    return num;

}

我正在使用的输入文件以及我的代码产生了。我基本上只是想清理打印到输出文件的2D数组。

The input file I am using along with the output file that my code produces. I am basically just wanting to clean up the 2D array that is printed to the output file.

输入文件:

23 34 -54 21 45 34 65 -54 21 45 34 65 -34 24 58
       49 45 10     -57 20
57 39 20    58 23 10 20 58 -60 76   -82 28
    28 -37 49 358 47 -50 37 29
57 -29 -20 47 69
    93 57   23 49 -38 49        27 -40 48 39
56 -30 47 28 49
37 49 
        27 26 10 20 58 -60 26 10 20 58 -60 76   -82 28
    28 -37 49 -28 93 28
73 47     27 83     37 -29 40 37 49 20
17 -26 12    17 17
18 38 29 39 -118
19 10 20 58 -60 76   -82 28
    28 -37 49 59 10 58 -60 76   -82 28
    28 -37 49 59 10 20 58 -60 76   -82 28
    28 -37 49 30 -58 58     38 49 30 -58 58     38 
49 30 -58 58    38 
28 39
39 48     23 -50 28
48 29 39 40 29

我的输出文件:

Matrix 1: 
Rows = 5 
Columns = 12 

28      39      20      49      58      76      37      -26         47      -40         216309856       26      
57      -50         30      47      29      58      73      20      26      216309856       49      26      
216309856       30      59      45      20      23      -50         83      -50         -37         28      30      
10      10      23      28      47      45      34      10      19      -38         -118        28      
47      49      -40         20      49      29      10      20      58      69      10      28      


推荐答案


如何在C(?)中打印整洁的2D数组

How to print a "neat" 2D array in C (?)

使用 snprintf(NULL,0,some_format,...

    int width = 1;
    for(i = 0; i < 5; i++ ) {
        for(j = 0; j < random; j++) {
            int rand = randomNumber(0, numbsinfile);
            matrix1[i][j] = nums[rand];
            int w = snprintf(NULL, 0, "%d", matrix1[i][j]);
            if (w > width) width = w;
        }
    }

使用 * 在说明符中,宽度用于打印。

Use * in the specifier and width for printing.

    for(i = 0; i < 5; i++ ) {
        for(j = 0; j < random; j++) {
            fprintf(out_file, " %*d", width, matrix1[i][j]);
        }
        fprintf(out_file, "\n");
    }

这篇关于如何打印“整洁”的C中的2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆