0

I have a multidimensional array with 3 rows and 4 columns. The program should use reverseRow()function to reverse a specific row from an array. Like, let's say user's input is 2, then it should reverse second row and print it.

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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Please, use your code editors "indent code", "format document", … functionality to indent your code – that makes it both easier to read for us (significantly so! This is a bit of a mess!), it also helps you see bugs yourself. Professional programmers use that functionality *all the time*, and will reject code that was not machine-formatted, because they don't trust themselves to see bugs without that. And for beginners, it's twice as important! – Marcus Müller Mar 22 '22 at 20:54
  • radicalEd, Are you trying to reverse a row or the first column? – chux - Reinstate Monica Mar 22 '22 at 20:57
  • To me, it looks like you have an array with *3* rows and *4* columns, and a function that reverses the first *column*, but then displays the contents of each row one after another on one line. – Dmitri Mar 22 '22 at 20:59
  • okay, I will edit the code. Thank you for the helpful information. –  Mar 22 '22 at 20:59
  • @chux-ReinstateMonica I am trying to reverse a specific row. LIke, if user's input is 2, then second row should be reversed. –  Mar 22 '22 at 21:00
  • Also see https://stackoverflow.com/questions/359732/why-is-it-considered-a-bad-practice-to-omit-curly-braces?lq=1 – Barmar Mar 22 '22 at 21:02
  • 3
    Take a smaller bite. Can you write a function that, given an array of specified length, reverses that one array ? Seems you can (sort of).. So, then realize all you're asking to do is the same, just for a specific array in an array of arrays. – WhozCraig Mar 22 '22 at 21:03
  • Why does the function take 2 arguments? Is it supposed to reverse all the rows in a range? – Barmar Mar 22 '22 at 21:04
  • I realized the question was unclear, so I edited it. –  Mar 22 '22 at 21:05
  • @radicalEd And where is there the function that reverses a row? I do not see such a function. – Vlad from Moscow Mar 22 '22 at 21:08
  • You're swapping the first element of two different rows. You're not reversing anything. – Barmar Mar 22 '22 at 21:12
  • @VladfromMoscow to be honest Im very confused. The "reverseRow" function is suppose to reverse it, but as I said it doesn't. –  Mar 22 '22 at 21:13
  • @Barmar oh, thank you for pointing that out. I will try changing the code. –  Mar 22 '22 at 21:13
  • Where do you ask the user for the row number to reverse? What are `low` and `high` supposed to be? – Barmar Mar 22 '22 at 21:13
  • The menu says that option 4 is for reversing, but you call the function with option 1. – Barmar Mar 22 '22 at 21:14
  • @Barmar I changed menu so I can reduce the code size in the question. –  Mar 22 '22 at 21:16
  • You still never ask the user for the row number to reverse. Why is it always `0` and `n-1`? – Barmar Mar 22 '22 at 21:17
  • some parts of code that do not affect this 'reversing' were removed before I uploaded the question. –  Mar 22 '22 at 21:18
  • for '0' and 'n-1' it is just one kind of solution I saw. But it doesn't work, so Im asking for help here –  Mar 22 '22 at 21:19
  • Are `high` and `low` supposed to be column numbers to swap? You also need the row number as a parameter. – Barmar Mar 22 '22 at 21:19
  • @Barmar hey. Thank you for your solution, but it crashed. I think it's because it is infinite loop. –  Mar 22 '22 at 21:51

2 Answers2

1

You're reversing the first column, not a user-selected row.

You're not passing the row number to the 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. 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;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Your solution did reversed specific row, which is great, but then it reversed row before itself and ended up with Segmentation fault (core dumped). –  Mar 22 '22 at 21:31
  • The problem was in your code that prints the array, it has the wrong limits in the indexes. I've fixed that now. – Barmar Mar 22 '22 at 22:52
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.

An iterative function can look the following way

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

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

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

reverseRow( arr[row], 4 );

To output the reversed row you need to write a separate 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

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

 1  2  3  4
 5  6  7  8
 9 10 11 12 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335