跳过正方矩阵内的对角元素

huangapple go评论53阅读模式
英文:

Skipping Diagonal Elements Within a Square Matrix

问题

写了一个程序来显示一个方阵(例如:5x5),我试图计算排除对角线上的元素的总数(从左上到右下和从左下到右上)

示例:

  在一个5x5的矩阵中,只有`X`会被计算

    O X X X O
    X O X O X
    X X O X X
    X O X O X
    O X X X O

因此:

    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1

    总计:16

或者

    2 2 2 2 2
    2 2 2 2 2
    2 2 2 2 2
    2 2 2 2 2
    2 2 2 2 2

    总计:32

以下是我的代码

```cpp
int calculateVal(int **mat, int size)
{
    int count = 0;
    for (int row = 0; row < size; row++)
    {
        for (int col = 0; col < size; col++)
        {
            if (isRightDiagonal(size, row, col) == true ||
                isLeftDiagonal(size, row, col) == true)
            {
                col++;
            }
            count += mat[row][col];
        }
    }
    return count;
}

bool isRightDiagonal(int size, int row, int col)
{
    return (col == abs(row - size)) ? true : false; 
}

bool isLeftDiagonal(int size, int row, int col)
{
    return (row == abs(col - size)) ? true : false; 
}
英文:

Wrote a program to display a square matrix (ex: 5x5) and I am trying to count the total of the elements excluding the elements that lie within a diagonal line (from top left to bottom right & from bottom left to top right)

EX:

In a 5x5 matrix, only the Xs would be counted

O X X X O
X O X O X
X X O X X
X O X O X
O X X X O

Thus:

1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
Total: 16

OR

2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
Total: 32

Here is my code

int calculateVal(int **mat, int size)
{
int count = 0;
for (int row = 0; row &lt; size; row++)
{
for (int col = 0; col &lt; size; col++)
{
if (isRightDiagonal(size, row, col) == true ||
isLeftDiagonal(size, row, col) == true)
{
col++;
}
count += mat[row][col];
}
}
return count;
}
bool isRightDiagonal(int size, int row, int col)
{
return (col = abs(row - size)) ? true : false; 
}
bool isLeftDiagonal(int size, int row, int col)
{
return (row = abs(col - size)) ? true : false; 
}

I tried using an if statement to check if the element is within a diagonal, and if so, skipping to the next element, but the output is wrong.

The test case I have been primarily using is a 5x5 of 1s like the example above; however, the total prints out as 10 rather than 16

答案1

得分: 1

对角线的测试是错误的:

  • 如果 col == row,元素属于左对角线
  • 如果 size - 1 - col == row,元素属于右对角线

此外,如果元素属于任何一个对角线,应该忽略它,递增 col 是错误的,因为它可能使 mat[row][col] 引用矩阵之外的元素,导致未定义的行为。

以下是修改后的版本:

int calculateVal(int **mat, int size) {
    int count = 0;
    for (int row = 0; row < size; row++) {
        for (int col = 0; col < size; col++) {
            if (col != row && size - 1 - col != row) {
                count += mat[row][col];
            }
        }
    }
    return count;
}
英文:

The tests for the diagonals are incorrect:

  • an element is part of the left diagonal if col == row
  • an element is part of the right diagonal if size - 1 - col == row

Furthermore, if the element is part of either diagonal, it should just be ignored, incrementing col is incorrect as it may make mat[row][col] refer en element outside the matrix, causing undefined behavior.

Here is a modified version:

int calculateVal(int **mat, int size) {
int count = 0;
for (int row = 0; row &lt; size; row++) {
for (int col = 0; col &lt; size; col++) {
if (col != row &amp;&amp; size - 1 - col != row) {
count += mat[row][col];
}
}
}
return count;
}

答案2

得分: 0

使用一个包含1的示例矩阵来标记你不想计算的条目(如果你将它们相加而不是计算它们,那么值就不为0,那么你就知道出现了问题)。你可以使用一个类似的精心制作的矩阵来确保你所关心的16个条目被计算:

#include <stdio.h>
#include <stdlib.h>

int main() {
	int matrix[][5] = {
		{1, 0, 0, 0, 1},
		{0, 1, 0, 1, 0},
		{0, 0, 1, 0, 0},
		{0, 1, 0, 1, 0},
		{1, 0, 0, 0, 1}
	};
	size_t sum = 0;
    const size_t n = sizeof matrix / sizeof *matrix;
	for(size_t r = 0; r < n; r++)
		for(size_t c = 0; c < n; c++)
			sum += (r != c && r + c + 1 != n) * matrix[r][c];
	printf("sum = %zu\n");
}

如果你不喜欢无分支的样式,还有另一种表达方式:

if(r != c && r + c + 1 != n)
    sum += matrix[r][c];
英文:

Using a sample matrix with 1 for the entries you don't want to count (if you sum them instead of count then the value is not 0 then you know something is off). You could use a similar crafted matrix to ensure that the the 16 entries you are interested in are counted:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
int main() {
int matrix[][5] = {
{1, 0, 0, 0, 1},
{0, 1, 0, 1, 0},
{0, 0, 1, 0, 0},
{0, 1, 0, 1, 0},
{1, 0, 0, 0, 1}
};
size_t sum = 0;
const size_t n = sizeof matrix / sizeof *matrix;
for(size_t r = 0; r &lt; n; r++)
for(size_t c = 0; c &lt; n; c++)
sum += (r != c &amp;&amp; r + c + 1 != n) * matrix[r][c];
printf(&quot;sum = %zu\n&quot;, sum);
}

If you don't like the non-branching style here is another way to express it:

			if(r != c &amp;&amp; r + c + 1 != n)
sum += matrix[r][c];

huangapple
  • 本文由 发表于 2023年2月16日 07:12:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定