avr39ripe

cppArr2DDynExamples

Oct 6th, 2021
739
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. void printArr(int* arr, int arrSize)
  4. {
  5.     for (int i{}; i < arrSize; ++i)
  6.     {
  7.         std::cout << arr[i] << ' ';
  8.     }
  9.     std::cout << '\n';
  10. }
  11.  
  12. void printArr(int arr[][4], int sizeY, int sizeX)
  13. {
  14.     for (int y{}; y < sizeY; ++y)
  15.     {
  16.         printArr(arr[y], sizeX);
  17.     }
  18.     std::cout << '\n';
  19. }
  20.  
  21. void printArr(int** arr, int sizeY, int sizeX)
  22. {
  23.     for (int y{}; y < sizeY; ++y)
  24.     {
  25.         printArr(arr[y], sizeX);
  26.     }
  27.     std::cout << '\n';
  28. }
  29.  
  30. template<typename T>
  31. void addRowLine(T**& contaier, int sizeY, int sizeX, int rowPosition, T* newRow = nullptr);
  32. template<typename T>
  33. void addRowChunk(T**& contaier, int sizeY, int sizeX, int rowPosition, T* newRow = nullptr);
  34.  
  35. int main()
  36. {
  37.     const int arrSize{ 10 };
  38.  
  39.     int arrStatic[arrSize]{ 1,2,3,4,5,6,7,8,9,10 };
  40.     int* arrDyn{ new int[arrSize] {11,12,13,14,15,16,17,18,19,20} };
  41.  
  42.  
  43.     const int sizeY{ 3 };
  44.     const int sizeX{ 4 };
  45.  
  46.     int arr2DStatic[sizeY][sizeX]{ {111,2,3,4}, {5,6,7,8}, {9,10,11,112} };
  47.  
  48.     int** arr2DDyn{ new int* [sizeY] {new int[sizeY * sizeX]{ 1,2,3,4,5,6,7,8,9,10,11,12 }} };
  49.     //*arr2DDyn = new int[sizeY * sizeX]{ 1,2,3,4,5,6,7,8,9,10,11,12 };
  50.     //int* storage{ new int[sizeY * sizeX] { 1,2,3,4,5,6,7,8,9,10,11,12 } };
  51.  
  52.  
  53.     //arr2DDyn[0] = bigArr; bigArr + 0 * sizeX
  54.     //arr2DDyn[1] = bigArr + sizeX; bigArr + 1 * sizeX
  55.     //arr2DDyn[2] = bigArr + sizeX + sizeX; bigArr + 2 * sizeX
  56.  
  57.     for (int y{0}; y < sizeY; ++y)
  58.     {
  59.         arr2DDyn[y] = *arr2DDyn + (y * sizeX);
  60.     }
  61.  
  62.     //arr2DDyn[0] = new int[sizeX]{1,2,3,4};
  63.     //arr2DDyn[1] = new int[sizeX]{5,6,7,8};
  64.     //arr2DDyn[2] = new int[sizeX]{9,10,11,12};
  65.  
  66.     //for (int y{}; y < sizeY; ++y)
  67.     //{
  68.     //  arr2DDyn[y] = new int[sizeX] {};
  69.     //}
  70.  
  71.     arr2DDyn[1][1] = 11;
  72.     arr2DDyn[2][3] = 120;
  73.     arr2DDyn[sizeY / 2][sizeX / 2] = 42;
  74.  
  75.     /*printArr(arr2DDyn, sizeY, sizeX);*/
  76.  
  77.     printArr((int*)arr2DStatic, sizeY * sizeX);
  78.  
  79.     //for (int y{}; y < sizeY; ++y)
  80.     //{
  81.     //  delete[] arr2DDyn[y];
  82.     //}
  83.  
  84.     //delete[] arr2DDyn[0];
  85.     //delete[] arr2DDyn[1];
  86.     //delete[] arr2DDyn[2];
  87.  
  88.     delete[] *arr2DDyn;
  89.     delete[] arr2DDyn;
  90.     //int* arr0{ new int[sizeX]{1,2,3,4} };
  91.     //int* arr1{ new int[sizeX] {5,6,7,8} };
  92.     //int* arr2{ new int[sizeX] {9,10,11,12} };
  93.  
  94.  
  95.     //printArr(arrStatic, arrSize);
  96.     //printArr(arrDyn, arrSize);
  97.     // printArr(arr2DStatic, sizeY, sizeX);
  98.  
  99.     delete[] arrDyn;
  100.     return 0;
  101. }
  102.  
  103.  
  104.  
  105.  
  106.  
RAW Paste Data