如何在2D数组中改变位置

时间:2018-03-20 12:09:42

标签: c loops for-loop matrix multidimensional-array

例如,假设有宽度和高度,6和4分别来自用户输入,并且存储在2D阵列中的输入(也来自用户输入)是:

0 1 2 2 1 0
1 0 0 0 0 1
1 0 0 0 0 1
0 1 1 1 1 0

有没有办法翻转x轴和y轴? 我想要做的是改变

0 1 2 2 1 0
1 0 0 0 0 1
1 0 0 0 0 1
0 1 1 1 1 0

0 1 1 0
1 0 0 1
2 0 0 1
2 0 0 1
1 0 0 1
0 1 1 0

以下代码,

scanf("%d %d", &width, &height);
int board[height][width];

for(i = 0; i < height; i++)
{
    for(j = 0; j < width; j++)
    {
        scanf("%d", &input);
        board[i][j] = input;
    }
}

并通过

for(i = 0; i < width; i++)
{

    for(j = 0; j < height; j++)
    {
        printf("%d", board[j][i]);
    }
    printf("\n");
}

,这打印出我期望的输出,但它实际上并没有改变它的位置......我不能改变第一个编码部分,因为我已经用它来做其他的工作。无论如何通过添加其他方法或新板来解决问题?

有人可以帮帮我吗?如果有人帮助我,我将非常感激! 感谢

2 个答案:

答案 0 :(得分:0)

只需声明第二个数组,其指定的行数等于width,列数等于height,并将值从souirce数组复制到第二个数组。

例如

#include <stdio.h>

int main(void) 
{
    size_t height, width;

    printf( "Enter the height and the width of the array: " );

    scanf( "%zu %zu", &height, &width );

    int a[height][width];

    puts( "Enter values for elements of the array" );

    for ( size_t i = 0; i < height; i++ )
    {
        printf( "%zu row: ", i + 1 );
        for ( size_t j = 0; j < width; j++ ) scanf( "%d", &a[i][j] );
    }

    int b[width][height];

    for ( size_t i = 0; i < height; i++ )
    {
        for ( size_t j = 0; j < width; j++ ) b[j][i] = a[i][j];
    }

    puts( "\nSource array is" );

    for ( size_t i = 0; i < height; i++ )
    {
        for ( size_t j = 0; j < width; j++ ) printf( "%d ", a[i][j] );
        putchar( '\n' );
    }

    puts( "\nReversed array is" );

    for ( size_t i = 0; i < width; i++ )
    {
        for ( size_t j = 0; j < height; j++ ) printf( "%d ", b[i][j] );
        putchar( '\n' );
    }

    return 0;
}

程序输出可能看起来像

Enter the height and the width of the array: 4 6
Enter values for elements of the array
1 row: 0 1 2 2 1 0 
2 row: 1 0 0 0 0 1
3 row: 1 0 0 0 0 1 
4 row: 0 1 1 1 1 0 

Source array is
0 1 2 2 1 0 
1 0 0 0 0 1 
1 0 0 0 0 1 
0 1 1 1 1 0 

Reversed array is
0 1 1 0 
1 0 0 1 
2 0 0 1 
2 0 0 1 
1 0 0 1 
0 1 1 0 

另一种方法是动态分配第一个和辅助数组。 例如

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

int main(void) 
{
    size_t height, width;

    printf( "Enter the height and the width of the array: " );

    scanf( "%zu %zu", &height, &width );

    int **a = malloc( height * sizeof( int * ) );

    for ( size_t i = 0; i < height; i++ ) a[i] = malloc( width * sizeof( int ) );

    puts( "Enter values for elements of the array" );

    for ( size_t i = 0; i < height; i++ )
    {
        printf( "%zu row: ", i + 1 );
        for ( size_t j = 0; j < width; j++ ) scanf( "%d", &a[i][j] );
    }

    int **b = malloc( width * sizeof( int * ) );
    for ( size_t i = 0; i < width; i++ ) b[i] = malloc( height * sizeof( int ) );

    for ( size_t i = 0; i < height; i++ )
    {
        for ( size_t j = 0; j < width; j++ ) b[j][i] = a[i][j];
    }

    puts( "\nSource array is" );

    for ( size_t i = 0; i < height; i++ )
    {
        for ( size_t j = 0; j < width; j++ ) printf( "%d ", a[i][j] );
        putchar( '\n' );
    }

    for ( size_t i = 0; i < height; i++ ) free( a[i] );
    free( a );

    a = b;

    puts( "\nReversed array is" );

    for ( size_t i = 0; i < width; i++ )
    {
        for ( size_t j = 0; j < height; j++ ) printf( "%d ", a[i][j] );
        putchar( '\n' );
    }

    for ( size_t i = 0; i < width; i++ ) free( a[i] );
    free( a );

    return 0;
}

答案 1 :(得分:0)

这可能是最简单的方法。避免在输入过程中复制和存储元素。但是,如果您无法更改输入过程,则必须使用以下方法复制元素:

 void reverse_matrix(int c, int r, int board[][r], int board2[][c])

代码:

#include <stdio.h>

void print(int c, int r, int board[][r] )
{
    for(int i = 0; i < c; i++)
    {
        for(int j = 0; j < r; j++)
        {
            printf("%d ", board[i][j]);
        }
        printf("\n");
    }
}

void reverse_matrix(int c, int r, int board[][r], int board2[][c] )
{
    for(int i = 0; i < c; i++)
    {
        for(int j = 0; j < r; j++)
        {
            board2[j][i] = board[i][j];
        }
    }   
}

int main()
{
    int width, height, input;

    scanf("%d %d", &width, &height);
    int board[height][width];

    int board2[width][height];

    for(int i = 0; i < height; i++)
    {
        for(int j = 0; j < width; j++)
        {
            scanf("%d", &input);
            board[i][j]  = input;
            // board2[j][i] = input; // if you can add it 
        }
    }

    printf("First matrix:\n");
    print(height, width, board);

    reverse_matrix(height, width, board, board2); // alternative

    printf("Second matrix:\n");
    print(width, height, board2);    

    return 0;    
}

输出:

2                                                                                                                                               
3                                                                                                                                               
1                                                                                                                                               
2                                                                                                                                               
3                                                                                                                                               
4                                                                                                                                               
5                                                                                                                                               
6                                                                                                                                               
First matrix:                                                                                                                                   
1 2                                                                                                                                             
3 4                                                                                                                                             
5 6                                                                                                                                             
Second matrix:                                                                                                                                  
1 3 5                                                                                                                                           
2 4 6