gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:23:32 25 4
gpt4 key购买 nike

之前我问过如何导出带有随机数的二维数组作为数据。

链接:Converting 2D array to text using c++ functions

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

但现在我遇到了“未初始化变量”的问题,我很确定这些变量已经初始化了。

不太确定从这里开始做什么。任何帮助将不胜感激!

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

关于c++ - 从导入的二维数组计算平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34144593/

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