Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // matrixx.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <limits.h>
- #include <vector>
- #include "matrixx.h"
- using namespace std;
- void MinMaxValueOfMatrix(int rows, int columns, std::vector<std::vector<int>> &map);
- void FillMatrix(int rows, int columns, std::vector<std::vector<int>> &map);
- void PrintMatrix(int rows, int columns, std::vector<std::vector<int>> &map);
- int main()
- {
- //Reading matrix size
- int rows, columns;
- cout << "Enter number of rows: ";
- cin >> rows;
- cout << endl;
- cout << "Enter number of columns: ";
- cin >> columns;
- cout << endl;
- //Creating matrix with custom sizes
- std::vector< std::vector<int> > map;
- map.resize(rows); // rows
- for (int i = 0; i < rows; ++i)
- map[i].resize(columns); // in every row, create columns
- // this is equivalent to a ROWxCOLS array
- //Filling values in matrix
- FillMatrix(rows, columns, map);
- //Print matrix
- cout << "Your matrix looks like this: " << endl;
- PrintMatrix(rows, columns, map);
- cout << endl;
- //Print min and max value of matrix
- MinMaxValueOfMatrix(rows, columns, map); return 0;
- }
- void MinMaxValueOfMatrix(int rows, int columns, std::vector<std::vector<int>> &map)
- {
- int maxValue = INT_MIN;
- int minValue = INT_MAX;
- int maxValueRow = 0;
- int maxValueCol = 0;
- int minValueRow = 0;
- int minValueCol = 0;
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- int currentNumber = map[i][j];
- if (currentNumber < minValue)
- {
- minValue = currentNumber;
- minValueRow = i;
- minValueCol = j;
- }
- if (currentNumber > maxValue)
- {
- maxValue = currentNumber;
- maxValueRow = i;
- maxValueCol = j;
- }
- }
- }
- cout << endl;
- printf("The min value is: %d. And its located at: Row[%d] Column[%d].\n", minValue, minValueRow + 1, minValueCol + 1); // прибавя не 1ца за да се брой по нормален за потребителя начин
- printf("The max value is: %d. And its located at: Row[%d] Column[%d].\n", maxValue, maxValueRow + 1, maxValueCol + 1);
- }
- void FillMatrix(int rows, int columns, std::vector<std::vector<int>> &map)
- {
- for (int i = 0; i < rows; i++) {
- if (i == 0)
- {
- cout << "Enter the values for the first row (" << columns << " values) :" << endl;
- }
- else {
- cout << "Enter the values for the next row (" << columns << " values) :" << endl;
- }
- for (int j = 0; j < columns; j++) {
- cin >> map[i][j];
- }
- }
- }
- void PrintMatrix(int rows, int columns, std::vector<std::vector<int>> &map)
- {
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < columns; j++) {
- cout << map[i][j] << " ";
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement