Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- // function declarations
- void v_alloc_table_add_5(int);
- bool b_alloc_table_2_dim(int***, int, int);
- bool b_dealloc_table_2_dim(int***, int, int);
- void v_alloc_table_char(int iSize);
- int main()
- {
- // TASK 1
- //v_alloc_table_add_5(10);
- // TASK 2
- //int** pi_table;
- //b_alloc_table_2_dim(&pi_table, 5, 3);
- // TASK 3
- //b_dealloc_table_2_dim(&pi_table, 5, 3);
- v_alloc_table_char(5);
- cout << "\n\n---The end---\n";
- }
- // 1
- void v_alloc_table_add_5(int iSize) {
- // checking for invalid size argument
- if (iSize <= 0) {
- cout << "Sorry, wrong size!" << endl;
- return;
- }
- // dynamic memory allocation
- int *table;
- table = new int[iSize];
- // initiating and displaying values
- for (int i = 0; i < iSize; i++) {
- table[i] = i + 5;
- cout << "table[" << i << "]: " << table[i] << endl;
- }
- // memory deallocation
- delete []table;
- }
- // 2
- bool b_alloc_table_2_dim(int ***piTable, int iSizeX, int iSizeY) {
- // checking for invalid arguments
- if (iSizeX <= 0 || iSizeY <= 0)
- {
- cout << "Sorry, invalid arguments" << endl;
- return false;
- }
- // allocate memory
- *piTable = new int*[iSizeX];
- for (int i = 0; i < iSizeX; i++) {
- (*piTable)[i] = new int[iSizeY];
- }
- cout << "\nMemory allocated" << endl;
- return true;
- }
- // 3
- bool b_dealloc_table_2_dim(int ***piTable, int iSizeX, int iSizeY) {
- // checking for invalid arguments
- if (iSizeX <= 0 || iSizeY <= 0) {
- cout << "Sorry, invalid arguments" << endl;
- return false;
- }
- // deallocatin memory
- for (int i = 0; i < iSizeX; i++) {
- delete (*piTable)[i];
- }
- delete* piTable;
- cout << "Memory dallocated" << endl;
- return true;
- }
- // ?? not sure about that below v ??
- // it cannot, because 2D arrays allocated dynamically are like linked lists, we dont know the size
- // so we wouldn't be able to deallocate memory
- // 1 introuction
- // 2 hom1 presentation
- // 3 hom1 grading hom2 presentation
- // 4 hom2 grading hom3 presentation
- // 4.5 is minimum to rewrite grade from lebas to lecture
- // additional
- void v_alloc_table_char(int iSize) {
- // checking for invalid size argument
- if (iSize <= 0) {
- cout << "Sorry, wrong size!" << endl;
- return;
- }
- // dynamic memory allocation
- char** table;
- table = new char*[iSize];
- for (int i = 0, move = 0; i < iSize; i++, move += iSize) {
- table[i] = new char[iSize];
- // filling with values + output
- for (int j = 0; j < iSize; j++) {
- table[i][j] = j + move + 65;
- cout << table[i][j] << " ";
- }
- cout << endl;
- }
- // memory deallocation
- for (int i = 0; i < iSize; i++) {
- delete table[i];
- }
- delete[] table;
- }
- /*
- instead of function which allocates in dynamic way 1-d int array
- we need to do it for 2-d char array
- also we need to display it
- fill array with values
- deallocate array
- check if size is valid (<= 0)
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement