Print the Corner Elements and their Sum in a 2-D Matrix in C++
The task is to display the sum of all the corner elements stored in an array given a 2X2 array. The corner components of a matrix with row "r" and column "c" starting from zero will be mat[0][0], mat[0][c-1], mat[r-1][0], and mat[r-1][c-1]. To print the result on the screen, you must now gather these corner elements, i.e., mat[0][0] + mat[0][c-1] + mat[r-1][0] + mat[r-1][c-1].
Example:
If the given Matrix is:-
3 6
2 5
The output sum of the matrix should be 16
Algorithm:
- Start
- Step 1 -> create macro for rows and column as #define row 3 and #define col 3
- Step 2 -> main()
- Declare int sum=0 and array as arr[row][col] and variables int i,j,n
- Loop For i=0 and i<3 and i++
- Loop For j=0 and j<3 and j++
- Input arr[i][j]
- End
- End
- Print [0][0] + arr[0][row-1] +arr[col-1][0] + arr[col-1][row-1]
- Stop
Program:
Let's take an example to print the Corner Elements and their Sum in a 2-D Matrix in C++:
#includeusing namespace std; int main() { int rows, cols; cout << "Enter the number of rows: "; cin >> rows; cout << "Enter the number of columns: "; cin >> cols; if (rows <= 0 || cols <= 0) { cout << "Invalid input for rows or columns. Exiting the program." << endl; return 1; } int matrix[rows][cols]; cout << "Enter the elements of the matrix:" << endl; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { cin >> matrix[i][j]; } } if (rows < 2 || cols < 2) { cout << "The matrix must have at least 2 rows and 2 columns to have corner elements." << endl; } else { int sumOfCorners = matrix[0][0] + matrix[0][cols - 1] + matrix[rows - 1][0] + matrix[rows - 1][cols - 1]; cout << "Corner elements of the matrix:" << endl; cout << "Top-left: " << matrix[0][0] << endl; cout << "Top-right: " << matrix[0][cols - 1] << endl; cout << "Bottom-left: " << matrix[rows - 1][0] << endl; cout << "Bottom-right: " << matrix[rows - 1][cols - 1] << endl; cout << "Sum of corner elements: " << sumOfCorners << endl; } return 0; }
Output:
![Print the Corner Elements and their Sum in a 2-D Matrix in C++](https://static.tutorialandexample.com/cpp/print-the-corner-elements-and-their-sum-in-a-2-d-matrix-in-cpp1.png)
Explanation:
- User Input: In this example, the program first asks the user how many rows and columns there are in a 2-D matrix.
- Input Validation: It verifies that the input values for the rows and columns are legitimate positive integers. In such a case, it alerts the user and ends the program.
- Matrix Declaration: According to the user's input for the number of rows and columns, the program declares a 2-D array (matrix) to hold the matrix elements.
- Input for the matrix: The user is prompted to enter the matrix's components. These values are read by the program and entered into the matrix array.
- Corner Element Check: The program verifies the matrix's corner elements to see if there are at least two rows and two columns. A matrix must have at least two rows and two columns to have the four separate corner components (top-left, top-right, bottom-left, bottom-right). Therefore, this check is mandatory.
- Calculation of the Corner Element: If the matrix meets the requirement for having corners, the program sums the four corner elements. It combines the values of the elements in the top-left, top-right, bottom-left, and bottom-right positions.
- Output: After that, the program prints the values of the corner elements and their sum.