i confused how to print every greatest per rows in array 2d python. my code so far walking properly but i can't get the right result. the program wants output like:
input:
2 - for how much array need to create
2 -> for how much size array(rows default = 3)
1 2 3
4 5 6
3
0 0 0
2 3 4
5 6 7
so far i get this right on input. and output:
in array 1 is : 2(represent row) -> (4+5+6) is greatest than (1+2+3)
in array 2 is : 3 (represent row) -> (5+6+7) is greatest
only (in array 1 is: 2
(and so on depending on the number of arrays or matrices created))` which will be displayed on the screen i confused how to implementing that. this my code:
import sys
print("Enter the number of arrays:")
K = int(input())
array = []
for i in range(K):
print("Enter the number of rows and columns you want:")
rows = int(input())
columns = 3
matrix = []
print("Start entering the rows:")
for j in range(rows):
matrix.append(list(map(int, input().split())))
array.append(matrix)
rows2 = len(matrix)
column2 = len(matrix[0])
idx = -1
maxi = -sys.maxsize
for r in range(0, rows2):
rowtotal=0
for s in range(0, column2):
rowtotal=rowtotal+int(matrix[r][s])
if (rowtotal > maxi):
maxi = rowtotal
idx = i
where should i put print to get the output and do i have to loop again?. and what is the syntax for displaying the output?. thanks.
You do not need array
outside the for loop, since you just need to evaluate row-wise in matrix
.
As user inputs the row, you can already just calculate the sum of the row using sum()
. If that row's sum is bigger than the previously biggest sum (or sentinel value -999999), we set it as the biggest_row_sum
and remember the biggest_row_index
. At the end of the building of a full matrix, we just print out biggest_row_sum
and biggest_row_index
.
print("Enter the number of arrays:")
K = int(input())
for i in range(K):
print("Enter the number of rows you want:")
rows = int(input())
columns = 3
matrix = []
biggest_row_sum = -999999
biggest_row_index = -1
print("Start entering the rows:")
for j in range(rows):
this_row = list(map(int, input().split()))
matrix.append(this_row)
this_row_sum = sum(this_row)
if this_row_sum > biggest_row_sum:
biggest_row_index = j
biggest_row_sum = this_row_sum
print(matrix)
print("in array " + str(i+1) + " is : " + str(biggest_row_index+1))
Output:
Enter the number of arrays:
2
Enter the number of rows you want:
2
Using default number of columns = 3
Start entering the rows:
1 2 3
4 5 6
[[1, 2, 3], [4, 5, 6]]
in array 1 is : 2
Enter the number of rows you want:
3
Using default number of columns = 3
Start entering the rows:
0 0 0
2 3 4
5 6 7
[[0, 0, 0], [2, 3, 4], [5, 6, 7]]
in array 2 is : 3
Updated with OP's comment below
To only print out at the end of user input for all matrixes, we can simply store the final results into a list of tuples containing (matrix_no, matrix_greatest_sum)
in answers, then print out at the end:
print("Enter the number of arrays:")
K = int(input())
answers = []
for i in range(K):
print("Enter the number of rows you want:")
rows = int(input())
columns = 3
matrix = []
biggest_row_sum = -999999
biggest_row_index = -1
print("Start entering the rows:")
for j in range(rows):
this_row = list(map(int, input().split()))
matrix.append(this_row)
this_row_sum = sum(this_row)
if this_row_sum > biggest_row_sum:
biggest_row_index = j
biggest_row_sum = this_row_sum
answers.append((i+1, biggest_row_index+1))
# Print final answer
for matrix_no, matrix_greatest_sum in answers:
print("in array " + str(matrix_no) + " is : " + str(matrix_greatest_sum))
import sys
print("Enter the number of arrays:")
K = int(input())
array = []
for i in range(K):
print("Enter the number of rows and columns you want:")
rows = int(input())
columns = 3
matrix = []
print("Start entering the rows:")
for j in range(rows):
matrix.append(list(map(int, input().split())))
array.append(matrix) # we also do not need this line
# we do not need to save matrix to array just for calculating the max row
# we can do it inside the loop using the matrix variable
greatest_row = 0 # initially let's say the greatest row is the first row
current_sum = 0 # initial sum 0
for j in range(rows):
a_row_sum = sum(matrix[j])
if a_row_sum > current_sum:
current_sum = a_row_sum
greatest_row = j
# everything is 0 based indexed
# so we add one while outputting the result
print("in array {0} is : {1}".format(i + 1, greatest_row + 1))
output
Enter the number of arrays:
2
Enter the number of rows and columns you want:
2
Start entering the rows:
1 2 3
4 5 6
in array 1 is : 2
Enter the number of rows and columns you want:
3
Start entering the rows:
0 0 0
2 3 4
5 6 7
in array 2 is : 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.