无法添加2D数组的列和行

时间:2016-11-16 19:21:29

标签: c++ mysql file multidimensional-array fstream

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

   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;

提前致谢!

2 个答案:

答案 0 :(得分:0)

问题在于:

<input ... min="{{minCompensation}}" />

正如您所看到的那样,您遍历外部循环中的行以及内部的列,但sumColumns不会添加正确的值。

替换它:

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

用这个:

 for(int i = 0; i < NUM_ROWS; i++)
 {
    for(int j = 0; j < NUM_COLS; j++)
    {
        sumColumns += scores[j][i];
    }

答案 1 :(得分:0)

你只是混合循环变量。

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到{ {1}}。很简单。