-3

I know how to transpose a matrix, But how to transpose a matrix with odd row Reversed form.

Example 3*3 Matrix

  • 1 2 3
  • 4 5 6
  • 7 8 9

Output

  • 1 6 7
  • 2 5 8
  • 3 4 9
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
AjithAj
  • 77
  • 1
  • 1
  • 10
  • 1
    Write a function to reverse rows, and apply it before you apply a function to transpose. – ad absurdum Jun 21 '17 at 01:25
  • Problem is, I want reverse only the odd rows of the Matrix – AjithAj Jun 21 '17 at 01:27
  • I meant, write a function to reverse _a_ row, and apply to whichever rows you choose; then transpose. The point is to break your problem down into smaller subproblems. – ad absurdum Jun 21 '17 at 01:29
  • Can u give me hint how to reverse a specific row @DavidBowling – AjithAj Jun 21 '17 at 01:31
  • 1
    `for (size_t i = 0; i < nrows; i++) { if (i % 2) { reverse_array(arr[i]); } }`. To reverse an array in place, you could swap the first and last elements, then swap the second and next-to-last elements, and so on until the middle of the array is reached. – ad absurdum Jun 21 '17 at 01:35
  • 1
    Thank you, will try @DavidBowling – AjithAj Jun 21 '17 at 01:37
  • That's an excellent use case scenario for `for` loops. – Michaël Roy Jun 21 '17 at 01:42
  • Sorry I can't do that... Can anyone code that pls.. and post it – AjithAj Jun 21 '17 at 01:59
  • @AjithAj David Bowling already gave you the answer. You said you know how to transpose a matrix. Ok. Then do what David said and then transpose the matrix. It yields the desired output. Reversing an array should be easier than transposing a matrix ... – jschultz410 Jun 21 '17 at 02:07
  • 1
    @DavidBowling change `i++` to `i += 2` would avoid the need for the modulo – phuclv Jun 21 '17 at 03:25
  • @AjithAj if the code is already provided and you still don't know how to use then you should [read a C book first](https://stackoverflow.com/q/562303/995714). You must show what you've tried first, otherwise the question is off-topic – phuclv Jun 21 '17 at 03:26
  • @LưuVĩnhPhúc-- sure, that's a good suggestion (about the `i += 2` _and_ the book :) – ad absurdum Jun 21 '17 at 03:26
  • One doubt how to get Transpose matrix in same matrix – AjithAj Jun 21 '17 at 04:27

2 Answers2

1

Here is a solution that breaks the problem into smaller subproblems. The function reverse_row() is a function that reverses an array in place, and the function transpose_array() transposes an array in place.

The first function, reverse_row() is called in a loop on the rows of the 2d array with odd indices, then the transpose_array() function is called on the resulting array. Note that the test if (i % 2) {} is used to determine if an array index is odd or even, since i % 2 evaluates to 0 only when i is even; you could also avoid this test and simply increment i by two at each iteration (starting from 1), as suggested by @Lưu Vĩnh Phúc in the comments:

for (size_t i = 1; i < MATRIX_SZ; i += 2) {
    reverse_row(MATRIX_SZ, array[i]);
    }

Also note that to transpose a square matrix in place, you do not need to iterate over all of the elements, but only the elements above or below the diagonal, swapping with the appropriate element. In this case, I have chosen to iterate over the elements below the diagonal, swapping with the corresponding element above the diagonal. Of course, the elements on the diagonal remain unchanged so are not iterated over at all.

Here is the code, using a 4X4 array as input. This code works for square matrices, and could be adapted to work for rectangular matrices. This would require care in choosing the size of the array used to represent the matrix, or dynamic allocation.

#include <stdio.h>

#define MATRIX_SZ  4

void print_array(size_t n, int arr[n][n]);
void reverse_row(size_t n, int arr[n]);
void transpose_array(size_t n, int arr[n][n]);

int main(void)
{
    int array[MATRIX_SZ][MATRIX_SZ] = { { 1, 2, 3, 4 },
                                        { 5, 6, 7, 8 },
                                        { 9, 10, 11, 12 },
                                        { 13, 14, 15, 16 } };

    puts("Before transformation:");
    print_array(MATRIX_SZ, array);
    putchar('\n');

    for (size_t i = 0; i < MATRIX_SZ; i++) {
        if (i % 2) {
            reverse_row(MATRIX_SZ, array[i]);
        }
    }
    transpose_array(MATRIX_SZ, array);

    puts("After transformation:");
    print_array(MATRIX_SZ, array);
    putchar('\n');

    return 0;
}

void print_array(size_t n, int arr[n][n])
{
    for (size_t i = 0; i < n; i++) {
        for (size_t j = 0; j < n; j++) {
            printf("%5d", arr[i][j]);
        }
        putchar('\n');
    }
}

void reverse_row(size_t n, int arr[n])
{
    size_t mid = n / 2;
    for (size_t i = 0; i < mid; i++) {
        size_t swap_dx = n - 1 - i;
        int temp = arr[i];
        arr[i] = arr[swap_dx];
        arr[swap_dx] = temp;
    }
}

void transpose_array(size_t n, int arr[n][n])
{
    for (size_t i = 0; i < n; i++) {
        for (size_t j = 0; j < i; j++) {
            int temp = arr[i][j];
            arr[i][j] = arr[j][i];
            arr[j][i] = temp;
        }
    }
}

And here is the program output:

Before transformation:
    1    2    3    4
    5    6    7    8
    9   10   11   12
   13   14   15   16

After transformation:
    1    8    9   16
    2    7   10   15
    3    6   11   14
    4    5   12   13
ad absurdum
  • 19,498
  • 5
  • 37
  • 60
0

If I have understood the assignment correctly then the program can look the following way.

#include <stdio.h>

#define MAX_VALUE   100

int main(void) 
{
    while ( 1 )
    {
        printf( "Enter the size of an array (0 - Exit): " );

        size_t n;

        if ( scanf( "%zu", &n ) != 1 || n == 0 ) break;

        putchar('\n');

        n %= MAX_VALUE;

        int a[n][n];

        for ( size_t i = 0; i < n; i++ )
        {
            for ( size_t j = 0; j < n; j++ )
            {
                a[i][j] = n * i + j;
            }
        }

        for (size_t i = 0; i < n; i++)
        {
            for (size_t j = 0; j < n; j++)
            {
                printf("%3d ", a[i][j]);
            }

            putchar('\n');
        }

        putchar('\n');

        for (size_t i = 0; i < n; i++)
        {
            for (size_t j = i; j < n; j++)
            {
                if (j % 2 == 1 && i < n / 2)
                {
                    int tmp = a[j][i];
                    a[j][i] = a[j][n - i - 1];
                    a[j][n - i - 1] = tmp;
                }
                if (i != j)
                {
                    int tmp = a[i][j];
                    a[i][j] = a[j][i];
                    a[j][i] = tmp;
                }
            }
        }

        for (size_t i = 0; i < n; i++)
        {
            for (size_t j = 0; j < n; j++)
            {
                printf("%3d ", a[i][j]);
            }

            putchar('\n');
        }

        putchar('\n');
    }

    return 0;
}

Its output might look like

Enter the size of an array (0 - Exit): 10

  0  1  2  3  4  5  6  7  8  9 
 10 11 12 13 14 15 16 17 18 19 
 20 21 22 23 24 25 26 27 28 29 
 30 31 32 33 34 35 36 37 38 39  
 40 41 42 43 44 45 46 47 48 49 
 50 51 52 53 54 55 56 57 58 59 
 60 61 62 63 64 65 66 67 68 69 
 70 71 72 73 74 75 76 77 78 79 
 80 81 82 83 84 85 86 87 88 89 
 90 91 92 93 94 95 96 97 98 99 

  0 19 20 39 40 59 60 79 80 99 
  1 18 21 38 41 58 61 78 81 98 
  2 12 22 37 42 57 62 77 82 97
  3 13 23 36 43 56 63 76 83 96
  4 14 24 34 44 55 64 75 84 95
  5 15 25 35 45 54 65 74 85 94
  6 16 26 33 46 53 66 73 86 93
  7 17 27 32 47 52 67 72 87 92
  8 11 28 31 48 51 68 71 88 91
  9 10 29 30 49 50 69 70 89 90 

Enter the size of an array (0 - Exit): 3

  0 1 2
  3 4 5
  6 7 8

  0 5 6
  1 4 7
  2 3 8 

Enter the size of an array (0 - Exit): 0

The compiler should support Variable Length Arrays. Otherwise you can rewrite the program for an array with a fixed size.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335