PYTHON locate the largest element.. write the following method that returns the
ID: 3861604 • Letter: P
Question
PYTHON
locate the largest element.. write the following method that returns the location of the largest element in a two-dimensional array.
def locateLargest(a):
the return value is a one dimensional list that contains two elements. these two elements indicated the row and column indexes of the largest element in the two dimensional list. write a test program that prompts the user to enter a two dimensional list and displays the location of the largest element in the list.
PYTHON
Example: Enter the number of rows in the list: 3
enter a row: 23.5 35 2 10
enter a row: 4.5 3 45 3.5
enter a row: 35 44 5.5 11.6
the location of the largest element is at (1, 2)
Explanation / Answer
python code:
def locateLargest(a):
i = 0
j = 0
maxx = a[i][j]
for x in range(0,len(a)):
for y in range(0,len(a[0])):
if(a[x][y] > maxx):
maxx = a[x][y]
i = x
j = y
return [i,j]
print "Enter number of rows in the Matrix"
n = int(raw_input().strip())
Matrix = []
i = 1
while(i <= n):
print "Enter Row ", i
line = raw_input().strip()
l = [float(x) for x in line.strip().split(" ")]
Matrix.append(l)
i = i + 1
print "The location of the largest element is at", locateLargest(Matrix)
Sample Output:
Enter number of rows in the Matrix
3
Enter Row 1
1 2 3 4
Enter Row 2
2 3 4 5
Enter Row 3
1 5 7 8 4
The location of the largest element is at [2, 3]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.