230k views
5 votes
Write a function that returns the sum of all the elements in a specified column in a matrix using the following header:

User Glogic
by
8.2k points

1 Answer

4 votes

Answer:

Following are the code to this question:

def sumColumn(m, columnIndex):#defining a method sumColumn that accept two parameters

x = 0#defining a variable x that hold a value 0

for i in range(3): #defining a for loop for count and add values

x += m[i][columnIndex]#add value in x variable

return x# return sum value

a = []#defining a empty list

for i in range(3):#defining a for loop for input value

print("Enter a 3-by-4 matrix row for row "+str(i)+":",end="")#use print method to input value

a.append([])#use append method to add value in list

y = input()#input value from user end

y = y.split(" ")#using split method to beak list value

for j in range(4):#defining a for loop count list value

a[i].append(float(y[j]))#use list to add value float value

print(a)#print value

for i in range(4):#defining for loop add all value

t = sumColumn(a, i)#declare t variable to call sumColumn method

print("Sum of the elements for column",i,"is",t)#defining print method print t variable value

Output:

Please find the attached file.

Step-by-step explanation:

Please find the complete question in the attached file.

In the above-given program, a method "sumColumn" is declared, that uses two variable in its parameter, and an x variable is declared that use the for loop to add all list value and use a return variable to return its value.

In the next step, a for loop is declared, that use a list for input value and use split and append method and use another loop to add float value in the list.

In the next step, for loop is declared the defines the t variable and add all value in the list.

Write a function that returns the sum of all the elements in a specified column in-example-1
Write a function that returns the sum of all the elements in a specified column in-example-2
User Rhonda
by
8.4k points