
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ code to find out the sum of the special matrix elements
Suppose, we are given a square matrix of dimensions n * n. The following values of the matrix are called special elements −
Values that are in the main diagonal.
Values that are in the second diagonal.
Values of the row that has exactly (n - 1 / 2) rows above it and the same number of rows below it.
Values of the column that has exactly (n - 1 / 2) columns at its left and right.
We find out the sum of these special values in the matrix.
So, if the input is like n = 4, mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}, then the output will be 107.
Steps
To solve this, we will follow these steps −
res := 0 for initialize i := 0, when i < n, update (increase i by 1), do: for initialize j := 0, when j < n, update (increase j by 1), do: if i is same as j or i is same as n / 2 or j is same as n/ 2 or i + j is same as n - 1, then: res := res + mat[i, j] print(res)
Example
Let us see the following implementation to get better understanding
#include <bits/stdc++.h> using namespace std; #define N 100 void solve(int n, vector<vector<int>> mat) { int res = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++){ if (i == j || i == n / 2 || j == n / 2 || i + j == n - 1) res += mat[i][j]; } cout << res << endl; } int main() { int n = 4; vector<vector<int>> mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; solve(n, mat); return 0; }
Input
4, {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}
Output
107
- Related Articles
- Python Program to find out the determinant of a given special matrix
- Swift Program to Find the Sum of the Boundary Elements of a Matrix
- C++ code to find total elements in a matrix
- Swift Program to Calculate the Sum of Matrix Elements
- Program to find out the number of submatrices from a matrix where the sum of elements is equal to a specific value in C++
- How to find the sum of all elements of a given matrix using Numpy?
- Go language Program to calculate the sum of matrix elements
- How to find the sum of anti-diagonal elements in a matrix in R?
- Swift Program to calculate the sum of columns of matrix elements
- Swift Program to calculate the sum of rows of matrix elements
- Golang Program to calculate the sum of columns of matrix elements
- Golang Program to calculate the sum of rows of matrix elements
- Program to find out the sum of evenly spaced elements in an array in Python
- C Program to print the sum of boundary elements of a matrix
- Program to find the sum of elements that forms a Z shape on matrix in Python

Advertisements