Write a C program to find saddle point of a matrix.
#include <stdio.h>
#define ROW 10
#define COL 10
int main() {
int i, j, k, n, min, max, matrix[ROW][COL], pos[2][2];
/* get the order of the matrix from the user */
printf("Enter the order of the matrix:");
scanf("%d", &n);
/* get the entries for the input matrix from the user */
printf("Enter your entries for the input matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
Consider the below matrix
1 2 3
4 5 6
7 8 9
If any minimum value in a row is the maximum value in its corresponding column, then the position of that element is called saddle point of a matrix.
7 is the minimum value of third row and it is the maximum value of first column. So, the position[3,1] (third row and first column) is the saddle point for the given matrix.
1 2 3
4 5 6
7 8 9
If any minimum value in a row is the maximum value in its corresponding column, then the position of that element is called saddle point of a matrix.
7 is the minimum value of third row and it is the maximum value of first column. So, the position[3,1] (third row and first column) is the saddle point for the given matrix.
#define ROW 10
#define COL 10
int main() {
int i, j, k, n, min, max, matrix[ROW][COL], pos[2][2];
/* get the order of the matrix from the user */
printf("Enter the order of the matrix:");
scanf("%d", &n);
/* get the entries for the input matrix from the user */
printf("Enter your entries for the input matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
/* find the saddle points in the given matrix */
for (i = 0; i < n; i++) {
min = matrix[i][0];
for (j = 0; j < n; j++) {
if (min >= matrix[i][j]) {
min = matrix[i][j];
pos[0][0] = i;
pos[0][1] = j;
}
}
j = pos[0][1];
max = matrix[0][j];
for (k = 0; k < n; k++) {
if (max <= matrix[k][j]) {
max = matrix[i][j];
pos[1][0] = k;
pos[1][1] = j;
}
}
/* saddle point - minimum of a row and maximum of the column */
if (min == max) {
if (pos[0][0] == pos[1][0] &&
pos[0][1] == pos[1][1]) {
printf("Saddle point (%d, %d) : %d\n",
pos[0][0], pos[0][1], max);
}
}
}
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter the order of the matrix: 3
Enter your entries for the input matrix:
1 2 3
4 5 6
7 8 9
Saddle point (2, 0) : 7
Enter the order of the matrix: 3
Enter your entries for the input matrix:
1 2 3
4 5 6
7 8 9
Saddle point (2, 0) : 7
thank you
ReplyDeletecan u give brief explanation of steps...
ReplyDeleteIS THIS A TWO DIMENSIONAL ARRAY
ReplyDeleteExplanation please.
ReplyDeletewaste its uselesss
ReplyDeletewaste its uselesss
ReplyDeleteplz upload simple 1
ReplyDeleteNot understand
ReplyDeleteSorting Searching Codes about c programming
ReplyDeleteIn this case , can we check for a No saddle point condition ?
ReplyDeletelogical errors
ReplyDelete