1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
// Forms vectors which contains information about the smallest members of the row / column section and the biggest members rows / columns
void VectorsOfMinimumAndMaximum(vector<vector<double> > matrix, vector<double> &vector_max_column,
vector<double> &vector_min_column, vector <double> &vector_max_row, vector <double> &vector_min_row){
for(int i = 0; i < matrix.size(); i++){
for(int j = 0; j < matrix[0].size(); j++) {
double max_row(matrix[i][0]), min_row(matrix[i][0]);
double max_column(matrix[0][j]), min_column(matrix[0][j]);
for(int k = 0; k < matrix[0].size(); k++) {
if(matrix[i][k] > max_row) max_row = matrix[i][k];
if(matrix[i][k] < min_row) min_row = matrix[i][k];
}
for(int k = 0; k < matrix.size(); k++) {
if(matrix[k][j] > max_column) max_column = matrix[i][k];
if(matrix[k][j] < min_column) min_column = matrix[i][k];
}
vector_max_column.push_back(max_column);
vector_min_column.push_back(min_column);
vector_max_row.push_back(max_row);
vector_min_row.push_back(min_row);
}
}
}
//ova kratka funkcija ustvari broji sedla
int Saddle(vector<vector<double> > matrix){
for(int i=0; i<matrix.size(); i++)
if(matrix[i].size()!=matrix[i-1].size() && i>0 ){
throw -1;
}
int counter(0);
vector<double> vector_max_column;
vector<double> vector_min_column;
vector<double> vector_max_row;
vector<double> vector_min_row;
VectorsOfMinimumAndMaximum(matrix, vector_max_column, vector_min_column, vector_max_row, vector_min_row);
for(int i=0; i<matrix.size(); i++){
for(int j=0; j<matrix[0].size(); j++){
if ( (matrix[i][j]==vector_max_row[i] && matrix[i][j]==vector_min_column[j])
|| (matrix[i][j]==vector_max_column[j] && matrix[i][j]==vector_min_row[i]))
counter++;
}
}
if(counter<matrix.size()*matrix[0].size()){
for(int i=0; i<matrix[0].size(); i++){
for(int j=0; j<matrix.size(); j++){
if ( (matrix[j][i]==vector_max_row[j] && matrix[j][i]==vector_min_column[i])
|| (matrix[j][i]==vector_max_column[j] && matrix[j][i]==vector_min_row[i]))
counter++;
if(counter==matrix.size()*matrix[0].size())
break;
}
}
}
return counter;
}
int main(){
try{
int column,row;
// Collect data and create a matrix
cout << "Enter the number of rows of the matrix " << endl;
cin >> row;
cout << "Enter the number of columns of the matrix " << endl;
cin >> column;
vector<vector<double> > matrix(row, vector<double>(column) );
cout << "Enter elements of the matrix :" << endl;
// Loop for element inputs
for(int i=0; i<matrix.size(); i++)
for(int j=0; j<matrix[0].size(); j++){
cout << "Enter element (" << i+1 << "," << j+1 << ") :";
cin >> matrix[i][j];
}
Saddle(matrix);
//Print results
cout << "\n The entered matrix is :" << endl;
for(int i=0; i<matrix.size(); i++){
for(int j=0; j<matrix[0].size(); j++){
cout << setw(4) << matrix[i][j];
}
cout << endl;
}
int number_of_saddles(Saddle(matrix));
if(number_of_saddles==0)
cout << "This matrix does not have a saddle ";
else if(number_of_saddles==1)
cout << "The entered matrix has " << number_of_saddles << " saddle";
else
cout << "The entered matrix has " << number_of_saddles << " saddles";
}
catch(int g){
cout << "An error has occurred " << endl;
}
return 0;
}
|