Advertisement
janac

Locate a number in a range in a matrix

Dec 17th, 2021
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib> // rand, srand
  4. #include <ctime> // time
  5. using namespace std;
  6.  
  7. // Find a number in a range of numbers in a matrix.
  8.  
  9. // These numbers have already been assigned to the matrix.
  10. vector<int> used_numbers;
  11.  
  12. // The matrix to be filled randomly with numbers 1-225.
  13. 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} };
  14.  
  15. // This function returns true if the number is not
  16. // in the used numbers vector. Otherwise, it returns false.
  17. bool number_is_available(int number, vector<int> numbers)
  18. {
  19.     // Check each number in the vector.
  20.     for (size_t i = 0; i < numbers.size(); ++i)
  21.     {
  22.         // If the specified number is in the vector,
  23.         if (numbers[i] == number)
  24.             return false; // Return false.
  25.     }
  26.     // If the number is not in the used number vector,
  27.     // return true.
  28.     return true;
  29. }
  30.  
  31. // This function returns the next number to
  32. // assign in the matrix. Each number in the range
  33. // 1 - 225 can only be used once.
  34. int get_next_number_for_matrix()
  35. {
  36.     int random_number = 0; // A random number generated.
  37.     bool available = true; // Has this number been used already?
  38.     while (true) // Forever,
  39.     {
  40.         // Get a number in the range 1 - 225.
  41.         random_number = (rand() % 225) + 1;
  42.         // If the number has not been used in the matrix yet,
  43.         if (number_is_available(random_number, used_numbers))
  44.         {
  45.             // Add it to the used numbers list.
  46.             used_numbers.push_back(random_number);
  47.             return random_number; // Return the number.
  48.         }
  49.     }  
  50. }
  51.  
  52. // This function displays the 2-d matrix.
  53. void display_matrix()
  54. {
  55.     for (int i = 0; i < 15; ++i) // For each row,
  56.     {
  57.         for (int j = 0; j < 15; ++j) // For each item in the row,
  58.         {
  59.             // Display the number in the cell, then tab.
  60.             std::cout << matrix[i][j] << "\t";
  61.         }
  62.         // Go to the next line.
  63.         std::cout << '\n';
  64.     }
  65. }
  66.  
  67. int main()
  68. {
  69.     bool found = false; // Has the user's number been found?
  70.     srand(time(NULL)); // Prepare the random number generator.
  71.     int users_number = 0; // The number the user wants to find.
  72.     int row_number = 0; // The row location of the user's number.
  73.     int column_number = 0; // The column location of the user's number.
  74.  
  75.     // Fill the matrix.
  76.     for (int i = 0; i < 15; ++i) // For each row,
  77.     {
  78.         for (int j = 0; j < 15; ++j) // For each item in the row,
  79.         {
  80.             // Get a number that has not been used yet, and
  81.             // assign it to the cell.
  82.             matrix[i][j] = get_next_number_for_matrix();
  83.         }
  84.     }
  85.  
  86.     std::cout << "The 15-by-15 matrix has numbers 1 to 225.\n"
  87.         "What number would you like to locate? ";
  88.     cin >> users_number; // Get the number from the user to serach for.
  89.     // Validate the unput. While the input is not 1 - 225,
  90.     while ((users_number < 1) || (users_number > 225))
  91.     {
  92.         std::cout << "Please choose a number from 1 to 225. ";
  93.         cin >> users_number; // Get new input.
  94.     }
  95.  
  96.     // Find the number.
  97.     for (int i = 0; i < 15; ++i) // For each row,
  98.     {
  99.         for (int j = 0; j < 15; ++j) // For each item in the row,
  100.         {
  101.             // If the item in the row matches the user's number,
  102.             if (users_number == matrix[i][j])
  103.             {
  104.                 // Indicate the user's number has been found.
  105.                 found = true;
  106.                 // The row number is the row index + 1.
  107.                 row_number = i + 1;
  108.                 // The colum number is the column index + 1.
  109.                 column_number = j + 1;
  110.                 // Stop searching.
  111.                 break;
  112.             }
  113.         }
  114.         // If the number has been found, stop searching.
  115.         if (found == true) break;
  116.     }
  117.    
  118.     // Display the result.
  119.     std::cout << "Number " << users_number << " is located in\n"
  120.         "row " << row_number << ", column " << column_number
  121.         << '\n';
  122.    
  123.     std::cout << "Here's the randomly-filled matrix:\n\n";
  124.     display_matrix(); // Display the matrix.
  125.  
  126.     // End the program.
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement