Sum of Boundary elements For given 3X3 matrix find out the sum of boundary matrix.Constraints:Input format :1st line of Input : 2 Integers ( SIze of Row and Coloumns)Integer Elements based on the size of the matrix Output format :Integer Example:Input 1:3 31 1 11 1 11 1 1Output 1:8Explanation:1+1+1+1+1+1+1+1 = 8
Question
Sum of Boundary elements For given 3X3 matrix find out the sum of boundary matrix.Constraints:Input format :1st line of Input : 2 Integers ( SIze of Row and Coloumns)Integer Elements based on the size of the matrix Output format :Integer Example:Input 1:3 31 1 11 1 11 1 1Output 1:8Explanation:1+1+1+1+1+1+1+1 = 8
Solution
To solve this problem, you need to follow these steps:
-
First, read the size of the matrix from the input. The first line of the input gives you two integers which represent the number of rows and columns of the matrix.
-
Then, read the elements of the matrix. The elements are given in the input as integers, based on the size of the matrix.
-
Now, you need to calculate the sum of the boundary elements of the matrix. The boundary elements are the elements that are located on the first row, the last row, the first column, and the last column.
-
To do this, you can initialize a variable, say
sum, to 0. Then, iterate over the elements of the matrix. If an element is a boundary element, add its value tosum. -
The condition to check if an element is a boundary element is as follows: an element is a boundary element if its row index is 0 or
row-1(whererowis the number of rows of the matrix), or if its column index is 0 orcol-1(wherecolis the number of columns of the matrix). -
After iterating over all the elements of the matrix,
sumwill hold the sum of the boundary elements. -
Finally, print
sum. This is the output of the problem.
For example, if the input is:
3 3
1 1 1
1 1 1
1 1 1
The output will be 8, because the boundary elements are the eight 1s that are located on the borders of the matrix.
Similar Questions
Write a program to obtain a matrix and find the sum of each row and each column.Input format :The first line of the input consists of the value of the number of rows and the number of columns.The second line of the input consists of a matrix.Output format :The output prints the sum of each row and each column.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :4 41 2 3 45 6 7 89 10 11 1213 14 15 16Output 1 :Sum of the row 0 = 10Sum of the row 1 = 26Sum of the row 2 = 42Sum of the row 3 = 58Sum of the column 0 = 28Sum of the column 1 = 32Sum of the column 2 = 36Sum of the column 3 = 40Input 2 :3 398 87 6545 32 2845 56 58Output 2 :Sum of the row 0 = 250Sum of the row 1 = 105Sum of the row 2 = 159Sum of the column 0 = 188Sum of the column 1 = 175Sum of the column 2 = 151
Given 2 matrices A and B of size NxM. You have to find the sum between 1st row of matrix A and Nth row of matrix B, 2nd row of matrix A and (N-1)th row of matrix B and so on.Input FormatFirst line of input contains N,M - the size of the matrices. Its followed by 2xN lines, each containing M integers - elements of the matrices. First N lines for matrix A and the next N lines for matrix B.Output FormatPrint the sum of row 1 of A and row N of B, row 2 of A and row (N-1) of B and so on.Constraints1 <= N, M <= 100-102 <= mat[i][j] <= 102ExampleInput3 33 -2 51 3 44 0 72 2 4-8 4 37 1 9Output23719ExplanationSelf Expla
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
Maximum sumSend FeedbackGiven an N*N matrix. The task is to find the index of the column with the maximum sum. That is the column whose sum of elements is maximum.Input FormatNN*N matrix in comma separated form for each row.Input : 5 1, 2, 3, 4, 5 5, 3, 1, 4, 2 5, 6, 7, 8, 9 0, 6, 3, 4, 12 9, 7, 12, 4, 3Output: Column 5 has the max sum of 31.Input : 3 1, 2, 3 4, 2, 1 5, 6, 7Output: Column 3 has the max sum of 11.
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.