gpt4 book ai didi

c - 如何更改二维数组中的位置

转载 作者:行者123 更新时间:2023-11-30 16:34:13 25 4
gpt4 key购买 nike

例如,假设有宽度和高度,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");
}

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

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

最佳答案

只需声明第二个数组,其中指定的行数等于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;
}

关于c - 如何更改二维数组中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49383851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com