Advertisement
aaaranes

Transpose of a matrix (SEGFAULT)

Mar 21st, 2024 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. /* SEGMENTATION FAULT after the createMat() function. Please help ;-;
  4.     I don't know why that happens. I tried using pointer-to-pointer-to-pointer-to-integer
  5. for the 2d array formal parameters (ex: func(int ***matrix)) and use &matrix for the
  6. actual parameter and it works. I don't understand why it wouldn't work for "int **matrix" formal
  7. parameter with "matrix" actual parameter */
  8.  
  9. void getRowCol(int *row, int *col){
  10.     // Get Dimensions
  11.     printf("Enter row-col: ");
  12.     scanf("%d-%d", row, col);
  13. }
  14.  
  15. void createMat(int **mat, int row, int col){
  16.     // Allocate memory for a matrix
  17.     int i;
  18.     mat = (int **)malloc(sizeof(int *)*row);
  19.    
  20.     for(i=0; i<row; ++i){
  21.         *(mat+i) = (int *)malloc(sizeof(int *)*col);
  22.     }
  23. }
  24.  
  25. void inputVal(int **mat, int row, int col){
  26.     // Iterate through each cell and ask for input
  27.     int i, j;
  28.     for(i=0; i<row; ++i){
  29.         for(j=0; j<col; ++j){
  30.             printf("Enter value in A%d%d: ", i+1, j+1);
  31.             scanf("%d", (*(mat+i)+j));
  32.         }
  33.     }
  34. }
  35.  
  36. void showMat(int **mat, int row, int col){
  37.     // Print the cells
  38.     int i, j;
  39.     for(i=0; i<row; ++i){
  40.         for(j=0; j<col; ++j){
  41.             printf(" %d ", *(*(mat+i)+j));
  42.         }
  43.         printf("\n");
  44.     }
  45. }
  46.  
  47. void transposeMat(int **mat, int **transpose, int row, int col){
  48.     /* Iterate through each cell in initial matrix
  49.     and put the value in the transposed cell*/
  50.     int i, j;
  51.     for(i=0; i<row; ++i){
  52.         for(j=0; j<col; ++j){
  53.             *(*(transpose+j)+i) = *(*(mat+i)+j);
  54.         }
  55.     }
  56. }
  57.  
  58. void freeMat(int **mat, int row){
  59.     // Free each allocated pointer to avoid memory leaks
  60.     for(int i=0; i<row; ++i){
  61.         free(*(mat+i));
  62.     }
  63.     free(mat);
  64. }
  65.  
  66. int main(){
  67.     int row, col, **matrix, **transpose;
  68.     // Get Dimensions
  69.     getRowCol(&row, &col);
  70.     printf("Matrix is %d by %d\n", row, col);
  71.    
  72.     // Create Matrices
  73.     createMat(matrix, row, col);
  74.     createMat(transpose, col, row);
  75.    
  76.     // Get Values for the Initial Matrix
  77.     inputVal(matrix, row, col);
  78.    
  79.     // Show Initial Matrix
  80.     printf("Initial Matrix\n");
  81.     showMat(matrix, row, col);
  82.    
  83.     // Transpose and Show
  84.     printf("Transposed Matrix\n");
  85.     transposeMat(matrix, transpose, row, col);
  86.     showMat(transpose, col, row);
  87.    
  88.     freeMat(matrix, row);
  89.     freeMat(transpose, col);
  90.     return 0;  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement