Help to set the condition "below the secondary diagonal."

#include <iostream> using namespace std; int main() { int M; int N; int i, j; double** matrix; cout << endl << "M = " ; cin >> M; cout << endl << "N = " ; cin >> N; matrix = new double*[M]; for (i = 0; i < M; i++) matrix[i] = new double[N]; for (i = 0; i < M; i++) // ввод for (j = 0; j < N; j++) { cout << "Inter element " << "[" << i << "][" << j << "] "; cin >> matrix[i][j]; } cout << endl; for (i = 0; i < M; i++) { for (j = 0; j < N; j++) cout << matrix[i][j] << "\t"; cout << "\n"; } int sum = 0; for (i = 0; i < M; i++) { for (j = 0; j < N ; j++) { } } cout << endl << sum << endl; system("pause"); } 
  • You just need to cycle j not from 0 but from N - i to begin, and then all the matrix[i][j] will be the ones you need. Only this if the matrix is ​​square, for non-square it is not clear what a diagonal is. - Arty OneSoul
  • Do you have elements with row indices below the diagonal, 1 less indices on the diagonal (ie, such a "parallel line", or all the elements below the diagonal (triangle or trapezium of elements)? - Harry

2 answers 2

What is a secondary diagonal is a diagonal that goes from above-right to bottom-left. The desired "diagonal below" is just a plus unit in height. But there is one problem: a non-square matrix needs to determine exactly what to call a diagonal.

So, if it was a square matrix of size x size, and numbered from scratch, then the secondary diagonal is (schematically)

 s=0; for (int i = 1; i <= size; i++) { s+=m[size-i][i-1] } 

And with the diagonal below, it will be like this (note that the size of this diagonal is 1 smaller)

 s=0; for (int i = 1; i <= size-1; i++) { s+=m[size-i][i] } 
  • At the first iteration, the program will fall, why do not you have m[size-i-1] ? indexing from scratch ... - ampawd
  • Yes, the hand out of habit wrote zeros. - KoVadim
  • in this case it will also fall - if i = size element m[size-i][i] goes beyond the array - ampawd
  • Having rummaged in the Internet, I found out that usually the secondary diagonal for non-square ones comes from the bottom left corner. But you have - only the elements lying directly next are summed up, but, as I understand it, you need to add a kind of triangle (or trapezoid)? - Harry
  • but I do not know, maybe everything is too cunning. Need to clarify. - KoVadim

The elements below the side diagonal in the matrix with M rows and N columns are those of a[i][j] , for which i+j > M-1 .

You can simply search through all elements and filter out unnecessary ones, or you can - by each line i from the Mi th element to the N-1 th ...