Advertisement
Mary_99

Determinant see progres

May 7th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. #define ERROR_TEXT "ALLOCATING MEMORY PROBLEMS "
  6.  
  7.  
  8. void fillingArray(double **matrix, int matrixSize);
  9. void showArray(double **matrix, int matrixSize1);
  10. void freeMatrix(double **matrix, int matrixSize);
  11.  
  12. double **allocateMatrix(int matrixSize);
  13.  
  14.  
  15. int main()
  16. {
  17.     int matrixSize;
  18.  
  19.     double **matrix = NULL;
  20.  
  21.     printf("Please insert size of the matrix");
  22.     scanf("%d", &matrixSize);
  23.  
  24.     matrix = allocateMatrix(matrixSize);
  25.      
  26.     if(matrix != NULL)
  27.     {
  28.         fillingArray(matrix, matrixSize);
  29.         showArray(matrix, matrixSize);
  30.         freeMatrix(matrix, matrixSize);
  31.     }
  32.      
  33.     return 0;  
  34. }
  35.  
  36.  
  37. void freeMatrix(double **matrix, int matrixSize)
  38. {                                                                //free allocate memory
  39.     int currentRow;
  40.     for(currentRow = 0; currentRow < matrixSize; currentRow++)
  41.     {
  42.         free(matrix[currentRow]);
  43.         free(matrix);
  44.     }
  45. }
  46.  
  47. double **allocateMatrix(int matrixSize)
  48. {
  49.     int givenRow, currentRow;  
  50.     double **matrix = malloc((matrixSize) * sizeof(double*));   //allocates space for number of rows
  51.    
  52.     if(matrix == NULL)
  53.     {
  54.         printf(ERROR_TEXT);
  55.     }
  56.     else
  57.     {
  58.         for(givenRow = 0; givenRow < matrixSize; givenRow++)
  59.         {
  60.             matrix[givenRow] = malloc(sizeof(double)*(matrixSize)); //fullfils rows with space for data
  61.             if(matrix[givenRow] == NULL)
  62.             {
  63.                 printf(ERROR_TEXT);
  64.                 for(currentRow = 0; currentRow < givenRow; currentRow++)    //empties space allocated prieviously
  65.                 {
  66.                     free(matrix[currentRow]);
  67.                    
  68.                 }
  69.                 free(matrix);                       //empites space allocated for number of rows
  70.             }
  71.         }
  72.     }
  73.     return matrix;
  74. }
  75.  
  76. void fillingArray(double **matrix, int matrixSize)
  77. {
  78.     int currentRow, currentColumn;
  79.     printf("\n\n");
  80.     for(currentRow = 0; currentRow < matrixSize; currentRow++)
  81.     {
  82.         for(currentColumn = 0; currentColumn < matrixSize; currentColumn++)
  83.         {
  84.             scanf("%lf",&matrix[currentRow][currentColumn]);
  85.         }
  86.     }
  87. }
  88.  
  89. void showArray(double **matrix, int matrixSize)
  90. {
  91.     int currentRow, currentColumn;
  92.     for(currentRow = 0; currentRow < matrixSize; currentRow++)
  93.     {
  94.         for(currentColumn = 0; currentColumn < matrixSize; currentColumn++)
  95.         {
  96.             printf("%lf \t",matrix[currentRow][currentColumn]);
  97.         }
  98.         printf("\n");
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement