Advertisement
avr39ripe

PV024arr2DDynMagic

Jan 14th, 2021
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. //int max(int a, int b)
  4. //{
  5. //  return a > b ? a : b;
  6. //}
  7. //
  8. //double max(double a, double b)
  9. //{
  10. //  return a > b ? a : b;
  11. //}
  12. //
  13. //char max(char a, char b)
  14. //{
  15. //  return a > b ? a : b;
  16. //}
  17. //template <typename T>
  18. //T max(T a, T b)
  19. //{
  20. //  return a > b ? a : b;
  21. //}
  22. //
  23. //int max(int a, int b)
  24. //{
  25. //  return a > b ? a : b;
  26. //}
  27. //
  28. //char max(char a, char b)
  29. //{
  30. //  return a > b ? a : b;
  31. //}
  32. //
  33. //double max(double a, double b)
  34. //{
  35. //  return a > b ? a : b;
  36. //}
  37. //
  38. //max(1, 3);
  39. //max('a', 'z');
  40. //max(5, 7);
  41. //max(1, 'a');
  42. //max<double>('a', true);
  43.  
  44. template <typename T>
  45. T* createArr(int xSize)
  46. {
  47.     return new T[xSize];
  48. }
  49.  
  50. template <typename T>
  51. void deleteArr(T* arr)
  52. {
  53.     delete[] arr;
  54. }
  55.  
  56.  
  57. template <typename T>
  58. T** createArr2D(int ySize, int xSize)
  59. {
  60.     T** container{ new T * [ySize] };
  61.  
  62.     for (int y{ 0 }; y < ySize; ++y)
  63.     {
  64.         container[y] = new T[xSize];
  65.     }
  66.  
  67.     return container;
  68. }
  69.  
  70. template <typename T>
  71. void deleteArr2D(T** container, int ySize)
  72. {
  73.     for (int y{ 0 }; y < ySize; ++y)
  74.     {
  75.         delete[] container[y];
  76.     }
  77.     delete[] container;
  78. }
  79.  
  80. template <typename T>
  81. T** createArr2DLinear(int ySize, int xSize)
  82. {
  83.     T** container{ new T * [ySize] };
  84.     T* lineArr{ new T[ySize * xSize] };
  85.     for (int y{ 0 }; y < ySize; ++y)
  86.     {
  87.         container[y] = lineArr + (xSize * y);
  88.     }
  89.     return container;
  90. }
  91.  
  92. template <typename T>
  93. void deleteArr2DLinear(T** container)
  94. {
  95.     delete[] container[0];
  96.     delete[] container;
  97. }
  98.  
  99. template <typename T>
  100. void fillArr2D(T** container, int ySize, int xSize)
  101. {
  102.     for (int y{ 0 }; y < ySize; ++y)
  103.     {
  104.         for (int x{ 0 }; x < xSize; ++x)
  105.         {
  106.             container[y][x] = rand() % 10;
  107.         }
  108.     }
  109. }
  110.  
  111. template <typename T>
  112. void printArr2D(T** container, int ySize, int xSize)
  113. {
  114.     for (int y{ 0 }; y < ySize; ++y)
  115.     {
  116.         for (int x{ 0 }; x < xSize; ++x)
  117.         {
  118.             std::cout << container[y][x] << '\t';
  119.         }
  120.         std::cout << '\n';
  121.     }
  122. }
  123.  
  124. template <typename T>
  125. T max(T a, T b)
  126. {
  127.     return a > b ? a : b;
  128. }
  129.  
  130. int main()
  131. {
  132.     double* ptrExt{};
  133.     {
  134.        
  135.         double* ptrD{ new double[100000] };
  136.         ptrExt = ptrD;
  137.     }
  138.     // work with dyn memory
  139.  
  140.     delete[] ptrExt;
  141.  
  142.     //int numI{ 42 };//[100][][][]
  143.     //float numF{ 42.0 };//[300][][][]
  144.     //bool flag{ true }; //[400]
  145.  
  146.     //void* ptrXZ;
  147.     //int* ptrInt;
  148.     //float* ptrFloat;
  149.  
  150.  
  151.     //ptrInt = &numI;
  152.     //ptrFloat = &numF;
  153.  
  154.     ////ptrXZ = &flag;
  155.     //ptrXZ = &numI;
  156.     //std::cout << *(float*)ptrXZ << '\n';
  157.     //ptrXZ = &numF;
  158.     //std::cout << *(int*)ptrXZ << '\n';
  159.  
  160.     //return 0;
  161.     //int arr[5]{ 1,2,3,4,5 };
  162.     ////[100][][][](1) [][][][](2) [108][][][](3) [][][][](4) [][][][](5)
  163.  
  164.     //int* ptrI{ arr + 2 }; // arr -> int* const arr <- 100
  165.     //// 100 + 2 -> 100 + 2 * sizeof(int) -> 100 + 2 * 4 -> 108
  166.     //std::cout << *ptrI << '\n';
  167.  
  168.     //return 0;
  169.     //char(*fPtr)(char, char) { max<char> };
  170.     int ySize{ 3 };
  171.     int xSize{ 3 };
  172.  
  173.     int** arr2D{ createArr2DLinear<int>(ySize, xSize) };
  174.  
  175.     fillArr2D(arr2D, ySize, xSize);
  176.     printArr2D(arr2D, ySize, xSize);
  177.  
  178.     std::cout << '\n';
  179.  
  180.     for (int y{ 0 }; y < ySize; ++y)
  181.     {
  182.         for (int x{ 0 }; x < xSize; ++x)
  183.         {
  184.             if (x == y)
  185.             {
  186.                 arr2D[y][x] = 33;
  187.             };
  188.         }
  189.     }
  190.     arr2D[1][2] = 444;
  191.  
  192.     printArr2D(arr2D, ySize, xSize);
  193.  
  194.     deleteArr2DLinear(arr2D);
  195.  
  196.     return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement