读取具有整数动态大小的二维数组并垂直输出

时间:2019-11-18 09:02:09

标签: c arrays

我试图让用户在一个输入中键入所有整数值,直到EOF:

1 2 3 4 5
6 7 8 9 10
11 12 13 14 15

然后垂直输出它们:

1 6 11
2 7 12
3 8 13
4 9 14
5 10 15

我尝试了不同的方法,但始终无法正确读取输入。

int numberArray[][100] = {0};  
char tempChar;

while (scanf("%d%c", &numberArray[i][j], &tempChar) != EOF) {
    j++;
    if (tempChar != '\n') {
        i++;
        j = 0;
    }
}

for (int k = 0; k < i; k++) {
    int arraySize = sizeof(numberArray[k]) / sizeof(numberArray[k][0]);
    for (int f = 0; f < arraySize; f++) {
        printf("%d ", numberArray[k][f]);
    }
}

2 个答案:

答案 0 :(得分:1)

我创建了某样东西,它仅适用于每行中相同数量的列。我认为这就是您要实现的目标。另外,如果您想真正基于动态内存来执行此操作,则应该使用mallocrealloc来执行此操作,因为现在数组大小是预定义的(在我的情况下为5x5)

#include <stdio.h>
#include <string.h>


int main(void)
{
        int arr[5][5],j,i,columns,rows;
        char x,end[10]; //end takes input after enter so it can get next value thats why its string
        for (i = 0, j = 0;; i++, j = 1) {
            if (j != 0) arr[i][0] = atoi(end);
            do {
                scanf("%d%c", &arr[i][j], &x);
                j++;

            } while (x != '\n');
            scanf("%s", end); //if u want to end input use x I could do it to next enter but I run into some dificulties and I got no time.
            if (strcmp("x",end)==0) {
                i++;
                rows = j;
                break;
            }
        }

        columns = i;
        for (j = 0; j < rows; j++) {
            for (i = 0; i < columns; i++) {
                printf("%d ", arr[i][j]);
            }
            printf("\n");
        }
    return 0;
}

答案 1 :(得分:1)

您可以这样进行:逐行输入,然后使用strtok提取由空格分隔的数字。实时查看代码here

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

void input_matrix(int matrix[][100], int *rows, int *cols)
{
    char *buffer;
    size_t buffer_size = 200;

    int row = 0;
    int col = 0;
    int ret;

    buffer = (char *)malloc(sizeof(char) * buffer_size);

    while((ret = getline(&buffer, &buffer_size, stdin)) > 0) {
            char *split = NULL;
            //printf("ret: %d\n", ret);
            split = strtok(buffer, " ");

            col = 0;

            while (split) {
                matrix[row][col] = strtol(split, NULL, 10);
                col++;

                split = strtok(NULL, " ");
            }

            row++;
    }
    free(buffer);

    *rows = row;
    *cols = col;
}

void print_matrix(int matrix[][100], int rows, int cols)
{
    printf("Printing matrix: \n");

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

void print_transpose(int matrix[][100], int rows, int cols)
{
    printf("Printing transpose: \n");

    for (int j = 0; j < cols; j++) {
        for (int i = 0; i < rows; i++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() 
{
    int m_input[100][100]; 

    int rows;
    int cols;

    input_matrix(m_input, &rows, &cols);

    printf("Input row: %d, col: %d\n", rows, cols);


    print_matrix(m_input, rows, cols);


    print_transpose(m_input, rows, cols); 

    return 0; 
}