SE Computer Engineering Practical 7 solution

Home » All Blogs » Python » Python assignments » SE Computer Engineering » SE Computer Engineering Practical 7 solution

SE Computer Engineering Practical 7 solution

Question:

Write a Python program that determines the location of a saddle point of the matrix if one exists.
An m x n matrix is said to have a saddle point if some entry a[i][j] is the smallest value in row i and the largest value in j.

Code:

m = int(input('Enter number of rows:'))
n = int(input('Enter number of columns'))

a = []
f = 0

# taking the array elements

print('Enter elements:')
for i in range(m):
   t = []
   for j in range(n):
       t.append(int(input()))
   a.append(t)

# printing the original matrix
print('The original matrix is :')
for i in range(m):
   for j in range(n):
       print(a[i][j], end='')
   print()

# finding the saddle point
for i in range(n):
   #find the minimum elements of row with its column index
   row_min = a[i][0]
   ci = 0
   for j in range(1, n):
       if row_min > a[i][j]:
           row_min = a[i][j]
           ci = j

   # checking the minimum element is the maximum element of the column or not
   k = 0
   while k < n:
       # the column index fixed
       if row_min < a[k][ci]:
           break
       k += 1
   #if saddle point is present
   if k == n:
       print('The saddle point is:',row_min)
       f = 1

# if saddle point is not found
if f == 0:
   print('Saddle point does not exist')

Output:

Enter number of rows:3
Enter number of columns3
Enter elements:
1
2
3
4
5
6
7
8
9
The original matrix is :
123
456
789
The saddle point is: 7

Process finished with exit code 0
Tech Amplifier Final Logo