Latest

6/recent/ticker-posts

write a program to rotate a matrix in 90 degree angle using dynamic memory allocation and random number generator.

 1. write a program to rotate a matrix in 90 degree angle using dynamic memory allocation and random number generator. 

CODE in C:

#include <stdio.h>

#include <stdlib.h>

#include<time.h>

 

int main()

{

    int n,upper,lower;

    printf("enter order of matrix and the range of the elements of the matrix");

    scanf("%d%d%d",&n,&upper,&lower);

    int *a=(int*)malloc(n*n*sizeof(int));

    srand(time(0));

    printf("the original matrix is\n");

    for(int i=0;i<n;i++)

    {

        for(int j=0;j<n;j++)

        {

            *(a + i*n+j)=(rand()%(upper-lower+1))+lower;

            printf("%d  " ,*(a+i*n+j));

 

        }

        printf("\n");

    }

     for (int i = 0; i < n / 2; i++)

        {

        for (int j = i; j < n- i - 1; j++)

        {

 

 

            int temp = *(a+i*n+j);

            *(a+i*n+j) =*(a+(n-1-j)*n +i);

            *(a+(n-1-j)*n +i) =*(a+(n-1-i)*n+(n-1-j));

            *(a+(n-1-i)*n+(n-1-j)) = *(a+j*n+(n-1-i));

            *(a+j*n+(n-1-i)) = temp;

        }

    }

    printf("after rotating\n");

     for(int i=0;i<n;i++)

    {

        for(int j=0;j<n;j++)

        {

           printf("%d  ",*(a+i*n+j));

        }

        printf("\n");

    }

    return 0;


OUTPUT:


Post a Comment

0 Comments