Advertisement
Monika__

Determinan- wersja połowiczna

May 7th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 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. double **allocateMatrix();
  8. void fillMatrix(double **matrix);
  9. void showMatrix(double **matrix);
  10. void freeMatrix(double **matrix);
  11.  
  12. int sizeOfMatrix;
  13.  
  14. void freeMatrix(double **matrix)
  15. {
  16.   int row;
  17.   for(row = 0; row < sizeOfMatrix; row++)
  18.   {
  19.     free(matrix[row]);
  20.   }
  21.   free(matrix);
  22. }
  23.  
  24. double **allocateMatrix()
  25. {
  26.   int rowIndex, row;
  27.   double **matrix = malloc((sizeOfMatrix) * sizeof(double*));
  28.  
  29.   if(matrix == NULL)
  30.   {
  31.     printf(ERROR_TEXT);
  32.   } else {
  33.     for(rowIndex = 0; rowIndex < sizeOfMatrix; rowIndex++)
  34.     {
  35.       matrix[rowIndex] = malloc(sizeof(double) * (sizeOfMatrix));
  36.       if(matrix[rowIndex] == NULL)
  37.       {
  38.         printf(ERROR_TEXT);
  39.         for(row = 0; row < rowIndex; row++)
  40.         {
  41.           free(matrix[row]);
  42.         }
  43.         free(matrix);
  44.       }
  45.     }
  46.   }
  47.   return matrix;
  48. }
  49.  
  50. void fillMatrix(double **matrix)
  51. {
  52.   int row, column;
  53.   for(row = 0; row < sizeOfMatrix; row++){
  54.     for(column = 0; column < sizeOfMatrix; column++){
  55.       scanf("%lf", &matrix[row][column]);
  56.     }
  57.   }
  58. }
  59.  
  60. void showMatrix(double **matrix)
  61. {
  62.   int row, column;
  63.   for(row = 0; row < sizeOfMatrix; row++){
  64.     for(column = 0; column < sizeOfMatrix; column++){
  65.       printf("%lf \t", matrix[row][column]);
  66.     }
  67.     printf("\n");
  68.   }
  69. }
  70.  
  71. int main(){
  72.   double **matrix = NULL;
  73.  
  74.   printf("Enter size of matrix: ");
  75.   scanf("%d", &sizeOfMatrix);
  76.  
  77.   matrix = allocateMatrix(sizeOfMatrix);
  78.   if(matrix != NULL)
  79.   {
  80.     fillMatrix(matrix);
  81.     showMatrix(matrix);
  82.     freeMatrix(matrix);
  83.   }
  84.  
  85.   return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement