# Given a square matrix of size N×N, calculate the absolute difference between the sums of its diagonals.
# Input Format
# The first line contains a single integer, N. The next N lines denote the matrix's rows, with each line containing N space-separated integers describing the columns.
# Output Format
# Print the absolute difference between the two sums of the matrix's diagonals as a single integer.
p "Enter the size of array :"
n = gets.strip.to_i
a = Array.new(n){Array.new(n)}
for a_i in (0..n-1)
p "Enter #{a_i+1} row elements with one space :"
a_t = gets.strip
a[a_i] = a_t.split(' ').map(&:to_i)
end
first_diagonal = 0
second_diagonal = 0
for i in (0..n-1)
j=i
first_diagonal += a[i][j]
second_diagonal += a[i][n-1]
n -= 1
end
difference = (first_diagonal - second_diagonal).abs
p "Difference is :", difference
# Input Format
# The first line contains a single integer, N. The next N lines denote the matrix's rows, with each line containing N space-separated integers describing the columns.
# Output Format
# Print the absolute difference between the two sums of the matrix's diagonals as a single integer.
p "Enter the size of array :"
n = gets.strip.to_i
a = Array.new(n){Array.new(n)}
for a_i in (0..n-1)
p "Enter #{a_i+1} row elements with one space :"
a_t = gets.strip
a[a_i] = a_t.split(' ').map(&:to_i)
end
first_diagonal = 0
second_diagonal = 0
for i in (0..n-1)
j=i
first_diagonal += a[i][j]
second_diagonal += a[i][n-1]
n -= 1
end
difference = (first_diagonal - second_diagonal).abs
p "Difference is :", difference
No comments:
Post a Comment