For this lab, you will work with two-dimensional lists in Python. Do the following:

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter7: Arrays
Section7.1: One-dimensional Arrays
Problem 10E: (Electrical eng.) Write a program that specifies three one-dimensional arrays named current,...
icon
Related questions
Question
Lab 1 - 2D Lists
For this lab, you will work with two-dimensional lists in Python. Do the following:
1. [4 points] Write a function that returns the sum of all the elements in a specified column in a matrix using the following header:
def sumColumn(matrix, columnindex)
2. [2 points] Write a function display() that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line. Use a single
space to separate different values. Sample output from this function when it is called on a 3 X4 matrix is as follows:
2.5 3.0 4.0 1.5
1.5 4.0 2.0 7.5
3.5 1.0 1.0 2.5
3. [4 points] Given this partial code
def getMatrix(numRows, numColumns):
"returns a matrix with the specified number of rows and columns"
m = [] # the 2D list (i.e., matrix), initially empty
for row in range(numRows):
matrix_dimensions_string = str(numRows) + "-by-" + str(numColumns)
line = input("Enter a "+ matrix_dimensions_string + " matrix row for row "+ str(row) + ": ")
return m
write a function called getMatrix(numRows, numColumns) that returns a matrix with numRows rows and numColumns columns. The function reads the values
of the matrix from the user on a row by row basis. The values of each row must be entered on one line using a space to separate the values. Use loops in your
solution. A sample run of this function when it is caled as getMatrix(3, 4) to return a 3 by 4 matrix is as follows. Values shown in red are entered by the user:
Enter a 3-by-4 matrix row for row 0: 2.5 3 4 1.5
Enter a 3-by-4 matrix row for row 1: 1.5 4 2 7.5
Enter a 3-by-4 matrix row for row 2: 3.511 2.5
Transcribed Image Text:Lab 1 - 2D Lists For this lab, you will work with two-dimensional lists in Python. Do the following: 1. [4 points] Write a function that returns the sum of all the elements in a specified column in a matrix using the following header: def sumColumn(matrix, columnindex) 2. [2 points] Write a function display() that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line. Use a single space to separate different values. Sample output from this function when it is called on a 3 X4 matrix is as follows: 2.5 3.0 4.0 1.5 1.5 4.0 2.0 7.5 3.5 1.0 1.0 2.5 3. [4 points] Given this partial code def getMatrix(numRows, numColumns): "returns a matrix with the specified number of rows and columns" m = [] # the 2D list (i.e., matrix), initially empty for row in range(numRows): matrix_dimensions_string = str(numRows) + "-by-" + str(numColumns) line = input("Enter a "+ matrix_dimensions_string + " matrix row for row "+ str(row) + ": ") return m write a function called getMatrix(numRows, numColumns) that returns a matrix with numRows rows and numColumns columns. The function reads the values of the matrix from the user on a row by row basis. The values of each row must be entered on one line using a space to separate the values. Use loops in your solution. A sample run of this function when it is caled as getMatrix(3, 4) to return a 3 by 4 matrix is as follows. Values shown in red are entered by the user: Enter a 3-by-4 matrix row for row 0: 2.5 3 4 1.5 Enter a 3-by-4 matrix row for row 1: 1.5 4 2 7.5 Enter a 3-by-4 matrix row for row 2: 3.511 2.5
Enter a 3-by-4 matrix row for row 0: 2.5 3 4 1.5
Enter a 3-by-4 matrix row for row 1: 1.5 427.5
Enter a 3-by-4 matrix row for row 2: 3.511 2.5
Use the following function to test your code:
def main():
m = getMatrix(3, 4)
print("\nThe matrix is")
display(m)
print()
for col in range(len(m[0])):
print("Sum of elements for column %d is %.2f" % (col, sumColumn(m,col)))
main()
A sample program run is as follows:
Enter a 3-by-4 matrix row for row 0: 1 22 3.7869 8
Enter a 3-by-4 matrix row for row 1: 4 5.6543 3 3
Enter a 3-by-4 matrix row for row 2: 1111
The matrix is
1.0 22.0 3.7869 8.0
4.0 5.6543 3.0 3.0
1.0 1.0 1.0 1.0
Sum of elements for column 0 is 6.00
Sum of elements for column 1 is 28.65
Sum of elements for column 2 is 7.79
Sum of elements for column 3 is 12.00
Transcribed Image Text:Enter a 3-by-4 matrix row for row 0: 2.5 3 4 1.5 Enter a 3-by-4 matrix row for row 1: 1.5 427.5 Enter a 3-by-4 matrix row for row 2: 3.511 2.5 Use the following function to test your code: def main(): m = getMatrix(3, 4) print("\nThe matrix is") display(m) print() for col in range(len(m[0])): print("Sum of elements for column %d is %.2f" % (col, sumColumn(m,col))) main() A sample program run is as follows: Enter a 3-by-4 matrix row for row 0: 1 22 3.7869 8 Enter a 3-by-4 matrix row for row 1: 4 5.6543 3 3 Enter a 3-by-4 matrix row for row 2: 1111 The matrix is 1.0 22.0 3.7869 8.0 4.0 5.6543 3.0 3.0 1.0 1.0 1.0 1.0 Sum of elements for column 0 is 6.00 Sum of elements for column 1 is 28.65 Sum of elements for column 2 is 7.79 Sum of elements for column 3 is 12.00
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Mergesort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,