Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define ERROR_TEXT "ALLOCATING MEMORY PROBLEMS "
- void fillingArray(double **matrix, int matrixSize);
- void showArray(double **matrix, int matrixSize1);
- void freeMatrix(double **matrix, int matrixSize);
- double **allocateMatrix(int matrixSize);
- int main()
- {
- int matrixSize;
- double **matrix = NULL;
- printf("Please insert size of the matrix");
- scanf("%d", &matrixSize);
- matrix = allocateMatrix(matrixSize);
- if(matrix != NULL)
- {
- fillingArray(matrix, matrixSize);
- showArray(matrix, matrixSize);
- freeMatrix(matrix, matrixSize);
- }
- return 0;
- }
- void freeMatrix(double **matrix, int matrixSize)
- { //free allocate memory
- int currentRow;
- for(currentRow = 0; currentRow < matrixSize; currentRow++)
- {
- free(matrix[currentRow]);
- free(matrix);
- }
- }
- double **allocateMatrix(int matrixSize)
- {
- int givenRow, currentRow;
- double **matrix = malloc((matrixSize) * sizeof(double*)); //allocates space for number of rows
- if(matrix == NULL)
- {
- printf(ERROR_TEXT);
- }
- else
- {
- for(givenRow = 0; givenRow < matrixSize; givenRow++)
- {
- matrix[givenRow] = malloc(sizeof(double)*(matrixSize)); //fullfils rows with space for data
- if(matrix[givenRow] == NULL)
- {
- printf(ERROR_TEXT);
- for(currentRow = 0; currentRow < givenRow; currentRow++) //empties space allocated prieviously
- {
- free(matrix[currentRow]);
- }
- free(matrix); //empites space allocated for number of rows
- }
- }
- }
- return matrix;
- }
- void fillingArray(double **matrix, int matrixSize)
- {
- int currentRow, currentColumn;
- printf("\n\n");
- for(currentRow = 0; currentRow < matrixSize; currentRow++)
- {
- for(currentColumn = 0; currentColumn < matrixSize; currentColumn++)
- {
- scanf("%lf",&matrix[currentRow][currentColumn]);
- }
- }
- }
- void showArray(double **matrix, int matrixSize)
- {
- int currentRow, currentColumn;
- for(currentRow = 0; currentRow < matrixSize; currentRow++)
- {
- for(currentColumn = 0; currentColumn < matrixSize; currentColumn++)
- {
- printf("%lf \t",matrix[currentRow][currentColumn]);
- }
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement