janac

Diagram locations of digits in a 2-d array

Dec 7th, 2021 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // Print nine separate diagrams of
  6. // locations of each digit 1 - 9 in
  7. // a matrix.
  8. // Numbers in left and top headings are
  9. // multiplied, then modulo 9 to fill the cells.
  10. // Ex: (5 x 4) mod 9 = 2
  11. // 0 1 2 3 4 5 6 7 8 9
  12. // 1 1 2 3 4 5 6 7 8 9
  13. // 2 2 4 6 8 1 3 5 7 9
  14. // 3 3 6 9 3 6 9 3 6 9
  15. // 4 4 8 3 7 2 6 1 5 9
  16. // 5 5 1 6 2 7 3 8 4 9
  17. // 6 6 3 9 6 3 9 6 3 9
  18. // 7 7 5 3 1 8 6 4 2 9
  19. // 8 8 7 6 5 4 3 2 1 9
  20. // 9 9 9 9 9 9 9 9 9 9
  21.  
  22. const int ROWS = 10; // Number of rows.
  23. const int COLUMNS = 10; // Number of columns.
  24. // Printed when the number is there.
  25. const char dot = '@';
  26. // Printed when the number is not there.
  27. const char space = '.';
  28. // Indent from the edge of the console window.
  29. const string indent_3 = "   ";
  30.  
  31. // The 2-d array.
  32. int matrix[ROWS][COLUMNS] = { {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
  33.     {1, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {2, 2, 4, 6, 8, 1, 3, 5, 7, 9},
  34.     {3, 3, 6, 9, 3, 6, 9, 3, 6, 9}, {4, 4, 8, 3, 7, 2, 6, 1, 5, 9},
  35.     {5, 5, 1, 6, 2, 7, 3, 8, 4, 9}, {6, 6, 3, 9, 6, 3, 9, 6, 3, 9},
  36.     {7, 7, 5, 3, 1, 8, 6, 4, 2, 9}, {8, 8, 7, 6, 5, 4, 3, 2, 1, 9},
  37.     {9, 9, 9, 9, 9, 9, 9, 9, 9, 9} };
  38.  
  39. // Show the locations of a specific digit.
  40. void print_digit_location(int arr[ROWS][COLUMNS], int number)
  41. {
  42.     // For each row starting with index 1,
  43.     // (ignore the top row)
  44.     for (int i = 1; i < ROWS; ++i)
  45.     {
  46.         // Start the diagram three spaces from
  47.         // the edge of the window.
  48.         cout << indent_3;
  49.  
  50.         // For each column starting with index 1,
  51.         for (int j = 0;  j < COLUMNS; ++j)
  52.         {
  53.             if (j != 0) // Ignore the left column.
  54.             {
  55.                 // If the specified number is
  56.                 // at this location,
  57.                 if (arr[i][j] == number)
  58.                 {
  59.                     // Print a dot character.
  60.                     cout << dot;
  61.                 }
  62.                 else // If it's not at this
  63.                     // location,
  64.                 {
  65.                     // Print a space character.
  66.                     cout << space;
  67.                 }
  68.             }  
  69.         }
  70.         cout << endl; // Go to the next line.
  71.     }
  72. }
  73.  
  74. int main()
  75. {
  76.     cout << "These are the patterns created by digits in "
  77.         "the array.\n";
  78.  
  79.     for (int i = 1; i <= 9; ++i) // For each digit,
  80.     {
  81.         // Print a heading
  82.         cout << "\nDigit " << i << "\n\n";
  83.         // Print diagram of all locations.
  84.         print_digit_location(matrix, i);
  85.         cout << "\n\n"; // Add blank lines below
  86.     }
  87.  
  88.     // End the program.
  89.     return 0;
  90. }
Add Comment
Please, Sign In to add comment