192k views
2 votes
Write a function that takes a matrix with m rows and n columns as input. The function should return the sum of all even numbers in the matrix. Recall that rem(n,2) returns 1 for odd and 0 for even.

a) sumEven(matrix)

b) evenSum(matrix)

c) matrixSum()

d) calculateEvenSum(matrix)

1 Answer

2 votes

Final answer:

The function to return the sum of all even numbers in a matrix with m rows and n columns is sumEven(matrix). The function should iterate through each element in the matrix and use the rem(n,2) function to determine if the number is even or odd. If the number is even, it should be added to a running total.

Step-by-step explanation:

The correct function to return the sum of all even numbers in a matrix with m rows and n columns is a) sumEven(matrix). The function should iterate through each element in the matrix and use the rem(n,2) function to determine if the number is even or odd. If the number is even, it should be added to a running total. Once all elements have been checked, the function should return the final sum. Here is an example implementation in Python:

def sumEven(matrix):
total = 0
for row in matrix:
for num in row:
if rem(num, 2) == 0:
total += num
return total

User Hamid Parchami
by
7.2k points