generate functionTue, 21 Mar 2023

Given a square matrix of numbers. Write a program that swaps elements on the main and secondary diagonals, with each element remaining in the same column (that is, in each column, you need to swap the element on the main diagonal and on the secondary diagonal). The input to the program is a natural number. n is the number of rows and columns in the matrix, then the matrix elements row by row separated by a space.

def SwapDiagonals(n,matrix): for i in range(n): matrix[i][i], matrix[i][n - 1 - i] = matrix[i][n - 1 - i], matrix[i][i] return matrix n = int(input()) matrix = [] for i in range(n): matrix.append(list(map(int, input().split()))) print(SwapDiagonals(n, matrix))

Questions about programming?Chat with your personal AI assistant