Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication25.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "iostream"
- #include "stdlib.h"
- using namespace std;
- int** createMatrix(int rows, int cols)
- {
- int **temp = (int**)malloc(rows * sizeof(int));
- if (temp == NULL)
- {
- cout << "Blad alokowania pamieci";
- return NULL;
- }
- for (int i = 0;i < rows;i++)
- {
- temp[i] = (int*)calloc(cols,sizeof(int));
- if (temp[i] == NULL)
- {
- cout << "Blad alokowania pamieci";
- return NULL;
- }
- }
- return (int**)realloc(temp, rows*cols * sizeof(int));
- }
- void displayMatrix(int **matrix, int rows, int cols)
- {
- for (int i = 0;i < rows;i++)
- {
- for (int x = 0;x < cols;x++)
- {
- cout << matrix[i][x] << "\t";
- }
- cout << endl;
- }
- }
- void freeMemory(int **matrix,int rows)
- {
- for (int x = 0;x < rows;x++)
- {
- free(matrix[x]);
- }
- free(matrix);
- }
- int main()
- {
- int rows, cols;
- int** matrix;
- cout << "Wymiary: ";
- cin >> rows >> cols;
- matrix = createMatrix(rows, cols);
- if (matrix != NULL)
- {
- displayMatrix(matrix, rows, cols);
- freeMemory(matrix, rows);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement