fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class Table
  5. {
  6. public:
  7. Table(int row, int col)
  8. : mRow(row), mCol(col)
  9. {
  10. mTable.resize(row, std::vector<int>(col, 0));
  11. }
  12.  
  13. int Row() const { return mRow; }
  14. int Col() const { return mCol; }
  15. void SetEntry(int row, int col, int value) { mTable[row][col] = value; }
  16. int GetEntry(int row, int col) const { return mTable[row][col]; }
  17.  
  18. void Print() const
  19. {
  20. for (auto &rowVector : mTable)
  21. {
  22. std::cout << "|";
  23. for (auto &entry : rowVector)
  24. {
  25. std::cout << " " << entry;
  26. }
  27. std::cout << " |\n";
  28. }
  29. }
  30.  
  31. private:
  32. int mRow;
  33. int mCol;
  34. std::vector<std::vector<int>> mTable;
  35. };
  36.  
  37. void SetTableToRowColMultiplication(Table &table)
  38. {
  39. for (auto row=0; row<table.Row(); ++row)
  40. {
  41. for (auto col=0; col<table.Col(); ++col)
  42. {
  43. table.SetEntry(row, col, (row+1)*(col+1));
  44. }
  45. }
  46. }
  47.  
  48. int main()
  49. {
  50. Table t(9, 9);
  51. t.Print();
  52. std::cout << "======\n";
  53. SetTableToRowColMultiplication(t);
  54. t.Print();
  55. return 0;
  56. }
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
| 0 0 0 0 0 0 0 0 0 |
| 0 0 0 0 0 0 0 0 0 |
| 0 0 0 0 0 0 0 0 0 |
| 0 0 0 0 0 0 0 0 0 |
| 0 0 0 0 0 0 0 0 0 |
| 0 0 0 0 0 0 0 0 0 |
| 0 0 0 0 0 0 0 0 0 |
| 0 0 0 0 0 0 0 0 0 |
| 0 0 0 0 0 0 0 0 0 |
======
| 1 2 3 4 5 6 7 8 9 |
| 2 4 6 8 10 12 14 16 18 |
| 3 6 9 12 15 18 21 24 27 |
| 4 8 12 16 20 24 28 32 36 |
| 5 10 15 20 25 30 35 40 45 |
| 6 12 18 24 30 36 42 48 54 |
| 7 14 21 28 35 42 49 56 63 |
| 8 16 24 32 40 48 56 64 72 |
| 9 18 27 36 45 54 63 72 81 |