Thread: How to reflect a square matrix diagonally?

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    17

    How to reflect a square matrix diagonally?

    I have progressed to a point but I don't know what have I done wrong. Can you help me please?
    Code:
    #include <stdio.h>#include <conio.h>
    int main ()
    {
        int i, j, n, temp;
        printf ("Give the dimension: ");
        scanf("%d", &n);
        int matrix [n][n];
        for (i=0;i<n;i++)
        {
            for (j=0;j<n;j++)
            {
                printf ("\nGive element [%d][%d]: ", i, j);
                scanf ("%d", &matrix [i][j]);
            }}
        
        for (i=0;i<n;i++)
        {
            for (j=0;j<n;j++)
            printf ("%4d", matrix [i][j]);
            printf ("\n\n");
            }
      
     for (i=0;i<n;i++)
        {
            for (j=0;j<i;j++)
            {
                temp=matrix[i][j];
                matrix[i][j]=matrix [n-1-j][n-1-i];
                matrix[n-1-j][n-1-i]=temp;
                }
                }
               printf ("\n\n\n\n");
               for (i=0;i<n;i++)
        {
            for (j=0;j<n;j++)
            printf ("%4d", matrix [i][j]);
            printf ("\n\n");
            }
            getch ();
            return 0;
            }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Specify what type of reflection you want, with an example, and show what your program does now.

    Save us time, and clear up possible misunderstandings.

    For example:
    Code:
    1  2  3  Original matrix
    4  5  6
    7  8  9
    
    to
    
    9  6  3   45° right upper diagonal 
    8  5  2   with no rotation
    7  4  1

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by keivi View Post
    How to reflect a square matrix diagonally?
    I'll assume you want to reflect the matrix by it's main diagonal (upper left to lower right). Note this is the same as transposing a matrix, so change your code to transpose a matrix.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Generally your swap loop should run only to n/2. If they run to n you're swapping the elements twice and you end up with the original order.
    Kurt

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    I used a fixed sized matrix and added some output to your example code to show what is being swapped and the intermediate states of the matrix. Compile and run this so you can see what is going on. Are you sure you want to reflect the matrix on the diagonal that goes from upper right to lower left?

    Code:
    #include <conio.h>
    #include <stdio.h>
    
    #define n 4
    
    int matrix[n][n];
                 
    void showm(void)
    {
    int i, j;
    
        for(i = 0; i < n; i++){
            for(j = 0; j < n; j++){
                printf("%02x ", matrix[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }
    
    int main ()
    {
    int i, j, temp;
    
        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++)
                matrix[i][j] = i * 0x10 + j;
    
        showm();
    
        for(i = 0; i < n; i++){
            for(j = 0; j < i; j++){
                printf("%d %d : %d %d\n", i, j, n-1-j, n-1-i);
                temp                 = matrix[i][j];
                matrix[i][j]         = matrix [n-1-j][n-1-i];
                matrix[n-1-j][n-1-i] = temp;
                showm();
            }
        }
    
    
        getch();
        return(0);
    }
    Last edited by rcgldr; 06-10-2013 at 02:37 PM.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Just to be clear, this program is supposed to swap the elements of the same name as shown in this example?

    Code:
    a1 a2 a3 a4 xx
    b1 b2 b3 xx a4
    c1 c2 xx b3 a3
    d1 xx c2 b2 a2
    xx d1 c1 b1 a1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in Program to find Transpose of Given Square Matrix
    By nazanin.padekan in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2013, 12:51 PM
  2. Replies: 3
    Last Post: 10-29-2011, 04:26 AM
  3. square root of a matrix
    By kwzeet in forum C Programming
    Replies: 2
    Last Post: 07-07-2011, 12:26 AM
  4. A square matrix with complex numbers
    By bchudomelka in forum C Programming
    Replies: 5
    Last Post: 05-28-2010, 06:35 PM
  5. square matrix
    By sal817 in forum C Programming
    Replies: 1
    Last Post: 04-07-2006, 11:09 PM