Question

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

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

  1. 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. 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 X 4 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. 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.5 1 1 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: 1 1 1 1

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

The format of the input and output of your program must be as in the sample run.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code :

#display function

def display(m):

#for each row in m

for i in range(len(m)):

#foreach column in m

for j in range(len(m[i])):

print(m[i][j], end=' ')

print()

#sumColumn function

def sumColumn(m,col):

#initialize colsum to 0

colsum = 0

#for each row and element at col

for i in range(len(m)):

colsum = colsum + m[i][col]

return colsum

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) + ": ")

#splitting line from the input given with space (' ')

nums = line.split(' ')

#nums is a list of strings so converting it to float and appending to m

m.append(list(map(float, nums)))

return m

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()

Add a comment
Know the answer?
Add Answer to:
For this lab, you will work with two-dimensional lists in Python. Do the following: Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Write a method that returns the sum of all the elements in a specified column...

    JAVA Write a method that returns the sum of all the elements in a specified column in a matrix using the following header: public static double sumColumn(double [][] m, int columnIndex) Write another method that returns the sum of all the elements in a specified row in a matrix using the following header: public static double sumRow( double [][] m, int rowIndex) Write a test program that reads a 3-by-4 matrix and displays the sum of each column and sum...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • Must be in Python(please have a screen shot on these solution code) 11.5 (Algebra: add two...

    Must be in Python(please have a screen shot on these solution code) 11.5 (Algebra: add two matrices) Write a function to add two matrices. The header of the function is def addMatrix(a, b): In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let c be the resulting matrix. Each element Cij is aij + bij. For example, for two 3 X 3 matrices a and b, c is...

  • Write a program that reads a matrix from the keyboard and displays the summations of all...

    Write a program that reads a matrix from the keyboard and displays the summations of all its rows on the screen. The size of matrix (i.e. the number of rows and columns) as well as its elements are read from the keyboard. A sample execution of this program is illustrated below: Enter the number of rows of the matrix: 3 Enter the number of columns of the matrix: 4 Enter the element at row 1 and chd umn 1: 1...

  • (Markov matrix) An n by n matrix is called a positive Markov matrix if each element...

    (Markov matrix) An n by n matrix is called a positive Markov matrix if each element is positive and the sum of the elements in each column is 1. Write the following function to check whether a matrix is a Markov matrix: def isMarkovMatrix(m): Write a test program that prompts the user to enter a 3 by 3 matrix of numbers and tests whether it is a Markov matrix. Note that the matrix is entered by rows and the numbers...

  • please use eclipse the sample output is incorrect CPS 2231 Chapter 8 Lab 1 Spring 2019...

    please use eclipse the sample output is incorrect CPS 2231 Chapter 8 Lab 1 Spring 2019 1. Write a class, SumOrRows. The main method will do the following methods: a. Call a method, createAndFillArray which: defines a 2 dim array of 3 rows and 4 columns, prompts the user for values and stores the values in the array. b. Calls a method, printArray, which: prints the Array as shown below c. Cails a method, calcSums, which: returns à 1 dimensional...

  • FOR PYTHON: Write a function findMinRow() that takes a two-dimensional list of numbers as a parameter....

    FOR PYTHON: Write a function findMinRow() that takes a two-dimensional list of numbers as a parameter. It returns the index of the minimum row in the list. The minimum row is the row with the smallest sum of elements. If the list has multiple rows that achieve the minimum value, the last such row should be returned. If the list is empty the function should return -1. The following shows several sample runs of the function: Python 3.4.1 Shell File...

  • PYTHON 3: An n x n matrix forms a magic square if the following conditions are...

    PYTHON 3: An n x n matrix forms a magic square if the following conditions are met: 1. The elements of the matrix are numbers 1,2,3, ..., n2 2. The sum of the elements in each row, in each column and in the two diagonals is the same value. Question: Complete the function that tets if the given matrix m forms a magic square. def is_square(m): '''2d-list => bool Return True if m is a square matrix, otherwise return False...

  • S.A vector IS givell by [5, 17,-3, 8, 0,-7, 12, 15, 20,-6, 6, 4,-7, 16]. Write...

    S.A vector IS givell by [5, 17,-3, 8, 0,-7, 12, 15, 20,-6, 6, 4,-7, 16]. Write a program as a 3 or 5, and, raises to the power of 3 the elements that are script tile that doubles the elements that are positive and are divisible by negative but greater than -5. following values. The value of each element in the first row is the number of the 6. Write a program in a script file that creates an matrix...

  • C Language Question

    Write a program to calculate the row averages of a 4x4 matrix. In main program get the elements of a 4x4 matrix and display the matrix, then call the function “average” to calculate the row averages. Function “average” will find the average of rows of a 4x4 matrix and store the results into a 1-dimensional array and return it as parameter. Main program should display the row averages which are greater than 5.0 returned by the function “average”. Use c language  Enter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT