Latest :

C Program : Rotate the Matrix by K Times | C Porgrams

Write a C program to rotate the given square matrix by k times. To implement this, we require the size of square matrix along with the elements or data values of the square matrix. The desire output is also a matrix of 2D array after rotating the input array k times, Rotate matrix by 90 degrees clockwise

The first step to follow when given a problem statement is, to understand it thoroughly and gather the necessary inputs. To read our required set of inputs at runtime in C lang, we can make use of predefined input function, scanf().

It reads inputs of any primitive datatype at runtime and based on the format specifier mentioned in the function, the datatype of input is determined. As all our inputs are of integer type, we will be using ‘%d’ format specifier.

Firstly, we read the size (n) of the input square matrix (2D array) and create the 2D array of the same size using dynamic memory allocation (calloc()). Then, we iterate through all the rows and columns to read the elements or data values of the array. Following this, we have to read the value of k indicating the number of rotations (column wise) to be made.

printf(“\nEnter the N value for square matrix :”);

scanf(“%d”,&n);

a= (int **)calloc(n,sizeof(int*));

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

a[i] =(int *)calloc(n,sizeof(int*));

printf(“\nEnter the elements in %dX%d matrix :”,n,n);

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

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

scanf(“%d”,&a[i][j]);

}

}

printf(“\nEnter the k value for rotation of matrix :”);

scanf(“%d”,&k);

Since we have to perform rotation k times, to complete it at a go, we store the k values in the array of each row and then, move it all to the end.

For this, we’ll create a 1D array of size k dynamically. For every row that we iterate, the first k elements we’ll store in this newly created array and move the remaining elements k indices front i.e., a[i]=a[i+k].

Then, the k elements stored in the array are added to the end after (n-k)th index to the end. For every row we’ll repeat the same. So by the end, the matrix we acquire is nothing but, our resultant output i.e., the input matrix after k rotations.

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

for (j=0;j<k; j++) {

a1[j] = a[i][j];

}

for (int j= 0; j<n-k; j++) {

a[i][j] = a[i][j+k];

}

for (int j=n-k,t=0; j<n; j++) {

a[i][j] =a1[t++];

}

}

This resultant output is then displayed on the console screen using predefined output function, printf(). This is displayed in matrix format with every row displayed in the new line.

Program Rotate matrix by 90 degrees clockwise

Output:

Using Function

Earlier, we have seen the logic to use to rotate a given matrix k times. But we can also observe that, the entire code is within main method. This is not necessarily bad but, it has a few disadvantages.

If same logic is to be applied somewhere else in the code then, we’ll have to rewrite the same set of statement again. Also, since the entire code is within main method, it could get confusing at times to find some particular part of it.

So, to make the code reusable and increase the readability of the code, we can make use of functions. On the basis of the functionality, we can split the code into parts and place them in separate function blocks.

Therefore, if same logic/functionality is required we can just make a function call and pass necessary arguments thereby, making it reusable. Also, since we have different function blocks now, each having a specific functionality, the readability is improved as well.

To implement the same for the given problem statement, we will split the code as follows. First we’ll read the size of array and create an array of the same size dynamically in main method. Then, we’ll make a function call to the input() function.

This function reads the elements of the array by iterating through all the rows and columns. Following this, we call the print() function which displays the array in matrix format. Later we call the reverse_rows() function which has the logic to rotate the matrix k times.

This rotated matrix is then displayed in matrix format on console screen by invoking the print() function again.

error code: 523
x

Check Also

C Program To Print Number Of Days In A Month | Java Tutoring

C program to input the month number and print the number of days in that ...