Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- // Print nine separate diagrams of
- // locations of each digit 1 - 9 in
- // a matrix.
- // Numbers in left and top headings are
- // multiplied, then modulo 9 to fill the cells.
- // Ex: (5 x 4) mod 9 = 2
- // 0 1 2 3 4 5 6 7 8 9
- // 1 1 2 3 4 5 6 7 8 9
- // 2 2 4 6 8 1 3 5 7 9
- // 3 3 6 9 3 6 9 3 6 9
- // 4 4 8 3 7 2 6 1 5 9
- // 5 5 1 6 2 7 3 8 4 9
- // 6 6 3 9 6 3 9 6 3 9
- // 7 7 5 3 1 8 6 4 2 9
- // 8 8 7 6 5 4 3 2 1 9
- // 9 9 9 9 9 9 9 9 9 9
- const int ROWS = 10; // Number of rows.
- const int COLUMNS = 10; // Number of columns.
- // Printed when the number is there.
- const char dot = '@';
- // Printed when the number is not there.
- const char space = '.';
- // Indent from the edge of the console window.
- const string indent_3 = " ";
- // The 2-d array.
- int matrix[ROWS][COLUMNS] = { {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
- {1, 1, 2, 3, 4, 5, 6, 7, 8, 9}, {2, 2, 4, 6, 8, 1, 3, 5, 7, 9},
- {3, 3, 6, 9, 3, 6, 9, 3, 6, 9}, {4, 4, 8, 3, 7, 2, 6, 1, 5, 9},
- {5, 5, 1, 6, 2, 7, 3, 8, 4, 9}, {6, 6, 3, 9, 6, 3, 9, 6, 3, 9},
- {7, 7, 5, 3, 1, 8, 6, 4, 2, 9}, {8, 8, 7, 6, 5, 4, 3, 2, 1, 9},
- {9, 9, 9, 9, 9, 9, 9, 9, 9, 9} };
- // Show the locations of a specific digit.
- void print_digit_location(int arr[ROWS][COLUMNS], int number)
- {
- // For each row starting with index 1,
- // (ignore the top row)
- for (int i = 1; i < ROWS; ++i)
- {
- // Start the diagram three spaces from
- // the edge of the window.
- cout << indent_3;
- // For each column starting with index 1,
- for (int j = 0; j < COLUMNS; ++j)
- {
- if (j != 0) // Ignore the left column.
- {
- // If the specified number is
- // at this location,
- if (arr[i][j] == number)
- {
- // Print a dot character.
- cout << dot;
- }
- else // If it's not at this
- // location,
- {
- // Print a space character.
- cout << space;
- }
- }
- }
- cout << endl; // Go to the next line.
- }
- }
- int main()
- {
- cout << "These are the patterns created by digits in "
- "the array.\n";
- for (int i = 1; i <= 9; ++i) // For each digit,
- {
- // Print a heading
- cout << "\nDigit " << i << "\n\n";
- // Print diagram of all locations.
- print_digit_location(matrix, i);
- cout << "\n\n"; // Add blank lines below
- }
- // End the program.
- return 0;
- }
Add Comment
Please, Sign In to add comment