Question

Please use C !!!

Write a C function matrixTranspose that takes a two-dimensional array as its input argument then transposes its elements and prints the results.

456 3 6 3 2

Develop the matrixTranspose method with two different methods:
1. Assume the matrix is a square matrix. This will make finding the transpose a simple swapping of the arrays’ elements and it can be done in place.
2. Generalize your function to work with any NxM matrix where N≠M. You will need to properly handle dynamic memory allocation for creating the transpose matrix inside the function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include<stdio.h>
void matrixTranspose(int *mat,int *trans,int r,int c){
   int i,j,k=0;
   for(i=0;i<r;i++){
       for(j=0;j<c;j++){
           trans[j*r+i]=mat[k++];                      //assigning main matrix values to transposed

                                                              //matrix with transposed   indexes.
       }
   }
}
int main(){
   int i,j,r,c;
   printf("matrix size: ");
   scanf("%d%d",&r,&c);                               //taking row and column size from user
   int mat[r][c],trans[c][r];                          //declaring matrix and transpose matrix with reverse dimensions
   printf("enter matrix:\n");
   for(i=0;i<r;i++){
       for(j=0;j<c;j++){
           scanf("%d",&mat[i][j]);                       //taking values for main matrix from user
       }
   }
   matrixTranspose(mat,trans,r,c);                       //passing addresses of main matrix and transpose matrix to matrixTranspose() function
   printf("Transpose matrix is:\n");
   for(i=0;i<c;i++){              
       for(j=0;j<r;j++){
           printf("%d ",trans[i][j]);                   //after completion of function calling main matrix transposed values strored in transpose matrix, printing those values.
       }
       printf("\n");
   }
}

Screenshots:

Dev-C++5.2.0.2 File Edit Search View Project Execute Debug Tools CVS Window Help globala) # include<stdio.h> void matrixTrans

output:

CAProgram Files (x86)ND matrix size: 3 3 enter matrix 1 2 3 4 567 8 9 auser exe ranspose matrix is: 2 5 8 ocess exited with r

Add a comment
Know the answer?
Add Answer to:
Please use C !!! Write a C function matrixTranspose that takes a two-dimensional array as its...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT