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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education