Javatpoint Logo
Javatpoint Logo

Print the corner elements and their sum in a 2-D matrix in C++

The corner elements of the matrix are present in the positions below:

Top-left corner: The element that is present in the first row and first column, i.e., matrix[0][0].

Top-right corner: The element that is present in the first row and last column, i.e., matrix[0][cols - 1], where cols is the number of columns in the matrix.

Bottom-left corner: The element that is present in the last row and first column, i.e., matrix[rows - 1][0], where rows are the number of rows in the matrix.

Bottom-right corner: The element that is present in the last row and last column, i.e., matrix[rows - 1][cols - 1].

An example of showing the corner elements in a two-dimensional matrix;

Matrix with 16 elements

Corner elements for the above matrix

Hence, the sum of the corner elements is 1 + 4 + 13 + 16

Sum = 34.

Example:

Let's take a program to find the corner elements in a 2D matrix in C++:

Output:

Print the corner elements and their sum in a 2-D matrix in C++

Explanation:

The above program is used to display the sum of the corner elements present in the two-dimensional matrix. First, in the program, we initialize the variables rows and cols, which represent the rows and columns of the 2d matrix. These rows and columns are taken from the user. After that, we initialize the vector, which is a 2D vector. The size of each vector present in the 2D vector is equal in cols, and there are rows number of vectors inside the 2D vector. This vector is named as a matrix. Now, there are two for loops that are nested in nature to take the elements from the user and store the elements in the 2D vector. Now again two for loops which are also nested in nature are used to print all the elements in the 2D vector.

Now, in the program we are printing all the corner elements in the matrix. In the matrix, the Top-left corner is represented as matrix[0][0], the Top right corner of the matrix is matrix[0][cols-1], the Bottom left corner of the matrix is matrix[rows-1][0], and the Botton right corner of the matrix is matrix[rows-1][cols-1].

Conclusion:

In conclusion, understanding the corner elements of a 2-D matrix is essential in various fields like computer science and mathematics. This fundamental concept allows us to access specific data points efficiently, leading to optimized algorithms and computations. The provided C++ program demonstrates the practical application of this knowledge. By enabling user input for the matrix dimensions and elements, the program dynamically processes any given 2-D matrix. After that, it accurately identifies and displays the corner elements - the building blocks of the matrix's structure. Moreover, the program provides a clear illustration of their significance by calculating and showcasing the sum of these corner elements.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA