Advertisement
krzotki

pedał 2

Jan 21st, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. // ConsoleApplication25.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "iostream"
  6. #include "stdlib.h"
  7.  
  8. using namespace std;
  9.  
  10. int** createMatrix(int rows, int cols)
  11. {
  12.     int **temp = (int**)malloc(rows * sizeof(int));
  13.     if (temp == NULL)
  14.     {
  15.         cout << "Blad alokowania pamieci";
  16.         return NULL;
  17.     }
  18.     for (int i = 0;i < rows;i++)
  19.     {
  20.         temp[i] = (int*)calloc(cols,sizeof(int));
  21.         if (temp[i] == NULL)
  22.         {
  23.             cout << "Blad alokowania pamieci";
  24.             return NULL;
  25.         }
  26.     }
  27.     return (int**)realloc(temp, rows*cols * sizeof(int));
  28. }
  29. void displayMatrix(int **matrix, int rows, int cols)
  30. {
  31.     for (int i = 0;i < rows;i++)
  32.     {
  33.         for (int x = 0;x < cols;x++)
  34.         {
  35.             cout << matrix[i][x] << "\t";
  36.         }
  37.         cout << endl;
  38.     }
  39. }
  40. void freeMemory(int **matrix,int rows)
  41. {
  42.     for (int x = 0;x < rows;x++)
  43.     {
  44.         free(matrix[x]);
  45.     }
  46.     free(matrix);
  47. }
  48.  
  49. int main()
  50. {
  51.     int rows, cols;
  52.     int** matrix;
  53.     cout << "Wymiary: ";
  54.     cin >> rows >> cols;
  55.  
  56.     matrix = createMatrix(rows, cols);
  57.     if (matrix != NULL)
  58.     {
  59.         displayMatrix(matrix, rows, cols);
  60.         freeMemory(matrix, rows);
  61.     }
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement