无法添加2D数组的列和行
Unable to add the columns and rows of a 2D array
我目前在添加数据文件中的总列时遇到了一些困难,如下所示:
46.70 83.20 39.40 18.00 42.40 63.60
29.10 12.40 9.50 74.80 43.40 55.70
13.90 96.70 11.70 71.20 39.90 46.20
22.50 84.30 76.00 27.40 50.60 7.10
52.90 56.40 17.60 62.60 59.50 10.30
46.00 67.90 47.00 58.70 75.20 64.30
25.70 19.70 90.10 65.50 67.90 76.00
11.10 97.70 64.10 75.00 65.20 38.80
96.30 48.60 19.20 48.40 69.70 66.60
17.50 74.10 46.10 23.00 6.10 5.40
88.20 43.90 94.90 17.00 47.30 23.20
35.00 40.60 86.10 11.80 18.20 74.60
58.30 14.40 88.10 87.60 62.90 90.50
68.90 82.40 65.30 55.50 63.30 97.80
76.80 87.80 33.30 40.70 81.10 60.10
80.40 84.70 16.40 24.20 93.30 27.70
49.70 61.90 10.40 73.50 57.40 36.80
82.70 9.50 20.20 69.10 39.60 5.50
10.60 23.30 31.60 70.70 15.20 20.70
64.20 95.80 42.80 40.20 64.80 10.70
14.60 71.50 62.80 26.80 81.60 25.10
95.20 61.80 20.60 36.20 86.60 26.20
90.30 93.40 2.20 28.30 74.00 93.80
10.40 27.50 53.70 17.90 60.70 23.80
52.80 93.40 53.70 63.90 33.70 59.30
60.50 62.20 41.00 9.70 18.40 50.10
76.90 31.00 62.60 14.10 18.80 87.50
27.30 71.60 46.90 28.30 39.50 13.00
82.70 27.40 63.90 53.30 12.60 81.20
30.30 74.90 44.50 40.50 23.10 82.20
38.30 65.70 57.20 71.00 25.80 84.80
22.30 75.30 27.20 15.30 40.20 20.70
80.40 38.90 89.20 72.40 55.30 70.20
95.60 5.00 75.40 96.50 8.40 72.70
25.70 8.40 90.60 40.20 43.90 61.10
11.50 65.90 55.00 20.80 48.60 84.70
86.60 13.50 79.20 26.60 95.60 92.80
10.50 84.10 30.00 61.30 37.60 63.40
39.50 62.70 13.90 62.90 23.10 81.40
27.00 67.50 94.20 55.20 49.40 34.30
8.10 25.10 74.60 89.20 54.60 35.60
97.00 95.30 77.10 12.40 8.60 63.10
11.50 16.50 70.70 5.70 38.20 40.40
45.50 73.40 55.20 74.50 95.10 14.80
71.90 49.70 84.30 92.10 52.40 24.90
21.20 46.00 63.70 88.90 5.40 10.70
71.00 64.60 84.00 98.00 43.50 1.40
46.70 32.60 24.40 36.50 10.60 14.60
18.60 67.40 92.00 62.40 36.10 71.30
39.60 74.10 89.60 15.00 73.30 89.30
我目前已经能够添加各个行,获得行和的适当最大值(以及它发生的索引)。然而,我一直无法添加正确的列数(当只有6列时,它不知怎么地计数为50列,并且没有添加每个列的值,而是添加了一些我不知道它们来自哪里的值)。下面是我当前的代码:
const int NUM_ROWS = 50;
const int NUM_COLS = 6;
double scores[NUM_ROWS][NUM_COLS];
double sumRows = 0, sumColumns = 0;
int colIndex, rowIndex;
double highestRow = 0, highestCol = 0;
fstream fin("scores.txt");
while (! fin.eof())
{
for (int i = 0; i < NUM_ROWS; i++)
{
for (int j = 0; j < NUM_COLS; j++)
{
fin >> scores[i][j];
}
}
}
fin.close();
for(int i = 0; i < NUM_ROWS; i++)
{
for(int j = 0; j < NUM_COLS; j++)
{
sumColumns += scores[j][i];
}
cout << "The sum of " << i+1 << " Column is " << sumColumns << endl;
if (sumColumns > highestCol)
{
highestCol = sumColumns;
colIndex = i+1;
}
sumColumns = 0;
}
cout << endl;
for(int i = 0; i < NUM_ROWS; i++)
{
for(int j = 0; j < NUM_COLS; j++)
{
sumRows += scores[i][j];
}
cout << "The sum of " << i+1 << " Row is " << sumRows << endl;
if (sumRows > highestRow)
{
highestRow = sumRows;
rowIndex = i+1;
}
sumRows = 0;
}
cout << "The total of the column with the highest total is: " << highestCol << " and occurs at Column #" << colIndex << endl;
cout << "The total of the row with the highest total is: " << highestRow << " and occurs at Row #" << rowIndex << endl;
提前感谢!
问题就在这里:
for(int i = 0; i < NUM_ROWS; i++)
{
for(int j = 0; j < NUM_COLS; j++)
{
sumColumns += scores[j][i];
}
cout << "The sum of " << i+1 << " Column is " << sumColumns << endl;
if (sumColumns > highestCol)
{
highestCol = sumColumns;
colIndex = i+1;
}
sumColumns = 0;
}
正如您所看到的,您在外部循环中遍历行,在内部循环中遍历列,但是sumColumns没有添加正确的值。
替换:
for(int i = 0; i < NUM_ROWS; i++)
{
for(int j = 0; j < NUM_COLS; j++)
{
sumColumns += scores[j][i];
}
与这个:for(int j = 0; j < NUM_COLS; j++)
{
for(int i = 0; i < NUM_ROWS; i++)
{
sumColumns += scores[i][j];
}
您只是混淆了循环变量。
for(int i = 0; i < NUM_COLS; i++)
{
for(int j = 0; j < NUM_ROWS; j++)
{
sumColumns += scores[j][i];
}
cout << "The sum of " << i+1 << " Column is " << sumColumns << endl;
if (sumColumns > highestCol)
{
highestCol = sumColumns;
colIndex = i+1;
}
sumColumns = 0;
}
2D数组的第一个索引是ROW索引,并且正确地使用了内部循环变量(j
)作为第一个索引,因此外部循环遍历列,内部循环遍历当前列的行,处理它们。但是,您混淆了循环的退出条件。如果j
是行索引,i
是列索引,那么i
应该从0到NUM_COLS
, j
应该从0到NUM_ROWS
。很简单。
相关文章:
- C++向数组添加元素并调整数组大小
- 向对象数组 c++ 添加值
- 在 c++ 中将 Char 数组添加到 Char * 变量
- 向动态数组添加内容
- 动态数组添加元素而不创建结构变量/对象
- 将 2D 数组添加到 1D 数组
- 如何使用sprintf将UINT_64的数组添加到一个字符数组中
- 当循环崩溃时,不向数组添加单词
- 如何将对象数组添加到另一个类
- 将字符数组添加到 const string&in C++
- 如何将数组添加到列表中
- 向量数组C++ - 添加元素时的奇怪行为
- 将char数组添加到C 中的字符串
- strncpy向我的char数组添加了额外的字符
- 在C++中向数组添加项的最有效方法
- 将类对象的数组添加到一个主类对象中
- 将BSON数组添加到MongoDB 3.2文档并提取值(MongoCXX 3.2)(C++11)
- 我是否正确地为 2D 数组添加了行与列
- 读取文件内容,存储在数组中,向数组添加更多内容,然后将新数组存储在文件C++中
- C++类对象数组 - 添加新记录