For a given 2D square matrix of size N*N, the task is to find the sum of elements in the Principal and Secondary diagonals. For example, analyze the following 4 × 4 input matrix.a00 a01 a02 a03a10 a11 a12 a13a20 a21 a22 a23a30 a31 a32 a33Example:Input 1 : 6 7 3 4 8 9 2 1 1 2 9 6 6 5 7 2Output 1 : Principal Diagonal: 26 Secondary Diagonal: 14Intuition:1. The principal diagonal is constituted by the elements a00, a11, a22, a33, and the row-column condition for the principal diagonal is: row = column2. However, the secondary diagonal is constituted by the elements a03, a12, a21, a30, and the row-column condition for the Secondary diagonal is: row + column = N – 1
Question
For a given 2D square matrix of size N*N, the task is to find the sum of elements in the Principal and Secondary diagonals. For example, analyze the following 4 × 4 input matrix.a00 a01 a02 a03a10 a11 a12 a13a20 a21 a22 a23a30 a31 a32 a33Example:Input 1 : 6 7 3 4 8 9 2 1 1 2 9 6 6 5 7 2Output 1 : Principal Diagonal: 26 Secondary Diagonal: 14Intuition:1. The principal diagonal is constituted by the elements a00, a11, a22, a33, and the row-column condition for the principal diagonal is: row = column2. However, the secondary diagonal is constituted by the elements a03, a12, a21, a30, and the row-column condition for the Secondary diagonal is: row + column = N – 1
Solution
Sure, here is the step-by-step solution to find the sum of elements in the Principal and Secondary diagonals of a given 2D square matrix of size N*N:
Step 1: Initialize two variables, say sumPrincipal and sumSecondary, to 0. These variables will hold the sum of the Principal and Secondary diagonals respectively.
Step 2: Start a loop to traverse the matrix. The loop should run from 0 to N-1, where N is the size of the matrix.
Step 3: Inside the loop, add the element at the current index (i, i) to sumPrincipal. This is because, for the Principal diagonal, the row index is equal to the column index.
Step 4: Similarly, add the element at the index (i, N-i-1) to sumSecondary. This is because, for the Secondary diagonal, the sum of the row index and the column index is equal to N-1.
Step 5: Continue this process until all elements in the Principal and Secondary diagonals have been traversed.
Step 6: After the loop ends, sumPrincipal and sumSecondary will hold the sum of the Principal and Secondary diagonals respectively.
For example, if the input is the 4x4 matrix:
6 7 3 4 8 9 2 1 1 2 9 6 6 5 7 2
The Principal diagonal elements are 6, 9, 9, 2 and their sum is 26. The Secondary diagonal elements are 4, 2, 2, 6 and their sum is 14.
So, the output will be: Principal Diagonal: 26 Secondary Diagonal: 14
Similar Questions
Write a program to obtain a matrix and find the sum of its diagonal elements.Note: Only square matrix.Input format :The input consists of the number of rows and columns separated by a space.The second line of the input is matrix elements.Output format :The output prints the sum of diagonal elements.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :3 31 2 34 5 67 8 9Output 1 :15Input 2 :4 412 23 45 5678 89 98 8765 54 32 2114 25 36 58Output 2 :191
Write a C program that calculates the sum of both diagonals in a square matrix. Implement a function that takes a square matrix and its size as input and returns the sum of the main diagonal and anti-diagonal elements.Input3 31 2 3 4 5 6 7 8 9OutputSum of the main diagonal elements: 15Sum of the anti-diagonal elements: 15
Write a C Program to Compute the Sum of Diagonals of a square matrix. Print the sum of both diagonals separately. Get the number of rows and columns from the user.
Given a 2D matrix of size NxN, print the sum of the elements of all its diagonals.Input FormatThe first line of input contains T - the number of test cases. The first line of each test case contains the N - the size of the matrix. Each of the next N lines contains N integers - the elements of the matrix.Output FormatFor each test case, print the sum of the elements of all the diagonals, separated by a new line. Refer to samples for more clarity.Constraints1 <= T <= 1001 <= N <= 100-100 <= ar[i][j] <= 100ExampleInput43-5 0 42 8 -63 7 11-425 -2-4 16-2 -3 -6 -5 50 38 7 10 -5 -3 306 3 70 9 -20 -7-9 9 -6 7 3 2-1 7 7 6 -4 38 5 6 -9 40 8Output4 -6 4 9 3-4-2 6 -43 80 -15 -29 22 86 51 13 4 4 8ExplanationTest Case 1Sum of the elements of the 1st diagonal: 4Sum of the elements of the 2nd diagonal: 0 + -6 = -6Sum of the elements of the 3rd diagonal: -5 + 8 + 1 = 4Sum of the elements of the 4th diagonal: 2 + 7 = 9Sum of the elements of the 5th diagonal: 3Test Case 2Sum of the elements of the 1st and only diagonal: -4Test Case 3Sum of the elements of the 1st diagonal: -2Sum of the elements of the 2nd diagonal: 5 + 1 = 6Sum of the elements of the 3rd diagonal: -4
Write a program to obtain a matrix and find the sum of the elements in the lower triangular matrix.Note: Only square matrixInput format :The first line of the input consists of the number of rows and columns.The second line of the input consists of the matrix element.Output format :The output prints the sum of the lower triangular matrix.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :3 312 23 4556 78 8995 51 20Output 1 :312
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.