-2

and if we want to reverse diagonal matrix, like:

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

and if we reverse the diagonals it will be like that:

5  2  3  4  1
6  9  8  7  10
11 12 13 14 15
16 19 18 17 20
25 22 23 24 21

or like this example:

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

and it will be like this:

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

but were not allowed to use functions and the matrix is always the a[N][N] kind..

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • 1
    Can you demonstrate *any* evidence of attempting to solve this yourself? – Scott Hunter May 31 '19 at 13:03
  • What have you tried so far in solving this homework problem? We're willing to give you tips or help push through specific issues, but we're not going to do your work for you. – metal May 31 '19 at 13:04

1 Answers1

0

This seems so do the trick - it's not pretty though. You can probably make it more generic to different sized arrays.

#include <stdio.h>
const int N = 5;

void print (int arr[][N]) {
    int i, j;
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            printf("%d ", arr[i][j]);
        }
        printf ("\n");
    }
    printf ("\n");
}

void flip (int arr[][N]) {
    int a[5][5] = {0};
    int x, y;
    for (y = 0; y < N; y++) {
        for (x = 0; x < N; x++) {
            if (x == y){
                a[y][x] = arr[y][N-y-1];
            } else if (x == N-(y+1)) {
                a[y][x] = arr[y][y];
            } else {
                a[y][x] = arr[y][x];
            }
        }
    }
    print(a);
}

int main() {
  int a[5][5] = {
    {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}
  };
  flip(a);
  return 0;
}

Outputs:

5 2 3 4 1 
6 9 8 7 10 
11 12 13 14 15 
16 19 18 17 20 
25 22 23 24 21 
Jeppe
  • 1,830
  • 3
  • 24
  • 33