Advertisement
krzotki

lepsze dynamiczne macierze :)))

Jan 21st, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. // ConsoleApplication23.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "stdlib.h"
  6. #include "stdio.h"
  7. #include "iostream"
  8. #include "iomanip"
  9.  
  10. using namespace std;
  11.  
  12. int getRandomInt(int min, int max);
  13. int** allocMemory(int rows, int cols);
  14. int** randomMatrix(int rows, int cols, int min, int max);
  15. void displayMatrix(int **m, int rows, int cols);
  16. void freeMemory(int**matrix, int rows);
  17. int** addMatrices(int **m1, int **m2,int rows, int cols);
  18. int main()
  19. {
  20.  
  21.     int **matrix1 = NULL;
  22.     int **matrix2 = NULL;
  23.     int **matrix3 = NULL;
  24.     int rows, cols, min, max;
  25.     cout << "wymiary: ";
  26.     cin >> rows >> cols;
  27.     cout << endl << "min max: ";
  28.     cin >> min >> max;
  29.  
  30.     matrix1 = randomMatrix(rows, cols, min, max);
  31.     matrix2 = randomMatrix(rows, cols, min, max);
  32.     matrix3 = addMatrices(matrix1, matrix2, rows, cols);
  33.  
  34.     displayMatrix(matrix1, rows, cols);
  35.     displayMatrix(matrix2, rows, cols);
  36.     displayMatrix(matrix3, rows, cols);
  37.  
  38.     freeMemory(matrix1, rows);
  39.     freeMemory(matrix2, rows);
  40.     freeMemory(matrix3, rows);
  41.     return 0;
  42. }
  43.  
  44. int getRandomInt(int min, int max)
  45. {
  46.     return rand() % (max - min + 1) + min;
  47. }
  48.  
  49. int** allocMemory(int rows, int cols)
  50. {
  51.     int **temp = (int**)malloc(rows * sizeof(int));
  52.     if (temp == NULL)return NULL;
  53.     for (int i = 0;i < rows;i++)
  54.     {
  55.         temp[i] = (int*)malloc(cols * sizeof(int));
  56.         if (temp[i] == NULL)return NULL;
  57.     }
  58.     return (int**)realloc(temp, rows*cols * sizeof(int));
  59. }
  60.  
  61. void freeMemory(int**matrix, int rows)
  62. {
  63.     for (int i = 0;i < rows;i++)
  64.     {
  65.         free(matrix[i]);
  66.     }
  67.     free(matrix);
  68. }
  69.  
  70. int** randomMatrix(int rows, int cols, int min, int max)
  71. {
  72.     int **temp = allocMemory(rows, cols);
  73.     if (temp == NULL)return NULL;
  74.     for (int i = 0;i < rows;i++)
  75.     {
  76.         for (int x = 0;x < cols;x++)
  77.         {
  78.             temp[i][x] = getRandomInt(min, max);
  79.         }
  80.     }
  81.     return (int**)realloc(temp, rows*cols * sizeof(int));
  82. }
  83. void displayMatrix(int **m, int rows, int cols)
  84. {
  85.     if (m == NULL)return;
  86.     int cellSize = 3;
  87.     for (int i = 0;i < rows;i++)
  88.     {
  89.         for (int x = 0;x < cols;x++)
  90.         {
  91.             cout << setw(cellSize);
  92.             cout << m[i][x];
  93.         }
  94.         cout << endl;
  95.     }
  96.     cout << endl;
  97. }
  98. int** addMatrices(int **m1, int **m2, int rows, int cols)
  99. {
  100.     if (m1 == NULL || m2 == NULL)return NULL;
  101.     int **temp = allocMemory(rows, cols);
  102.     for (int i = 0;i < rows;i++)
  103.     {
  104.         for (int x = 0;x < cols;x++)
  105.         {
  106.             temp[i][x] = m1[i][x] +m2[i][x];
  107.         }
  108.     }
  109.     return (int**)realloc(temp, rows*cols * sizeof(int));
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement