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.
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.
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. ;)
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.