Difference between revisions of "MatrixElementSum.py"

From wikieduonline
Jump to navigation Jump to search
(Created page with "{{lc}} <code> echo '#!/usr/bin/env python3 def matrixElementsSum(matrix): # Initialize a variable to store the total sum total_sum = 0 # Iterate through each...")
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{lc}}
 
{{lc}}
<code>
 
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: "))
 
  
matrix = []
+
echo '#!/usr/bin/env python3
 
+
for i in range(rows):
+
[[def]] matrixElementsSum(matrix):
    row = []
+
    # Initialize a variable to store the total sum
    for j in range(columns):
+
    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: "))
 +
 +
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)' > matrixElementsSum.py && chmod +x matrixElementsSum.py && ./matrixElementsSum.py
+
[[print]]("The total sum of non-zero elements in the matrix is:", total_sum)' >  
</code>
+
matrixElementsSum.py && chmod +x matrixElementsSum.py && ./matrixElementsSum.py

Latest revision as of 13:28, 15 April 2023


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

matrix = []

for i in range(rows):
    row = []
    for j in range(columns):
       element = int(input(f"Enter the element at position ({i}, {j}): "))
        row.append(element)
    matrix.append(row)

# Calculate the total sum of non-zero elements
total_sum = matrixElementsSum(matrix)

# Print the total sum
print("The total sum of non-zero elements in the matrix is:", total_sum)' > 
matrixElementsSum.py && chmod +x matrixElementsSum.py && ./matrixElementsSum.py

Advertising: