Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstdlib> // rand, srand
- #include <ctime> // time
- using namespace std;
- // Find a number in a range of numbers in a matrix.
- // These numbers have already been assigned to the matrix.
- vector<int> used_numbers;
- // The matrix to be filled randomly with numbers 1-225.
- int matrix[15][15] = { {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, 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, 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} };
- // This function returns true if the number is not
- // in the used numbers vector. Otherwise, it returns false.
- bool number_is_available(int number, vector<int> numbers)
- {
- // Check each number in the vector.
- for (size_t i = 0; i < numbers.size(); ++i)
- {
- // If the specified number is in the vector,
- if (numbers[i] == number)
- return false; // Return false.
- }
- // If the number is not in the used number vector,
- // return true.
- return true;
- }
- // This function returns the next number to
- // assign in the matrix. Each number in the range
- // 1 - 225 can only be used once.
- int get_next_number_for_matrix()
- {
- int random_number = 0; // A random number generated.
- bool available = true; // Has this number been used already?
- while (true) // Forever,
- {
- // Get a number in the range 1 - 225.
- random_number = (rand() % 225) + 1;
- // If the number has not been used in the matrix yet,
- if (number_is_available(random_number, used_numbers))
- {
- // Add it to the used numbers list.
- used_numbers.push_back(random_number);
- return random_number; // Return the number.
- }
- }
- }
- // This function displays the 2-d matrix.
- void display_matrix()
- {
- for (int i = 0; i < 15; ++i) // For each row,
- {
- for (int j = 0; j < 15; ++j) // For each item in the row,
- {
- // Display the number in the cell, then tab.
- std::cout << matrix[i][j] << "\t";
- }
- // Go to the next line.
- std::cout << '\n';
- }
- }
- int main()
- {
- bool found = false; // Has the user's number been found?
- srand(time(NULL)); // Prepare the random number generator.
- int users_number = 0; // The number the user wants to find.
- int row_number = 0; // The row location of the user's number.
- int column_number = 0; // The column location of the user's number.
- // Fill the matrix.
- for (int i = 0; i < 15; ++i) // For each row,
- {
- for (int j = 0; j < 15; ++j) // For each item in the row,
- {
- // Get a number that has not been used yet, and
- // assign it to the cell.
- matrix[i][j] = get_next_number_for_matrix();
- }
- }
- std::cout << "The 15-by-15 matrix has numbers 1 to 225.\n"
- "What number would you like to locate? ";
- cin >> users_number; // Get the number from the user to serach for.
- // Validate the unput. While the input is not 1 - 225,
- while ((users_number < 1) || (users_number > 225))
- {
- std::cout << "Please choose a number from 1 to 225. ";
- cin >> users_number; // Get new input.
- }
- // Find the number.
- for (int i = 0; i < 15; ++i) // For each row,
- {
- for (int j = 0; j < 15; ++j) // For each item in the row,
- {
- // If the item in the row matches the user's number,
- if (users_number == matrix[i][j])
- {
- // Indicate the user's number has been found.
- found = true;
- // The row number is the row index + 1.
- row_number = i + 1;
- // The colum number is the column index + 1.
- column_number = j + 1;
- // Stop searching.
- break;
- }
- }
- // If the number has been found, stop searching.
- if (found == true) break;
- }
- // Display the result.
- std::cout << "Number " << users_number << " is located in\n"
- "row " << row_number << ", column " << column_number
- << '\n';
- std::cout << "Here's the randomly-filled matrix:\n\n";
- display_matrix(); // Display the matrix.
- // End the program.
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement