Advertisement
kaenan

C++ - Matrix

Nov 28th, 2017
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <vector>
  4. #include <fstream>
  5.  
  6. #define SUCCESS   (0)
  7. #define NOT_FOUND (404)
  8. #define FOUND     (-404)
  9.  
  10. #define NEWLINE std::cout << std::endl;
  11.  
  12. // Print matrix to stdout.
  13. template <typename type>
  14. void printmat(std::vector< std::vector<type> >&);
  15.  
  16. // Read matrix from stdin.
  17. template <typename type>
  18. void readmat(std::vector< std::vector<type> >&);
  19.  
  20. // Read matrix from a given fstream.
  21. template <typename type>
  22. void readmat(std::vector< std::vector<type> >&, std::fstream&);
  23.  
  24. // Binary search a matrix.
  25. template <typename type>
  26. int binS_matrix(std::vector< std::vector<type> >&, const type);
  27.  
  28. int main()
  29. {
  30.     std::fstream input;
  31.  
  32.     int rows, cols;
  33.  
  34.     /* Open file for reading. */
  35.     input.open("matrix.txt", std::ios::in);
  36.  
  37.     /* Read the number of lines and collums */
  38.     input >> rows;
  39.     input >> cols;
  40.  
  41.     if (input.peek() == EOF) {
  42.         std::cerr << "[Error] Not enough input."; NEWLINE;
  43.         exit(1);
  44.     }
  45.  
  46.     // The MATRIX
  47.     std::vector< std::vector<int> > mat(rows, std::vector<int>(cols));
  48.  
  49.     // Read the matrix from file.
  50.     readmat(mat, input);
  51.     input.close();
  52.  
  53.     int search_value;
  54.     std::cout << "Search for: ";
  55.     std::cin >> search_value;
  56.  
  57.     if ( binS_matrix(mat, search_value) == FOUND ) {
  58.         std::cout << "Found " << search_value << "!";
  59.     }
  60.     else {
  61.         std::cout << "Not found " << search_value << ".";
  62.     }
  63.     NEWLINE;
  64.  
  65.     return SUCCESS;
  66. }
  67.  
  68.  
  69.  
  70. template <typename type>
  71. int binS_matrix(std::vector< std::vector<type> >& mat, const type search_value)
  72. {
  73.     /* [Note] Could use auto. */
  74.     typename std::vector< std::vector<type> >::iterator row;
  75.     typename std::vector<type>::iterator col;
  76.  
  77.     // Variable used to shrink the vector horizontally.
  78.     unsigned int shrink_H = 0;
  79.  
  80.     for (
  81.         row = mat.begin();  
  82.         row < mat.end();
  83.         ++row
  84.         )
  85.     {
  86.         for(
  87.             col = row->end() - 1 - shrink_H;
  88.             col >= row->begin();
  89.             --col
  90.             )
  91.         {
  92.             if (search_value > *col) {
  93.                 break;  // skip current line = verical shrink
  94.             }
  95.             else if (search_value < *col) {
  96.                 ++shrink_H;
  97.             }
  98.             else {
  99.                 return FOUND; // found!
  100.             }
  101.         }
  102.     }
  103.  
  104.     /* Not found. */
  105.     return NOT_FOUND;
  106. }
  107.  
  108.  
  109.  
  110. template <typename type>
  111. void printmat(std::vector< std::vector<type> >& mat)
  112. {
  113.     NEWLINE;
  114.  
  115.     for (auto& row : mat) {
  116.         for (auto& elem : row) {
  117.             std::cout << elem << " ";
  118.         }
  119.         NEWLINE;
  120.     }
  121.  
  122.     NEWLINE;
  123. }
  124.  
  125. template <typename type>
  126. void readmat(std::vector< std::vector<type> >& mat)
  127. {
  128.     for (auto& row : mat) {
  129.         for (auto& elem : row)
  130.         {
  131.             std::cin >> elem;
  132.         }
  133.     }
  134. }
  135.  
  136. template <typename type>
  137. void readmat(std::vector< std::vector<type> >& mat, std::fstream& input)
  138. {
  139.     for (auto& row : mat) {
  140.         for (auto& elem : row)
  141.         {
  142.  
  143.             input >> elem;
  144.  
  145.             // C++ makes sense! :PPP
  146.             if (input.peek() == EOF) {
  147.                 std::cerr << "[Error] Not enough input."; NEWLINE;
  148.                 exit(1);
  149.             }
  150.            
  151.         }
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement