您好我正在尝试在数组的各个元素中添加行和列。我能够添加行而不是列。下面我有一个二维数组,我的循环添加行产生我想要的,但是我不能将列的元素添加到一起。我很难理解如何做到这一点,我无法弄清楚如何将所有数字加在一起,并希望有人可以帮我解决这个问题。提前致谢。这是代码。
#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.
我希望第二个循环具有类似的输出但是如果运行代码则不会添加数字,它会对各个元素进行couts。代替。再次感谢。
答案 0 :(得分:0)
这可以获得总和。
int totalSum = 0;
for (int m = 0; m < ROW1; m++)
{
for (int n = 0; n < COL1; n++)
{
totalSum += days[m][n];
}
}
cout << "Total sum: " << totalSum << endl;
按专栏:
for (int n = 0; n < COL1; n++)
{
cout << "The total of Column " << n << ": ";
for (int m = 0; m < ROW1; m++)
{
int columnAdder = 0;
columnAdder += days[m][n];
cout << columnAdder << endl;
}
}
答案 1 :(得分:0)
您需要颠倒循环的顺序:
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
的操作类似。区别在于循环的顺序。
答案 2 :(得分:0)
要显示列中的总和,您需要先选择一列然后循环遍历行,所以:
sum1 = 0
for (int m = 0; m < ROW1; m++)
{
sum1 += days[m][0];
}
cout << "Total sum in column 1: " << sum1 << endl;
要遍历所有列,您需要添加第二个循环:
for (int n = 0; n < COL1; n++)
{
int columnSum = 0;
for (int m = 0; m < ROW1; m++)
{
columnSum += days[m][n];
}
cout << "Column sum: " << columnSum << endl;
}
因此,与计算行时相比,循环是反转的。 希望这会对你有所帮助。
答案 3 :(得分:0)
你有一个逻辑问题和一个语法问题。首先,这个循环:
for (int m = 0; m < ROW1; m++)
{
for (int n = 0; n < COL1; n++)
{
int columnAdder = 0;
columnAdder += days[m][n];
cout << columnAdder << endl;
}
}
您的代码首先遍历每一列,然后在迭代遍历每一行的代码中。所以,即使它像你认为的那样工作,你仍然会走错路。
您遇到的另一个问题是您已在columnAdder
循环范围内声明for
。这意味着对于该循环的每次迭代,您都要重新声明变量,将其分配给您当前的元素,然后将其打印出来。所以,这就是你打印每个元素的原因。
你想要的是这个:
for (int m = 0; m < COL1; m++)
{
int columnAdder = 0;
for (int n = 0; n < ROW1; n++)
{
columnAdder += days[n][m];
}
cout << columnAdder << endl;
}
注意你现在如何首先遍历每一列,然后为每次迭代重新声明一个columnAdder
变量并循环遍历每一行以获得答案。