When prompted for a number of rows in a matrix, then prompted to enter the elements of those rows, I need to find the largest element in that matrix and return its location (row and column).
For example, if I enter 2 rows as follows:
[1 3 7] [4 8 1]
the location of the largest element would be (1, 1) meaning row index 1 and column index 1.
I have the following code, which semi works to get the location:
def main():
matrix = []
numRows = eval(input("Enter the number of rows in the list: "))
for i in range(numRows):
rows = input("Enter a row: ")
items = rows.split()
list = [eval(x) for x in items]
matrix.append(list)
return locateLargest(matrix)
def locateLargest(a):
for i in range(len(a)):
indexOfMaxRow = 0
maxRow = max(a[i])
for row in range(len(a)):
if max(a[row]) > maxRow:
maxRow = max(a[row])
indexOfMaxRow = row
for j in range(len(a)):
indexOfMaxColumn = 0
maxColumn = max(a[j])
for column in range(len(a)):
if max(a[column]) > maxColumn:
maxColumn = max(a[column])
indexOfMaxColumn = column
print("The location of the largest element is at (", indexOfMaxRow, ", ", indexOfMaxColumn, ")")
main()
I think my code is wrong under def locateLargest(a)
since everything works until the results are printed. Can anyone advise what is wrong with it? Thanks in advance for any input!