gpt4 book ai didi

c++ - 无法添加二维数组的列和行

转载 作者:行者123 更新时间:2023-11-29 19:57:19 26 4
gpt4 key购买 nike

我目前在向我提供的数据文件中添加总列数时遇到一些困难,如下所示:

   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];
}

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com