繁体   English   中英

有没有办法对角求和一个二维数组?

[英]Is there a way to sum an 2d array diagonally?

我试图使它的二维数组中的值从左上角到右下角和右上角到左下角相加。 我很难考虑该怎么做。 我在想也许一个 for 循环会在那里,但这就是我的想法。 我也有一个蓄能器。 我的问题是指我的lefttorightrighttoleft功能。 谢谢!

#include <iostream>
using namespace std; 

const int COLS = 3;

bool showarray(const int[][COLS]);
int sumofrow(const int[][COLS]);
int sumofcolumn(const int[][COLS]);
int lefttoright(const int[][COLS]); // function to sum diagonally
int righttoleft(const int[][COLS]); // function to sum diagonally

int main()
{

int table1[COLS][COLS] = { 1,2,3,4,5,6,7,8,9 };
int table2[COLS][COLS] = { {4, 9, 2},
                        {3, 5, 7},
                         {8, 1, 6} };


cout << "ARRAY\n";
cout << (showarray(table1) ? " " : " NOT ");
cout << "a LO Shu Magic Square!"
    << endl;


}

int sumofrow(const int arry[][COLS])
{
int total;
for (int i = 0; i < COLS; i++)
{
    total = 0; 
    for (int count = 0; count < COLS; count++)
    {
        total += arry[i][count];
    }
}
return total; 
}

int sumofcolumn(const int arry[][COLS])
{
int total;
for (int col = 0; col < COLS; col++)
{
    total = 0; 
    for (int row = 0; row < COLS; row++)
    {
        total += arry[col][row];
    }

}
return total;
}

int lefttoright(const int arry[][COLS])
{
   int total = 0;
   // for loop possibly?

    return total;
}

int righttoleft(const int arry[][COLS])
{
   int total = 0;
    // for loop here possibly?

    return total;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM