【问题标题】:how to change positions in 2D array如何更改二维数组中的位置
【发布时间】:2018-08-29 05:54:08
【问题描述】:

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

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");
}

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

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

【问题讨论】:

  • 您必须声明第二个数组并将值从第一个数组复制到第二个数组。

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


【解决方案1】:

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

 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

【讨论】:

    【解决方案2】:

    只需声明第二个数组,指定行数等于width,列数等于height,然后将源数组中的值复制到第二个数组。

    例如

    #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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-27
      • 2018-06-25
      相关资源
      最近更新 更多