We all know how to find the elements above the main diagonal in a square matrix. And what if the matrix is ​​not square, but simply rectangular, and how to find in it all the elements above the main diagonal? The usual if (i <= j) not enough.

Suppose there is such a code that makes all the elements above the diagonal 0, the rest are random:

 int n = 10; int m = 16; int** arr = (int**)malloc(sizeof(int*)*m); for (int i = 0; i < m; i++) { arr[i] = (int*)malloc(sizeof(int)*n); } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { arr[i][j] = rand() % 9 + 1; if (i <= j) //Какое-то корректное условие вместо данного { arr[i][j] = 0; } printf("%d ", arr[i][j]); } printf("\n"); } 

How to solve this problem?

  • The main diagonal of the matrix is ​​the diagonal that starts in the upper left corner of the matrix and changes down and to the right until the right or lower edge of the matrix is ​​reached. That is, you will have i = 0, j = 0 at the first cycle, i = 1, j = 1 at the second cycle, etc. go diagonal, until i or j ends - Vitaly Robinovsky
  • one
    @VitaliyRobinovsky, you are right, but I meant the diagonal, which starts at point [0] [0] and ends at [m-1] [n-1], with m! = N, the diagonal does not touch any point the sides, until i or j ends, but it concerns the intersection point of the sides (angle), that is, in this case, the elements of the diagonal are not arr [k] [k], but something else that I want to know. - Vladislav
  • 3
    @ Vladislav: What is this "diagonal" such? How does it go with m! = N? What is the "straight" in the matrix? - AnT
  • one
    @ Vladislav specify the question, i.e. start with the definition of the main diagonal of a rectangular matrix. - Yuri Negometyanov
  • one
    Such a "diagonal" will be drawn by a rasterization algorithm for segments, such as Bresenham. But you can rasterize an inclined segment in different ways, i.e. there will be no unambiguity here until a clear algorithm for such a diagonal is specified. - AnT

2 answers 2

If we understand the desired dividing line here as a diagonal of a rectangle, then we can suggest the following approach:

  if (i * n < j * m) arr[i][j] = 2; //выше диагонали else if (i * n == j * m) arr[i][j] = 1; //точно на диагонали, таких элементов будет всего НОД(n,m) else arr[i][j] = 0; //ниже диагонали 

for 6x6:

 1 2 2 2 2 2 0 1 2 2 2 2 0 0 1 2 2 2 0 0 0 1 2 2 0 0 0 0 1 2 0 0 0 0 0 1 

for 6x9:

 1 2 2 2 2 2 0 2 2 2 2 2 0 0 2 2 2 2 0 0 1 2 2 2 0 0 0 2 2 2 0 0 0 0 2 2 0 0 0 0 1 2 0 0 0 0 0 2 0 0 0 0 0 0 

    following the definition of the main diagonal that was given in the comments - the following code will print all the elements above the main diagonal (except the elements of this diagonal themselves)

     constexpr size_t n = 5, m = 4; int mat[n][m] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {10, 11, 12, 13}, {14, 15, 16, 17} }; for (size_t i = 0; i < n; i++) { for (size_t j = i + 1; j < m; j++) { cout << mat[i][j] << ' '; } cout << '\n'; } 
    • "following the definition of the main diagonal that was given in the comments" - there are several of them. In response, which one was followed? - Kromster