从导入的二维数组计算平均值

Calculating average from imported 2D array

本文关键字:计算 平均值 二维数组 导入      更新时间:2023-10-16

之前我问过如何导出一个以随机数为数据的2D数组。

链接:使用c++函数将2D数组转换为文本

现在我正在尝试编写一个单独的程序,可以计算该数组中每列的平均值。

但现在我遇到了"未初始化变量"的问题,我确信这些变量已经初始化。

不知道该怎么办。任何帮助都将不胜感激!

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
    string Data, FileName;
int row, col, x, y;
float Matrix[50][50], sum, average;
cout << "Enter the name you gave the matrix file.n";
cout << "(DO NOT INCLUDE ANY SPACES OR EXTENSIONS!)n";
cin >> FileName;
FileName = "C:\Users\Public\Documents\" + FileName + ".ascii";
ifstream Fin(FileName);
if (Fin.is_open())
{
    row=0;
    while(!Fin.eof())
    {
        getline(Fin, Data);
        stringstream ss(Data);
        col=0;
        while(ss >> Matrix[row][col])
        {
            col++;
        }
        row++;
    } 
    Fin.close();
}
else 
    cout << "Unable to open file"; 
for (int y = 0; y < col; y++)
{
    for (int x = 0; x < row; x++)
    {
        sum = sum + Matrix[x][y];
    }
    average = sum / row;
    cout << average << " for column " << col << "n";
}
system("pause"); 
return 0;
}

更新:解决了"未初始化变量"错误。

但现在,当我试图计算平均值时,得到"-nan(ind)"。

这是新代码。。。

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
string Data, FileName;
int row, col;
float Matrix[50][50], sum, average;
sum = 0;
cout << "Enter the name you gave the matrix file.n";
cout << "(DO NOT INCLUDE ANY SPACES OR EXTENSIONS!)n";
cin >> FileName;
FileName = "C:\Users\Public\Documents\" + FileName + ".ascii";
ifstream Fin(FileName);
if (Fin.is_open())
{
    row=0;
    while(!Fin.eof())
    {
        getline(Fin, Data);
        stringstream ss(Data);
        col=0;
        while(ss >> Matrix[row][col])
        {
            col++;
        }
        row++;
    } 
    Fin.close();
}
else 
    cout << "Unable to open file"; 
for (int y = 0; y < row; y++)
{
    for (int x = 0; x < col; x++)
    {
        sum = sum + Matrix[x][y];
    }
    average = sum / col;
    cout << average << "n";
}

system("pause"); 
return 0;
}

更新2:我所能得到的似乎只是第一列的平均值。真的不知道如何重复这一步。我试过使用do和for循环,但这给我带来了很多错误,失去了我得到的唯一平均值。

如果有人想让它看一看,请注意它很乱。。。

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main ()
{
string Data, FileName;
int row, col;
float Matrix[50][50], sum, average;
sum = 0;
cout << "Enter the name you gave the matrix file.n";
cout << "(DO NOT INCLUDE ANY SPACES OR EXTENSIONS!)n";
cin >> FileName;
FileName = "C:\Users\Public\Documents\" + FileName + ".ascii";
ifstream Fin(FileName);
if (Fin.is_open())
{
    row=0;
    while(!Fin.eof())
    {
        getline(Fin, Data);
        stringstream ss(Data);
        col=0;
        while(ss >> Matrix[row][col])
        {
            col++;
        }
        row++;
    } 
    Fin.close();
}
else 
    cout << "Unable to open file"; 
double AvgArray[50];
for (int y = 0; y < 50; y++)
{
    for (int x = 1; x < 50; x++)
    {
        if (Matrix[x][y]<0)
        {
            break;
        }
        sum = sum + Matrix[x][y];
        average = sum / x;
    }
    if (Matrix[y][y]<0)
    {
        break;
    }
    average = AvgArray[y];
}
cout << average << "n";
system("pause"); 
return 0;
}

您忘记在第二个for循环之前将sum设置为0。在UPDATE 2中,您仍然没有将sum设置为0!?

在第一个"for"之后,在第二个之前。。。在开始添加列的总和之前。。。

像这个

for (int y = 0; y < col; y++)
{
    sum = 0;
    for (int x = 0; x < row; x++)
    {
        sum = sum + Matrix[x][y];
    }
    average = sum / row;
    cout << average << " for column " << y << "n";
}

您给出的是:

for (int y = 0; y < 50; y++)
{
    for (int x = 1; x < 50; x++)
    {
           if (Matrix[x][y]<0)
        {
            break;
        }
        sum = sum + Matrix[x][y];
        average = sum / x;
    }
    if (Matrix[y][y]<0)
    {
        break;
    }
    average = AvgArray[y];
}

在代码的这一部分中,您似乎正在尝试计算每列中每行的平均值(因为您将average = sum / x;放在两个for循环中。)我建议在计算平均值之前计算整列的总和,以节省代码/时间。此外,您还放置了average = AvgArray[y];。假设您试图用每列的平均值填充此数组,则需要将average分配给AvgArray[y]。目前,您从未为AvgArray[]指定任何值。

cout << average << "n";

就像Zsolt Marx给出的代码一样,您需要将这一行放在两个for循环中较大的一个循环中。否则,如果保持原样,代码将只显示计算出的最后一列的平均值。