saddles in a matrice

Hello friends,
Some time ago I had a homework, I needed to write a program that tracks and counts saddles of an imported matrix. Do not worry, I have done my homework. But I'm not satisfied, I know that there is much more elegant way of solving this task.

Here is my code, I expect the proposals, suggestions and corrections.

Thank you in advance
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;
}


Last edited on
You didn't need to use a vector. You could have created a 2d array. Even better, you could have created a 1d array and mapped both dimensions of the 2d array to the 1d array and then you'd need only one for loop.

Formula: (posin1d = columnnumber + rownumber*lengthoftherows)

Columnnumber and rownumber start from zero, but lengthoftherows represents the number of columns in one row, so it has to start from 1 for one column.

-Albatross
i had to use a vector<vector it was so given in the homework
Ah... all well.

By the way... before I go any further... what do you mean "saddles"?

-Albatross
a saddle of a matrice is an element that is the bigest element of it's row and the smalest of it's column, or the bigest in its column and the smalest in it's row

if you have
1 2 3
4 5 6
7 8 9
that this matrice has 2 saddles
number 3, and number 7

There is no possible way for me to explain in this ambiguous language what to do, except that the failsafe way is to compute four arrays of pointers to the smallest and biggest values in your matrix within a certain row and column and then check for intersections, and it looks like that is what you did.

Though you could have defined your variables outside of any functions and then your functions wouldn't need any arguments. ;)

-Albatross
hmmm that is not a bad idea, ,but still there is one more problem
if I enter something like

3 3 1
1 1 1
1 1 1

it gives me 6 instead of 7 saddles...
any idea why? i have looked the code for like 2 weeks and didn't find a thing...
Albatross wrote:
Though you could have defined your variables outside of any functions and then your functions wouldn't need any arguments. ;)


Are you trying to suggest he should have used global variables...?? Globals are evil.
firedraco
Globals are evil.


why is that?
http://www.learncpp.com/cpp-tutorial/42-global-variables/

They basically make your program hard to follow (like using goto incorrectly)
I'd be interested to know that as well as to why in this case global variables are evil. It's not like he's writing this code for use in another piece of code. If it were to be used in another file, then it might be a little bit more of a problem. However... it's not.

EDIT: I'm late.

EDIT2: Really? I argue a long list of arguments to a function make it harder to follow than via using global variables, at least for one-file cpp programs. Eh... we all have our preferences.

-Albatross
Last edited on
Topic archived. No new replies allowed.