Thread: Beginner C issue ( matrix 90*)

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    2

    Beginner C issue ( matrix 90*)

    So I have this program I made which makes a matrix rotate by 90 degrees to the left. The problem is after I run it, it does show me rotated matrix but it crashes giving me this : Gyazo - 1645b2f0f3328381d8cd773c6c96e843.png
    Gyazo - f7f3a9280469291ef1331ed119d83821.png
    Gyazo - 30de4561f483bb50dfc8fd40c2d1ba04.png

    Code :

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    
    
    int *read_matrix(int *dim)
    {
        int i,j;
        int *A;
        printf("Enter the matrix dimension ");
        scanf("%d",dim);
        
        A=(int *)malloc((*dim)*sizeof(int));
        if (!A)
            {
                printf("Memory couldn't be allocated'");
                exit(1);
            }
        printf("Memory allocated succesfully \n");
        
        for(i=0;i<*dim;i++)
                for(j=0;j<*dim;j++)
                    {
                        printf("A[%d][%d] = ",i+1,j+1);
                        scanf("%d",A+*dim*i+j);
                    }
            
        return A;
    }
    
    
    
    
    int *rotate_matrix(int *A,int dim)
    {
        int i,j;
        int temp;
        
        for ( i=0; i< dim/2 ;i++ )
        {
    
    
            for ( j=i;j<dim-i-1;j++ )
                {
    
    
                        temp = *(A+dim*i+j);
     
                    *(A+dim*i+j) = *(A+dim*j+dim-1-i); 
     
                    *(A+dim*j+dim-1-i) = *(A+dim*(dim-1-i)+dim-1-j); 
    
    
                    *(A+dim*(dim-1-i)+dim-1-j) = *(A+dim*(dim-1-j)+i); 
     
                    *(A+dim*(dim-1-j)+i) = temp;
                   }
        }
        
     
        return A;
    }
    
    
    
    
    
    
    void show_matrix(int *A,int dim)
    {
        int i,j;
        for(i=0;i<dim;i++)
            {
                for(j=0;j<dim;j++)
                    {
                        printf("%d \t",*(A+dim*i+j));
                    }
                printf("\n");
            }
    }
    
    
    void free_m(int *A)
        {
            if (A!=NULL)
                free(A);
        }
    
    
    int main()
    {
        int i,j;
        int *A;
        int dim=0;
    
    
    
    
        A=read_matrix(&dim);
        
        printf("Matrix is  : \n");
        show_matrix(A,dim);
        
        A=rotate_matrix(A,dim);
        
        printf(" The rotated matrix is :  \n");
        show_matrix(A,dim);
        
        free_m(A);
        
         system("PAUSE");
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You're not allocating enough memory. Instead of malloc(*dim * sizeof(int)) it should be malloc(*dim * *dim * *sizeof(int))

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    2
    Thank you, it worked.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by MarkussR View Post
    Thank you, it worked.
    No problem. Another point is that rotate_matrix doesn't need to return the matrix since it's rotating the input matrix in place. And it can be made a little cleaner looking:
    Code:
    void rotate_matrix(int *A, int dim) {
        for (int row = 0; row < dim / 2; row++) {
            int row_end = dim - row - 1;
    
            for (int col = row; col < row_end; col++) {
                int col_end = dim - col - 1;
    
                int *p = A + dim * row     + col;
                int *q = A + dim * col     + row_end;
                int *r = A + dim * row_end + col_end;
                int *s = A + dim * col_end + row;
    
                int temp = *p;
                *p = *q;
                *q = *r;
                *r = *s;
                *s = temp;
            }
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner variable issue
    By YayIguess in forum C Programming
    Replies: 2
    Last Post: 09-30-2015, 08:13 AM
  2. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  3. Super beginner C programming issue
    By ollenia in forum C Programming
    Replies: 2
    Last Post: 09-24-2012, 08:26 AM
  4. Replies: 8
    Last Post: 01-17-2006, 05:39 PM
  5. matrix program issue, recursion
    By sweener2001 in forum C Programming
    Replies: 1
    Last Post: 04-28-2005, 04:28 AM

Tags for this Thread