1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
// Modified from http://stackoverflow.com/questions/15908632/returning-a-two-dimensional-array-from-a-function-in-c
#include<iostream>
int** createTable(int &, int &);
void deleteTable(int**, int rows);
void printTable(int**, int rows, int);
int main()
{
int rows = 0;
int cols = 0;
int** table = createTable(rows, cols);
printTable(table, rows, cols);
deleteTable(table, rows);
return 0;
}
int** createTable(int &r, int &c)
{
std::cout << "Enter rows cols:";
std::cin >> r >> c;
int** table = new int*[r];
for(int i = 0; i < r; i++) {
table[i] = new int[c];
for(int j = 0; j < c; j++)
{
table[i][j] = (i+j);
}// sample set value;
}
return table;
}
void deleteTable(int** table, int rows){
if(table){
for(int i = 0; i < rows; i++){ if(table[i]){ delete[] table[i]; } }
delete[] table;
}
}
void printTable(int** table, int rows, int columns){
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
printf("(%d,%d) -> %d\n", i, j, table[i][j]);
}
}
}
| |