fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3. int* create2Darray(int rows, int columns);
  4. void set(int *arr, int rows, int columns, int desired_row, int desired_column, int val);
  5. int get(int *arr, int rows, int columns, int desired_row, int desired_column);
  6. int main()
  7. {
  8.  
  9. int *arr = create2Darray(10,10);
  10.  
  11. for (int i = 0; i < 10; i ++)
  12. {
  13. for(int j = 0; j < 10; j ++)
  14. {
  15. set(arr, 10, 10, i, j, i*10+j);
  16. }
  17. }
  18. for (int i = 0; i < 10; i ++)
  19. {
  20. for(int j = 0; j < 10; j ++)
  21. {
  22. if(i == 0 && j == 9)
  23. cout <<"[0][9]=";//here is the position of the garbage
  24. cout << get(arr, 10, 10, i, j) << " ";
  25.  
  26. }
  27. cout << endl;
  28. }
  29. set(arr, 10, 10, 10, 10, 10);
  30. get(arr, 10, 10, 10, 10);
  31. return 0;
  32.  
  33.  
  34.  
  35.  
  36.  
  37. }
  38.  
  39. int* create2Darray(int rows, int columns)
  40. {
  41. return new int(rows * columns);
  42. }
  43.  
  44. void set(int *arr, int rows, int columns, int desired_row, int desired_column, int val)
  45. {
  46. if (desired_row<rows&&desired_column<columns&&desired_row>=0&&desired_column>=0)
  47. {
  48. arr[desired_row*columns+desired_column] = val;
  49. }
  50.  
  51. else
  52. {
  53. cout << "Invalid index!" << endl;
  54. return;
  55. }
  56. }
  57.  
  58. int get(int *arr, int rows, int columns, int desired_row, int desired_column)
  59. {
  60. if (desired_row<rows&&desired_column<columns&&desired_row>=0&&desired_column>=0)
  61. {
  62. return arr[desired_row*columns+desired_column];
  63. }
  64.  
  65. else
  66. {
  67. cout << "Invalid index!" << endl;
  68. system("pause");
  69. exit(0);
  70. }
  71. }
  72.  
Success #stdin #stdout #stderr 0s 3412KB
stdin
Standard input is empty
stdout
0 1 2 3 4 5 6 7 8 [0][9]=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 53 54 55 56 57 58 59 
60 61 62 63 64 65 66 67 68 69 
70 71 72 73 74 75 76 77 78 79 
80 81 82 83 84 85 86 87 88 89 
90 91 92 93 94 95 96 97 98 99 
Invalid index!
Invalid index!
stderr
sh: 1: pause: not found