Answered by:
"SADDLE POINTS"?
Question

okay, i hv finished all my exercises except this one..this one makes me sooo urggh.. im so confused!!
please help me one more time..thanks, this is in c# btw :))
Write a program that search for the "saddle point" in a 5 by 5 array of integers. A saddle point in a cell whose value is greater than or equal to any in its row, and less than or equal to any in its column. There may be more than one saddle point in the array. Print out the coordinates of any saddle points your program finds. Print out "No Saddle Points" if there are none. Use the following values as your initial values of array
34 21 32 41 25
14 42 43 14 31
64 45 62 42 23
23 15 51 31 25
21 52 22 13 23
 Edited by endor12 Tuesday, September 6, 2011 12:31 PM
Tuesday, September 6, 2011 12:28 PM
Answers

Here is a working code:
static void Main(string[] args) { int[,] myArray ={ {34 , 21 , 32, 41, 25}, {14 , 42, 43, 14, 31}, {64, 45, 62, 42, 23}, {23, 15, 51 , 31 , 25}, {21, 25 , 30 , 13 , 23} }; bool NoSaddle = true; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (IsGreaterThanAnyNumberInRow(myArray, i, j)) continue; else if (IsLessThanAnyNumberInColumn(myArray, i, j)) continue; NoSaddle = false; Console.WriteLine("{0} in Row {1} Col {2} Is a saddle", myArray[i, j], i + 1, j + 1); } } if (NoSaddle) Console.WriteLine("There was no saddle point in the array"); Console.ReadLine(); } private static bool IsLessThanAnyNumberInColumn(int[,] myArray, int i, int j) { return myArray[i, j] > myArray[0, j]  myArray[i, j] > myArray[1, j]  myArray[i, j] > myArray[2, j]  myArray[i, j] > myArray[3, j]  myArray[i, j] > myArray[4, j]; } private static bool IsGreaterThanAnyNumberInRow(int[,] myArray, int i, int j) { return myArray[i, j] < myArray[i, 0]  myArray[i, j] < myArray[i, 1]  myArray[i, j] < myArray[i, 2]  myArray[i, j] < myArray[i, 3]  myArray[i, j] < myArray[i, 4]; }
BTW  In the array that you have provided there is no saddle point.I changed the values in order to have one.
So check both arrays.
Noam B.
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
Tuesday, September 6, 2011 1:49 PM
All replies

Hi,
ok, you can store teh values in a 2 dimensional array. So a point is a saddle point, when all these checks are true:
bool IsSaddlePoint(int[,] Points, int x, int y)
{
return (Compare(Points, x, y, x, y1) &&
Compare(Points, x, y, x, y+1) &&
Compare(Points, x, y, x1, y) &&
Compare(Points, x, y, x+1, y));
}
bool Compare (int[,] Points, int x1, int y1, int x2, int y2)
{
if ( (x1<0)  (y1<0)  (x2<0)  (y2<0)  (x1>=5)  (x2>=5)  (y1>=5)  (y2>=5) )
return true;
return Points[x1,y1] > Points[x2,y2];
}So I compare the point with the points above, bolow and to the sides. The compare is simply a compare of the given values. If a point does not exist, the result is also true.
(So you can compare Field [0,0] to [0,1]  there is no point [0,1] so we have to ignore it. And we put together the results be an logical and operation and a neutral element in there is true:
true && something = something.)Hope that helped. code was never checked  just written out ofmy head.
Konrad
Tuesday, September 6, 2011 12:46 PM 
thank you ill try it
Tuesday, September 6, 2011 1:03 PM 
Here is a working code:
static void Main(string[] args) { int[,] myArray ={ {34 , 21 , 32, 41, 25}, {14 , 42, 43, 14, 31}, {64, 45, 62, 42, 23}, {23, 15, 51 , 31 , 25}, {21, 25 , 30 , 13 , 23} }; bool NoSaddle = true; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (IsGreaterThanAnyNumberInRow(myArray, i, j)) continue; else if (IsLessThanAnyNumberInColumn(myArray, i, j)) continue; NoSaddle = false; Console.WriteLine("{0} in Row {1} Col {2} Is a saddle", myArray[i, j], i + 1, j + 1); } } if (NoSaddle) Console.WriteLine("There was no saddle point in the array"); Console.ReadLine(); } private static bool IsLessThanAnyNumberInColumn(int[,] myArray, int i, int j) { return myArray[i, j] > myArray[0, j]  myArray[i, j] > myArray[1, j]  myArray[i, j] > myArray[2, j]  myArray[i, j] > myArray[3, j]  myArray[i, j] > myArray[4, j]; } private static bool IsGreaterThanAnyNumberInRow(int[,] myArray, int i, int j) { return myArray[i, j] < myArray[i, 0]  myArray[i, j] < myArray[i, 1]  myArray[i, j] < myArray[i, 2]  myArray[i, j] < myArray[i, 3]  myArray[i, j] < myArray[i, 4]; }
BTW  In the array that you have provided there is no saddle point.I changed the values in order to have one.
So check both arrays.
Noam B.
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
Tuesday, September 6, 2011 1:49 PM 
thank you..
Tuesday, September 6, 2011 2:00 PM