Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // function declarations
  5. void v_alloc_table_add_5(int);
  6. bool b_alloc_table_2_dim(int***, int, int);
  7. bool b_dealloc_table_2_dim(int***, int, int);
  8. void v_alloc_table_char(int iSize);
  9.  
  10.  
  11.  
  12. int main()
  13. {
  14.  
  15. // TASK 1
  16. //v_alloc_table_add_5(10);
  17.  
  18. // TASK 2
  19. //int** pi_table;
  20. //b_alloc_table_2_dim(&pi_table, 5, 3);
  21.  
  22. // TASK 3
  23. //b_dealloc_table_2_dim(&pi_table, 5, 3);
  24.  
  25. v_alloc_table_char(5);
  26.  
  27. cout << "\n\n---The end---\n";
  28.  
  29. }
  30.  
  31.  
  32.  
  33. // 1
  34. void v_alloc_table_add_5(int iSize) {
  35.  
  36. // checking for invalid size argument
  37. if (iSize <= 0) {
  38. cout << "Sorry, wrong size!" << endl;
  39. return;
  40. }
  41.  
  42. // dynamic memory allocation
  43. int *table;
  44. table = new int[iSize];
  45.  
  46. // initiating and displaying values
  47. for (int i = 0; i < iSize; i++) {
  48. table[i] = i + 5;
  49. cout << "table[" << i << "]: " << table[i] << endl;
  50. }
  51.  
  52. // memory deallocation
  53. delete []table;
  54. }
  55.  
  56.  
  57.  
  58.  
  59. // 2
  60. bool b_alloc_table_2_dim(int ***piTable, int iSizeX, int iSizeY) {
  61.  
  62. // checking for invalid arguments
  63. if (iSizeX <= 0 || iSizeY <= 0)
  64. {
  65. cout << "Sorry, invalid arguments" << endl;
  66. return false;
  67. }
  68.  
  69. // allocate memory
  70. *piTable = new int*[iSizeX];
  71. for (int i = 0; i < iSizeX; i++) {
  72. (*piTable)[i] = new int[iSizeY];
  73. }
  74.  
  75. cout << "\nMemory allocated" << endl;
  76. return true;
  77. }
  78.  
  79.  
  80.  
  81. // 3
  82. bool b_dealloc_table_2_dim(int ***piTable, int iSizeX, int iSizeY) {
  83.  
  84. // checking for invalid arguments
  85. if (iSizeX <= 0 || iSizeY <= 0) {
  86. cout << "Sorry, invalid arguments" << endl;
  87. return false;
  88. }
  89.  
  90. // deallocatin memory
  91. for (int i = 0; i < iSizeX; i++) {
  92. delete (*piTable)[i];
  93. }
  94. delete* piTable;
  95.  
  96. cout << "Memory dallocated" << endl;
  97. return true;
  98. }
  99. // ?? not sure about that below v ??
  100. // it cannot, because 2D arrays allocated dynamically are like linked lists, we dont know the size
  101. // so we wouldn't be able to deallocate memory
  102.  
  103.  
  104. // 1 introuction
  105. // 2 hom1 presentation
  106. // 3 hom1 grading hom2 presentation
  107. // 4 hom2 grading hom3 presentation
  108.  
  109.  
  110. // 4.5 is minimum to rewrite grade from lebas to lecture
  111.  
  112.  
  113.  
  114. // additional
  115. void v_alloc_table_char(int iSize) {
  116.  
  117. // checking for invalid size argument
  118. if (iSize <= 0) {
  119. cout << "Sorry, wrong size!" << endl;
  120. return;
  121. }
  122.  
  123. // dynamic memory allocation
  124. char** table;
  125. table = new char*[iSize];
  126.  
  127. for (int i = 0, move = 0; i < iSize; i++, move += iSize) {
  128. table[i] = new char[iSize];
  129.  
  130. // filling with values + output
  131. for (int j = 0; j < iSize; j++) {
  132. table[i][j] = j + move + 65;
  133. cout << table[i][j] << " ";
  134. }
  135. cout << endl;
  136. }
  137.  
  138. // memory deallocation
  139. for (int i = 0; i < iSize; i++) {
  140. delete table[i];
  141. }
  142. delete[] table;
  143. }
  144. /*
  145. instead of function which allocates in dynamic way 1-d int array
  146. we need to do it for 2-d char array
  147. also we need to display it
  148. fill array with values
  149. deallocate array
  150. check if size is valid (<= 0)
  151. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement