从导入的二维数组计算平均值
Calculating average from imported 2D array
之前我问过如何导出一个以随机数为数据的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
循环中较大的一个循环中。否则,如果保持原样,代码将只显示计算出的最后一列的平均值。
相关文章:
- 计算平均值,不包括上次得分
- 在二叉搜索树C++中计算平均值
- 计算平均值:蒙版图像与投资回报率的不同结果
- 类型检测:使用variadic参数正确实现计算平均值的函数
- C 平均程序在循环时不计算n后不计算平均值
- 我必须将 5 个数字放入一个数组中,而不是 cin 计算平均值并输出它,我做错了
- 我需要知道如何正确计算平均值和偏差
- 从数组中获取值并计算平均值,然后存储在数组中
- 发布获取语义以计算平均值
- 计算平均值,而不向退出循环添加负等级
- 如何正确使用指针来计算平均值
- 如何计算平均值
- 在没有值时跳过计算平均值.C++.Xcode
- 用c++计算平均值和方差
- 用于计算平均值的C++程序
- 高精度计算平均值的最佳策略
- 计算平均值C++
- 程序不会降低最低分数并在没有它的情况下计算平均值
- 我需要将Valus与字符串关联的帮助才能计算平均值
- 如何使用 std::累积 和 lambda 来计算平均值