#include <iostream>
using namespace std;
int* create2Darray(int rows, int columns);
void set(int *arr, int rows, int columns, int desired_row, int desired_column, int val);
int get(int *arr, int rows, int columns, int desired_row, int desired_column);
int main()
{
int *arr = create2Darray(10,10);
for (int i = 0; i < 10; i ++)
{
for(int j = 0; j < 10; j ++)
{
set(arr, 10, 10, i, j, i*10+j);
}
}
for (int i = 0; i < 10; i ++)
{
for(int j = 0; j < 10; j ++)
{
if(i == 0 && j == 9)
cout <<"[0][9]=";//here is the position of the garbage
cout << get(arr, 10, 10, i, j) << " ";
}
cout << endl;
}
set(arr, 10, 10, 10, 10, 10);
get(arr, 10, 10, 10, 10);
return 0;
}
int* create2Darray(int rows, int columns)
{
return new int(rows * columns);
}
void set(int *arr, int rows, int columns, int desired_row, int desired_column, int val)
{
if (desired_row<rows&&desired_column<columns&&desired_row>=0&&desired_column>=0)
{
arr[desired_row*columns+desired_column] = val;
}
else
{
cout << "Invalid index!" << endl;
return;
}
}
int get(int *arr, int rows, int columns, int desired_row, int desired_column)
{
if (desired_row<rows&&desired_column<columns&&desired_row>=0&&desired_column>=0)
{
return arr[desired_row*columns+desired_column];
}
else
{
cout << "Invalid index!" << endl;
system("pause");
exit(0);
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwppbnQqIGNyZWF0ZTJEYXJyYXkoaW50IHJvd3MsIGludCBjb2x1bW5zKTsKdm9pZCBzZXQoaW50ICphcnIsIGludCByb3dzLCBpbnQgY29sdW1ucywgaW50IGRlc2lyZWRfcm93LCBpbnQgZGVzaXJlZF9jb2x1bW4sIGludCB2YWwpOwppbnQgZ2V0KGludCAqYXJyLCBpbnQgcm93cywgaW50IGNvbHVtbnMsIGludCBkZXNpcmVkX3JvdywgaW50IGRlc2lyZWRfY29sdW1uKTsKaW50IG1haW4oKQp7CgogICAgaW50ICphcnIgPSBjcmVhdGUyRGFycmF5KDEwLDEwKTsKCiAgICBmb3IgKGludCBpID0gMDsgaSA8IDEwOyBpICsrKQogICAgewogICAgICAgIGZvcihpbnQgaiA9IDA7IGogPCAxMDsgaiArKykKICAgICAgICB7CiAgICAgICAgICAgIHNldChhcnIsIDEwLCAxMCwgaSwgaiwgaSoxMCtqKTsKICAgICAgICB9CiAgICB9CiAgICBmb3IgKGludCBpID0gMDsgaSA8IDEwOyBpICsrKQogICAgewogICAgICAgIGZvcihpbnQgaiA9IDA7IGogPCAxMDsgaiArKykKICAgICAgICB7CgkJCWlmKGkgPT0gMCAmJiBqID09IDkpCgkJCQljb3V0IDw8IlswXVs5XT0iOy8vaGVyZSBpcyB0aGUgcG9zaXRpb24gb2YgdGhlIGdhcmJhZ2UKICAgICAgICAgICAgY291dCA8PCBnZXQoYXJyLCAxMCwgMTAsIGksIGopIDw8ICIgIjsKCQkJCiAgICAgICAgfQoJCWNvdXQgPDwgZW5kbDsKICAgIH0KICAgIHNldChhcnIsIDEwLCAxMCwgMTAsIDEwLCAxMCk7CiAgICBnZXQoYXJyLCAxMCwgMTAsIDEwLCAxMCk7CiAgICByZXR1cm4gMDsKCgoKCgp9CgppbnQqIGNyZWF0ZTJEYXJyYXkoaW50IHJvd3MsIGludCBjb2x1bW5zKQp7CiAgICByZXR1cm4gbmV3IGludChyb3dzICogY29sdW1ucyk7Cn0KCnZvaWQgc2V0KGludCAqYXJyLCBpbnQgcm93cywgaW50IGNvbHVtbnMsIGludCBkZXNpcmVkX3JvdywgaW50IGRlc2lyZWRfY29sdW1uLCBpbnQgdmFsKQp7CiAgICBpZiAoZGVzaXJlZF9yb3c8cm93cyYmZGVzaXJlZF9jb2x1bW48Y29sdW1ucyYmZGVzaXJlZF9yb3c+PTAmJmRlc2lyZWRfY29sdW1uPj0wKQogICAgewogICAgICAgIGFycltkZXNpcmVkX3Jvdypjb2x1bW5zK2Rlc2lyZWRfY29sdW1uXSA9IHZhbDsKICAgIH0KCiAgICBlbHNlCiAgICB7CiAgICAgICAgY291dCA8PCAiSW52YWxpZCBpbmRleCEiIDw8IGVuZG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