Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. // matrixx.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <limits.h>
  6. #include <vector>
  7. #include "matrixx.h"
  8. using namespace std;
  9.  
  10. void MinMaxValueOfMatrix(int rows, int columns, std::vector<std::vector<int>> &map);
  11. void FillMatrix(int rows, int columns, std::vector<std::vector<int>> &map);
  12. void PrintMatrix(int rows, int columns, std::vector<std::vector<int>> &map);
  13.  
  14. int main()
  15. {
  16. //Reading matrix size
  17. int rows, columns;
  18. cout << "Enter number of rows: ";
  19. cin >> rows;
  20. cout << endl;
  21. cout << "Enter number of columns: ";
  22. cin >> columns;
  23. cout << endl;
  24.  
  25. //Creating matrix with custom sizes
  26. std::vector< std::vector<int> > map;
  27. map.resize(rows); // rows
  28. for (int i = 0; i < rows; ++i)
  29. map[i].resize(columns); // in every row, create columns
  30. // this is equivalent to a ROWxCOLS array
  31.  
  32. //Filling values in matrix
  33. FillMatrix(rows, columns, map);
  34.  
  35. //Print matrix
  36. cout << "Your matrix looks like this: " << endl;
  37. PrintMatrix(rows, columns, map);
  38. cout << endl;
  39.  
  40. //Print min and max value of matrix
  41. MinMaxValueOfMatrix(rows, columns, map); return 0;
  42.  
  43. }
  44.  
  45. void MinMaxValueOfMatrix(int rows, int columns, std::vector<std::vector<int>> &map)
  46. {
  47. int maxValue = INT_MIN;
  48. int minValue = INT_MAX;
  49. int maxValueRow = 0;
  50. int maxValueCol = 0;
  51. int minValueRow = 0;
  52. int minValueCol = 0;
  53.  
  54. for (int i = 0; i < rows; i++) {
  55. for (int j = 0; j < columns; j++) {
  56.  
  57. int currentNumber = map[i][j];
  58.  
  59. if (currentNumber < minValue)
  60. {
  61. minValue = currentNumber;
  62. minValueRow = i;
  63. minValueCol = j;
  64.  
  65. }
  66. if (currentNumber > maxValue)
  67. {
  68. maxValue = currentNumber;
  69. maxValueRow = i;
  70. maxValueCol = j;
  71. }
  72. }
  73. }
  74. cout << endl;
  75.  
  76. printf("The min value is: %d. And its located at: Row[%d] Column[%d].\n", minValue, minValueRow + 1, minValueCol + 1); // прибавя не 1ца за да се брой по нормален за потребителя начин
  77. printf("The max value is: %d. And its located at: Row[%d] Column[%d].\n", maxValue, maxValueRow + 1, maxValueCol + 1);
  78.  
  79. }
  80.  
  81. void FillMatrix(int rows, int columns, std::vector<std::vector<int>> &map)
  82. {
  83. for (int i = 0; i < rows; i++) {
  84.  
  85. if (i == 0)
  86. {
  87. cout << "Enter the values for the first row (" << columns << " values) :" << endl;
  88. }
  89. else {
  90. cout << "Enter the values for the next row (" << columns << " values) :" << endl;
  91. }
  92.  
  93. for (int j = 0; j < columns; j++) {
  94. cin >> map[i][j];
  95. }
  96.  
  97. }
  98. }
  99.  
  100. void PrintMatrix(int rows, int columns, std::vector<std::vector<int>> &map)
  101. {
  102. for (int i = 0; i < rows; i++) {
  103. for (int j = 0; j < columns; j++) {
  104. cout << map[i][j] << " ";
  105. }
  106. cout << endl;
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement