简体   繁体   English

如何反转 C 中多维数组的特定行?

[英]How do I reverse a specific row of a multidimensional array in C?

I have a multidimensional array with 3 rows and 4 columns.我有一个包含 3 行和 4 列的多维数组。 The program should use reverseRow() function to reverse a specific row from an array.该程序应使用reverseRow() function 来反转数组中的特定行。 Like, let's say user's input is 2, then it should reverse second row and print it.比如,假设用户的输入是 2,那么它应该反转第二行并打印它。

I have tried a swap method, but it didn't work.我试过交换方法,但没有用。 I also tried using pointers, but it didn't work as well.我也尝试过使用指针,但效果不佳。 Can someone explain it for me?有人可以为我解释一下吗? How do I reverse a specific row?如何反转特定行?

#include <stdlib.h>

int arr[3][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}};
int n = sizeof(arr) / sizeof(arr[0]);

void reverseRow(int low, int high)
{
    if (low < high)
    {
        int temp = arr[low][0];
        arr[low][0] = arr[high][0];
        arr[high][0] = temp;

        reverseRow(low + 1, high - 1);

        for (int i = 0; i < n; i++)
            for (int j = 3; i > 0; j--)
                printf("%d ", arr[i][j]);
    }
}

void printMenu()
{
    printf("\n");
    printf("You can choose one of these services: \n");
    printf("1. Get the elements of a specific row reversed \n");
    printf("Please select one to try ");
    int answer;
    scanf("%d", &answer);

    switch (answer)
    {

    case 1:
        reverseRow(0, n - 1);
        break;

    case 2:
        printf("Bye!\n");
        break;

    default:
        printf("please select carefully! \n");
        break;
    }
}

int main()
{
    printMenu();

    return 0;
}

Best regards.最好的祝福。

You're reversing the first column, not a user-selected row.您正在反转第一列,而不是用户选择的行。

You're not passing the row number to the function.您没有将行号传递给 function。

The loop that prints the array is printing all the columns in reverse order, and it's using n as the number of rows, not columns.打印数组的循环以相反的顺序打印所有列,它使用n作为行数,而不是列数。 I've renamed the variables to be clearer and fixed the printing loop.我已将变量重命名为更清晰并修复了打印循环。

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

int arr[3][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}};
int rows = sizeof(arr) / sizeof(arr[0]);
int cols = sizeof(arr[0]) / sizeof(arr[0][0]);

void reverseRow(int rownum, int low, int high)
{
    if (low < high)
    {
        int temp = arr[rownum][low];
        arr[rownum][low] = arr[rownum][high];
        arr[rownum][high] = temp;

        reverseRow(rownum, low + 1, high - 1);
    } else {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                printf("%d ", arr[i][j]);
            }
            printf("\n");
        }
    }
}

void printMenu()
{
    printf("\n");
    printf("You can choose one of these services: \n");
    printf("1. Get the elements of a specific row reversed \n");
    printf("Please select one to try ");
    int answer;
    scanf("%d", &answer);

    switch (answer)
    {

    case 1:
        printf("Please select row number: ");
        int row;
        scanf("%d", &row);
        if (row >= rows || row < 0) {
            printf("Invalid row\n");
            break;
        }
        reverseRow(row, 0, cols - 1);
        break;

    case 2:
        printf("Bye!\n");
        break;

    default:
        printf("please select carefully! \n");
        break;
    }
}

int main()
{

    printMenu();

    return 0;
}

What is the row of a two-dimensional array?二维数组的行是什么?

It is a one dimensional array.它是一个一维数组。

So what you need is to write a function that reverses a one-dimensional array.所以你需要的是写一个 function 来反转一个一维数组。

An iterative function can look the following way迭代 function 可以看成下面的样子

void reverseRow( int *a, size_t n )
{
    for ( size_t i = 0; i < n / 2; i++ )
    {
        int tmp = a[i];
        a[i] = a[n-i-1];
        a[n-i-1] = tmp;
    }
} 

A recursive function can look the following way递归 function 可以看下面的方式

void reverseRow( int *a, size_t n )
{
    if ( !( n < 2 ) )
    {
        int tmp = a[0];
        a[0] = a[n-1];
        a[n-1] = tmp;

        reverseRow( a + 1, n - 2 );
    }
}

And either function is called like for example例如,调用 function

reverseRow( arr[1], 4 );

that reverses the second row of the original array.反转原始数组的第二行。

Or if the number of row (starting from 0) is stored in some variable as for example row then the function is called like或者,如果行数(从 0 开始)存储在某个变量中,例如行,则调用 function

reverseRow( arr[row], 4 );

To output the reversed row you need to write a separate function.到output的反行你需要单独写一个function。

Here is a demonstration program.这是一个演示程序。

#include <stdio.h>

void reverseRowIterative( int *a, size_t n )
{
    for (size_t i = 0; i < n / 2; i++)
    {
        int tmp = a[i];
        a[i] = a[n - i - 1];
        a[n - i - 1] = tmp;
    }
}

void reverseRowRecursive( int *a, size_t n )
{
    if (!( n < 2 ))
    {
        int tmp = a[0];
        a[0] = a[n - 1];
        a[n - 1] = tmp;

        reverseRowRecursive( a + 1, n - 2 );
    }
}

void printRow( const int *a, size_t n )
{
    for (size_t i = 0; i < n; i++)
    {
        printf( "%2d ", a[i] );
    }
    putchar( '\n' );
}

int main( void )
{
    enum { M = 3, N = 4 };

    int arr[M][N] = 
    {
        { 1,  2,  3,  4 },
        { 5,  6,  7,  8 },
        { 9, 10, 11, 12 }
    };

    for ( size_t i = 0; i < M; i++ )
    {
        reverseRowIterative( arr[i], N );
        printRow( arr[i], N );
    }

    putchar( '\n' );

    for (size_t i = 0; i < M; i++)
    {
        reverseRowRecursive( arr[i], N );
        printRow( arr[i], N );
    }

    putchar( '\n' );
}

The program output is程序 output 是

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM