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 "
- double **allocateMatrix();
- void fillMatrix(double **matrix);
- void showMatrix(double **matrix);
- void freeMatrix(double **matrix);
- int sizeOfMatrix;
- void freeMatrix(double **matrix)
- {
- int row;
- for(row = 0; row < sizeOfMatrix; row++)
- {
- free(matrix[row]);
- }
- free(matrix);
- }
- double **allocateMatrix()
- {
- int rowIndex, row;
- double **matrix = malloc((sizeOfMatrix) * sizeof(double*));
- if(matrix == NULL)
- {
- printf(ERROR_TEXT);
- } else {
- for(rowIndex = 0; rowIndex < sizeOfMatrix; rowIndex++)
- {
- matrix[rowIndex] = malloc(sizeof(double) * (sizeOfMatrix));
- if(matrix[rowIndex] == NULL)
- {
- printf(ERROR_TEXT);
- for(row = 0; row < rowIndex; row++)
- {
- free(matrix[row]);
- }
- free(matrix);
- }
- }
- }
- return matrix;
- }
- void fillMatrix(double **matrix)
- {
- int row, column;
- for(row = 0; row < sizeOfMatrix; row++){
- for(column = 0; column < sizeOfMatrix; column++){
- scanf("%lf", &matrix[row][column]);
- }
- }
- }
- void showMatrix(double **matrix)
- {
- int row, column;
- for(row = 0; row < sizeOfMatrix; row++){
- for(column = 0; column < sizeOfMatrix; column++){
- printf("%lf \t", matrix[row][column]);
- }
- printf("\n");
- }
- }
- int main(){
- double **matrix = NULL;
- printf("Enter size of matrix: ");
- scanf("%d", &sizeOfMatrix);
- matrix = allocateMatrix(sizeOfMatrix);
- if(matrix != NULL)
- {
- fillMatrix(matrix);
- showMatrix(matrix);
- freeMatrix(matrix);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement