204k views
4 votes
Assume you are given an int variable named sum and a 2-dimensional array of ints that has been created and assigned to a2d. Write some statements that compute the sum of all the elements in the entire 2-dimensional array and assign the value to sum.

1 Answer

3 votes

Answer:

array_2d = [[7, 20],[13, 4],[25, 679]]

total = 0

for row in array_2d:

total += sum(row)

print(total)

Step-by-step explanation:

* The code is in Python.

Since sum() is a built-in function in Python, I used total instead.

- Create a for loop that iterates through the 2d array, visits each row

- Add the values of the element in each row - using sum() function - to the total variable

- When the loop is done, print the total

User Morganwahl
by
3.8k points