Editing MatrixElementSum.py

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
Line 2: Line 2:
  
  
 +
<pre>
 +
echo '#!/usr/bin/env python3
 +
 +
def matrixElementsSum(matrix):
 +
    # Initialize a variable to store the total sum
 +
    total_sum = 0
 +
   
 +
    # Iterate through each column in the matrix
 +
    for column in range(len(matrix[0])):
 +
        # Iterate through each row in the column
 +
        for row in range(len(matrix)):
 +
            # If the current element is zero, break out of the loop
 +
            if matrix[row][column] == 0:
 +
                break
 +
            # Otherwise, add the current element to the total sum
 +
            else:
 +
                total_sum += matrix[row][column]
 +
   
 +
    # Return the total sum
 +
    return total_sum
 +
 +
# Prompt the user to input a matrix
 +
rows = int(input("Enter the number of rows in the matrix: "))
 +
columns = int(input("Enter the number of columns in the matrix: "))
  
echo '#!/usr/bin/env python3
+
matrix = []
+
 
[[def]] matrixElementsSum(matrix):
+
for i in range(rows):
    # Initialize a variable to store the total sum
+
    row = []
    total_sum = 0
+
    for j in range(columns):
   
 
    # Iterate through each column in the matrix
 
    for column in [[range]]([[len]](matrix[0])):
 
        # Iterate through each row in the column
 
        for row in range(len(matrix)):
 
            # If the current element is zero, break out of the loop
 
            if matrix[row][column] == 0:
 
                [[break]]
 
            # Otherwise, add the current element to the total sum
 
            else:
 
                total_sum += matrix[row][column]
 
   
 
    # Return the total sum
 
    return total_sum
 
 
# Prompt the user to input a matrix
 
rows = int(input("Enter the number of rows in the matrix: "))
 
columns = int(input("Enter the number of columns in the matrix: "))
 
 
matrix = []
 
 
for i in range(rows):
 
    row = []
 
    for j in range(columns):
 
 
         element = int(input(f"Enter the element at position ({i}, {j}): "))
 
         element = int(input(f"Enter the element at position ({i}, {j}): "))
        row.[[append]](element)
+
        row.append(element)
    matrix.[[append]](row)
+
    matrix.append(row)
+
 
# Calculate the total sum of non-zero elements
+
# Calculate the total sum of non-zero elements
total_sum = matrixElementsSum(matrix)
+
total_sum = matrixElementsSum(matrix)
+
 
# Print the total sum
+
# Print the total sum
[[print]]("The total sum of non-zero elements in the matrix is:", total_sum)' >  
+
print("The total sum of non-zero elements in the matrix is:", total_sum)' > matrixElementsSum.py && chmod +x matrixElementsSum.py && ./matrixElementsSum.py
matrixElementsSum.py && chmod +x matrixElementsSum.py && ./matrixElementsSum.py
+
</pre>

Please note that all contributions to wikieduonline may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Wikieduonline:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)

Templates used on this page:

Advertising: