Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
C Program Transpose of a Matrix 2 Ways | C Programs
Latest :

C Program Transpose of a Matrix 2 Ways | C Programs

C program to find the transpose of a given matrix. Transpose of a matrix is nothing but flipping the matrix via its diagonal such that, the rows and columns of the matrix are switched i.e., a[i][j] becomes a[j][i] and vice versa.

To implement this, we require an input matrix (2D array) whose transpose matrix is to be found along with the number of rows and columns in that matrix. The expected output is also a matrix or 2D array which represents the transpose of the given input matrix.

For this, the first step is to gather all the necessary inputs. To read the inputs at runtime, we can make use of the predefined function, scanf(). This is well-known to read any primitive datatype input at runtime.

The datatype of the input being read is determined by the format specifier mentioned in the code. In this case, all our required inputs are of integer type therefore, we make use of ‘%d’ format specifier for all. We first read the number of rows and columns of the matrix and create an 2D array  using dynamic memory allocation (calloc).

If the number of rows is greater than columns then, we create a matrix of dimension arr[rows][rows] else, we create with dimensions arr[column][column].

This is because, when we transpose the array the row becomes column and vice versa so, it should have sufficient memory to store them. Then, we iterate and read all the data values of elements of the matrix (2D array).

int r,c,i, j,**arr1,x,t;

printf(“\nEnter the number of rows :”);

scanf(“%d”,&r);

printf(“\nEnter the number of coloums :”);

scanf(“%d”,&c);

if(r>=c)

x=r;

else x=c;

arr1 = (int **)calloc(x,sizeof(int*));

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

arr1[i] =(int *)calloc(x,sizeof(int*));

printf(“\nEnter the elements in 2d array :”);

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

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

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

}

}

We can then display the array in matrix format on the output console screen for a better clarity of the user.

For this, we’ll be using predefined output function, printf() with the same format specifier as input. For completion of every row, we change it to a new line.

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

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

printf(“%d\t”, arr1[i][j]);

}

printf(“\n”);

}

Later, we iterate through all the rows (i=0) to end (i<x) and columns (j=0) to end (j<x) and if value of outer loop or row (i) is less than the inner loop or column (j) then, we swap that position arr1[i][j] with arr1[j][i] using temporary variable (t) thereby, interchanging the rows and columns.

We check for i less than j condition since, we already swap them with its corresponding value of j less than i we do not need to swap them again. If we do so then, it’ll result in the input matrix again.

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

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

if(i<j) {

t=arr1[i][j];

arr1[i][j]=arr1[j][i];

arr1[j][i]=t;

}

}

}

After completion of all the iterations, all the row values are interchanged with its corresponding column values and vice versa. This newly modified matrix is nothing but, our desired resultant output matrix i.e., the transpose of the input matrix. We can then, display this on console screen.

printf(“\n after transpose of matrix :\n”);

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

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

printf(“%d\t”, arr1[i][j]);

}

printf(“\n”);

}

Transpose Matrix In C Program

Output:

Transpose Matrix C Program Using Function

Above, we have seen the logic to find the transpose of the given matrix but since, the entire code is within main method itself it has a few disadvantages. If same logic is required elsewhere in the code then, we might have to rewrite it all over again. Apart from that, the code is not quite readable and finding some part can be time consuming as, we have to search the entire main method.

To overcome this, we can make use of functions instead. In this case, we generally split the code based on its functionality and place in within different function blocks. This way, the code is placed systematically with a clearer understanding of which part is placed where thereby, enhancing the readability of the code.

Apart from that, if a particular logic is needed elsewhere then, we can just call the function and pass the required parameters instead of rewriting it hence, making the code reusable.

Here, we implement the same in similar fashion. The main method first reads the number of rows and columns on the input matrix using scanf() and dynamically allocates the memory for the matrix similar to that of above.

Then, it makes a function call to user-defined function (input) and passes the array, rows and columns as parameter. In this function, it reads the data values or elements of the matrix or 2D array passed as parameter.

It then returns to the main method where we display the input matrix before transposing it using another user-defined function (print) passing the same parameters. This function prints the parameter array in matrix format, with every row displayed in the new line.

We then call a user-defined function (transpose) and pass the same parameters.

This function contains the logic discussed above to interchange the rows and columns and thereby, transposing the matrix. After this, it again comes back to the main method where this transposed matrix is displayed by again call the user-defined function (print).

This way, based on the logic or functionality, the code is split into three user-defined functions- input, print and transpose and each of this function can now be reusable. Similarly because of implementing this way, it has improved the readability as well.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
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 ...