c++ - 添加二维数组中列的值

标签 c++ arrays algorithm multidimensional-array

您好,我正在尝试在数组的各个元素中添加行和列。我可以添加行但不能添加列。下面我有一个二维数组,我添加行的循环产生了我想要的结果,但是我不能将列的元素加在一起。我不知道该怎么做,我不知道如何将所有数字加在一起,希望有人能帮我解决这个问题。提前致谢。这是代码。

#include <iostream>
using namespace std;

int main()
{

    const int ROW1 = 29;
    const int COL1 = 5;
    int days[ROW1][COL1] =
    {
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 },
      { 1, 2, 3, 4, 5 }, 
    };

//code that sums each row in the array and displays the results.

    for (int k = 0; k < ROW1; k++)
    {
        int rowAdder = 0;
        for (int l = 0; l < COL1; l++)
        {
            rowAdder += days[k][l];
        }
        cout << "The total of row " << k + 1 << " is " << rowAdder << "." << endl;
    }

    cout << endl;

//code that sums each column in the array and displays the results.

    for (int m = 0; m < ROW1; m++)
    {
        for (int n = 0; n < COL1; n++)
        {
            int columnAdder = 0;
            columnAdder += days[m][n];
            cout << columnAdder << endl;
        }
    }
    return 0;
}

行的输出如下:

The total of row 1 is 15.
The total of row 2 is 15.
The total of row 3 is 15.
The total of row 4 is 15.
The total of row 5 is 15.
The total of row 6 is 15.
The total of row 7 is 15.
The total of row 8 is 15.
The total of row 9 is 15.
The total of row 10 is 15.
The total of row 11 is 15.
The total of row 12 is 15.
The total of row 13 is 15.
The total of row 14 is 15.
The total of row 15 is 15.
The total of row 16 is 15.
The total of row 17 is 15.
The total of row 18 is 15.
The total of row 19 is 15.
The total of row 20 is 15.
The total of row 21 is 15.
The total of row 22 is 15.
The total of row 23 is 15.
The total of row 24 is 15.
The total of row 25 is 15.
The total of row 26 is 15.
The total of row 27 is 15.
The total of row 28 is 15.
The total of row 29 is 15.

我希望第二个循环具有类似的输出,但如果您运行代码,数字将不会相加,它会计算出各个元素。反而。再次感谢。

最佳答案

您需要颠倒循环的顺序:

for (int m = 0; m < ROW1; m++)
{
    for (int n = 0; n < COL1; n++)
    {
        int columnAdder = 0;
        columnAdder += days[m][n];
        cout << columnAdder << endl;
    }
}

应该是:

for (int n = 0; n < COL1; n++)
{
    int columnAdder = 0;
    for (int m = 0; m < ROW1; m++)
    {
        columnAdder += days[m][n];
    }
    cout << "The total of column " << n + 1 << " is " << columnAdder << endl;
}

请注意这与您为 rowAdder 所做的有何相似之处。区别在于循环的顺序。

关于c++ - 添加二维数组中列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50430756/

相关文章:

c++ - 视觉 C++ 2005 : How to view the window during a debugging session

java - 重复该程序,直到用户在数组中键入 none

java - 对象数组和字符串数组有什么区别

python - 从 .npy 文件中提取特定值

在立方体中查找 3D 点子集的算法复杂度

通过迷宫的算法

c# - TreeMap 的旅行商问题(无汉密尔顿路径)

c++ - Qt 警告:传递 qreal 进行转换 QRect::QRect(int, int, int, int)

c++ - 恒定时间码

c++ - QT GUI 进度条