开发者

Sum of the diagonal in a 2 dimensional array

开发者 https://www.devze.com 2023-02-14 02:43 出处:网络
for (row=0; row<SIZE; row++) { for (col=0; col<SIZE; col++) { if (row == 0 && col == 0) { answer+=my_data[row][col];
for (row=0; row<SIZE; row++)
    {
        for (col=0; col<SIZE; col++)
        {
            if (row == 0 && col == 0) {
                answer+=my_data[row][col];
            }
            else if ((row)/(col) == 1) //1 is slope of array
            {
                answer+=my_data[row][col];
            }
        }
    }
    printf("The diagonal sum of the 8x8 array is: %i\n",answer);

starting from [0,0] and ending at开发者_开发百科 [8,8]

"Sum the diagonal values inside the 8x8 array starting at [0,0]"

I realize I can do a single for loop since the shape is just 1:1 but how would I do it just in case I need to sum a diagonal 8x10 array?


Diagonal element(s) and main diagonal are defined only for a square matrix.

if rowcount == colcount
  sum = 0
  for i = 0 to rowcount
    sum += matrix[i][i]
  end-for
else
  print Diagonal sum not defined
end-if

For a non-square matrix if you want to sum the elements at position with equal row and column number you can do:

sum = 0
for i = 0 to min(rowcount,colcount)
  sum += matrix[i][i]
end-for
print sum


Depends how you will define diagonal elements. If we define diagonal elements as - cells through which diagonal line passes somewhere NEAR the center of cell, then diagonal elements of matrix:

Sum of the diagonal in a 2 dimensional array

(0,0);(1,0);(2,1);(3,1);(4,2);(5,2) will be perfectly valid.


Well the primary concern over here is, what's the definition of diagonal elements if it's not a square matrix. If it's a square matrix then it should be RowCount==ColCount. But what if it's not a square matrix??


codaddict is right; only square matrixes have diagonal elements. I suppose you want addition of elements where rownum==colnum?


My guess would be to ignore the excess rows or columns if it's non-square; that seems like the most reasonable approach, although that is just a guess since the diagonal of a non-square matrix isn't defined, but since you're asking about 2-dim array instead of a matrix, I presume the context of the question is a programming assignment, rather than linear algebra.

To do this, you can get the min length of row & column and then iterate from 0 to that min length:

diag_len = min (row_size, col_size);

for (int i = 0; i < /* or <= */ diag_len; i++) {
  answer += my_data[i][i];
}

This works in both the square and lossy non-square cases. Your example code ends up considering the coordinates of every cell which is wasteful when you know you only care about the cases where row == col. (It also obviates the need for a special case when row & col are both 0 and avoids division, which is a slower CPU operation than addition.)


include

int main(){

int a[10][10],i,j,sum=0,m,n;

printf("\nEnter the row and column of matrix: "); scanf("%d %d",&m,&n);

printf("\nEnter the elements of matrix: ");

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号