Question

In: Computer Science

Write a Python program which adds up columns and rows of given table as shown in...

Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.):

Input number of rows/columns (0 to exit)

4

Input cell value:

25 69 51 26

68 35 29 54

54 57 45 63

61 68 47 59

Result:

25 69 51 26 171

68 35 29 54 186

54 57 45 63 219

61 68 47 59 235

208 229 172 202 811

Input number of rows/columns (0 to exit)

Solutions

Expert Solution

Here, int the code , we have declared an double dimenstional array matrix startingly assigned 0 to each value .

We have used two loop for i and j index to input values from console

In length variable , we have stored rows/column count

After inputting values , we have again used two loop for i and j index to calculate sum of all rows and columns

Then after that, again loop is used to count sum of last column which is already a sum of all rows

Then we have printed the entered array in console

And , then printed the summed array in console

____________________________________

Please find below code in Python :


length=int(input("Input number of rows/columns: "))

matrix=[[0 for i in range(length+1)] for j in range(length+1)]

for i in range (length):

for j in range (length):
value= int(input("Enter value for row : "))
matrix[i][j]=value

colCount=0   
for i in range (length):
sumRow=0
sumCol=0
  
for j in range (length):
sumRow+= matrix[i][j]
sumCol+= matrix[j][i]
  

matrix[i][j+1]=sumRow

matrix[length][colCount]=sumCol
colCount+=1

sumLastCol=0
for i in range(length):
sumLastCol+=matrix[i][length]
matrix[length][length]=sumLastCol

print("Input cell value: ")
for i in range (length):

for j in range (length):
print(matrix[i][j],end=" ")
print()

print("Result : ")
for i in range (length+1):

for j in range (length+1):
print(matrix[i][j],end=" ")
print()

Please find below OUTPUT screenshot:


Related Solutions

Write a Python program which adds up columns and rows of given table as shown in...
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.): Input number of rows/columns (0 to exit) 4 Input cell value: 25 69 51 26 68 35 29 54 54 57 45 63 61 68 47 59 Result:             25   69   51   26 171...
"Create a program that displays a table consisting of four rows and five columns. The first...
"Create a program that displays a table consisting of four rows and five columns. The first column should display the numbers 1 through 4. The second and sub-sequent columns should display the result of multiplying the number in the first column by the numbers 2 through 5. If necessary, create a new project named Introductory14 Project, and save it in the Cpp8\Chap08 folder. Enter the C++ instructions into a source file named Introductory14.cpp. Also enter appropriate comments and any additional...
Write a c program that reads a .img file by rows and columns and prints out...
Write a c program that reads a .img file by rows and columns and prints out the arrays. The .img file contains h(the height) and w(the width) of the text size. An example .img file would be: 2 4 DFJSK HJ5JF HFDY5
Using the Python program: a/ Write a program that adds all numbers from 1 to 100...
Using the Python program: a/ Write a program that adds all numbers from 1 to 100 to a list (in order). Then remove the multiples of 3 from the list. Print the remaining values. b/ Write a program that initializes a list with ten random integers from 1 to 100. Then remove the multiples of 3 and 5 from the list. Print the remaining values. Please show your work and explain. Thanks
JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns...
JAVA LANGUAGE Write a program that declares a 2-Dimensional Array with 4 rows and 4 columns to store integer values, and then: fill elements with values as the sum of its column index and row index, e.g., the element at row index 0 and column index 0 is (0+0=0), the element at row index 0 and column index 1 is (0+1=1). compute the sum of elements at the second row. compute the sum of elements at the third column. compute...
The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in...
The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in Figure 8-23. The Lo Shu Magic Square has the following properties: l The grid contains the numbers 1 through 9 exactly. l The sum of each row, each column, and each diagonal all add up to the same number. This is shown in Figure 8-24. In a program, you can simulate a magic square using a two-dimensional array. Design a program that initializes a...
Assume that A and B are MATLAB arrays with 10 rows and an equal number of columns (the number of columns is not given).
Assume that A and B are MATLAB arrays with 10 rows and an equal number of columns (the number of columns is not given). Important: Next, the expression "a single line of code" implies a single command or equality. In other words, the code: X=A+1; X=X+B; is considered to be TWO lines of code, even though it can be written as one line. (a) (3%) Write a single line of code which saves the first two rows of array A...
When testing for independence in a contingency table with 2 rows and 2 columns at 5%...
When testing for independence in a contingency table with 2 rows and 2 columns at 5% level of significance, the critical value of the test statistic is____
1 – Create a webpage that contains a table with exactly three rows and two columns....
1 – Create a webpage that contains a table with exactly three rows and two columns. The first row will contain a table heading containing the name of a US National Park, that spans across all columns. Hint: use the colspan attribute inside the opening th tag Give the table heading an onmouseover that will change the text of the heading when it is moused over to read My Favorites Park! (Hint: use innerHTML). Use onmouseout to change it back....
1 – Create a webpage that contains a table with exactly three rows and two columns....
1 – Create a webpage that contains a table with exactly three rows and two columns. The first row will contain a table heading containing the name of a US National Park, that spans across all columns. Hint: use the colspan attribute inside the opening th tag Give the table heading an onmouseover that will change the text of the heading when it is moused over to read My Favorites Park! (Hint: use innerHTML). Use onmouseout to change it back....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT