Thread: Rotate the matrix

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    2

    Rotate the matrix

    I'm trying to rotate a matrix inplace (clockwise direction). Below is my code, everything seems right but I'm getting runtime error. Can anybody explain to me why I'm getting runtime error.


    Code:
    #include<stdio.h>
    #define SIZE 4
    int matrix[4][4] = { {10,11,12,13}, {14,15,16,17}, {18,19,20,21}, {22,23,24,25} };
     
    void swap(int *p, int *q)
    {
        int *t;
        *t = *p;
        *p = *q;
        *q = *t;
    }
     
    void printMatrix()
    {
        int i, j;
        for (i = 0; i < SIZE; ++i)
        {
            for (j = 0; j < SIZE; ++j)
            {
                printf("%d ", matrix[i][j]);
            }
            printf("\n");
        }
    }
     
    int main()
    {
        int first, last, i, t, n = 0;
        printf("Before Rotation:\n");
        printMatrix();
     
        while(n < SIZE/2)
        {
            first = n; 
            last = (SIZE-1)-n;
     
            /* Save First Element */
            t = matrix[n][n+1];
     
            /* Top Row */
            for (i = first; i <= last; ++i)
            {
                swap(&t, &matrix[first][i]);
            }
     
            /* Last Column */
            for (i = first+1; i <= last; ++i)
            {
                swap(&t, &matrix[i][last]);
            }
     
            /* Bottom Row */
            for (i = last-1; i >= first; --i)
            {
                swap(&t, &matrix[last][i]);
            }
     
            /* First Column */
            for (i = last-1; i >= first; --i)
            {
                swap(&t, &matrix[i][first]);
            }
     
            ++n;
        }
     
        printf("After Rotation:\n");
        printMatrix();
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try
    Code:
        int t;
        t = *p;
    There is no need for t to be a pointer.
    If it really must be a pointer, you need to make it point somewhere before doing *t = on it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2016
    Posts
    2

    Red face

    Quote Originally Posted by Salem View Post
    Try
    Code:
        int t;
        t = *p;
    There is no need for t to be a pointer.
    If it really must be a pointer, you need to make it point somewhere before doing *t = on it.
    Thanks it worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-18-2016, 07:24 PM
  2. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  3. How do I rotate a matrix 45 degrees?
    By caduardo21 in forum C Programming
    Replies: 2
    Last Post: 06-06-2005, 01:19 AM
  4. Do I really need to rotate?
    By JimJoyce in forum Game Programming
    Replies: 13
    Last Post: 02-24-2005, 04:04 PM
  5. How to rotate
    By cfrost in forum Windows Programming
    Replies: 5
    Last Post: 07-15-2004, 10:08 AM

Tags for this Thread