我想从.dat文件中读取数字,然后计算标准偏差,并在文件中输出数字量。我相信我的均值和标准差函数是正确的。实际上是从文件到函数的数字输入,这使我不知所措。这就是我到目前为止所拥有的。
#include "pch.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
const int MAX_COUNT = 1000; //for max size of array
double Mean(double*, int); //calculates average of numbers
double Standard_Deviation(double*, int); //calculates standard deviation
void Magic_Number();
ifstream InFile;
int main()
{
Homework_Header();
string NameOfInputFile = "StdDev.dat";
InFile.open("StdDev.dat");
if (InFile.fail()) {
cout << "Cannot open file: " << NameOfInputFile << "\d";
exit(1);
}
int SamplePoint = 0;
double dataPoint[MAX_COUNT];
double sd = Standard_Deviation(dataPoint, MAX_COUNT);
while (InFile >> dataPoint)
{
void Magic_Number();
sd = Standard_Deviation(dataPoint, MAX_COUNT);
SamplePoint++;
if (InFile.eof())break;
}
cout << "The Standard Deviation is: " << sd << endl;
cout <<SamplePoint << " records process \n";
InFile.close();
if (InFile.fail()) {
cout << "Cannot close file: " << NameOfInputFile << "\d";
exit(-5);
}
cin.get();
return 0;
}
void Magic_Number()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
}
double Mean(double* numbers, int count)
{
double calculated_mean = 0.0;
for (int i = 0; i < count; ++i)
{
calculated_mean += numbers[i];
}
calculated_mean /= double(count);
return calculated_mean;
}
double Standard_Deviation(double* numbers, int count) // * is pointer: special variable that has a memory address as value
{
double std_dev = 0.0;
double average = Mean(numbers, count); //Mean of numbers
double temp_dev;
for (int i = 0; i < count; ++i)
{
temp_dev = numbers[i] - average; //sets temp_dev to be the deviation from the average
std_dev += temp_dev * temp_dev; //adds squares of the deviations
}
std_dev /= double(count);
std_dev = sqrt(std_dev); // square roots
return std_dev;
}
答案 0 :(得分:0)
#include "pch.h"
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;
double Calc_Stand_Dev(ifstream &);
void Magic_Number();
ifstream InFile;
const int MAX_NUM = 1000;
int main()
{
string NameOfInputFile = "StdDev.dat";
InFile.open("StdDev.dat");
if (InFile.fail()) {
cout << "Cannot open file: " << NameOfInputFile << "\d";
exit(-3);
}
double sd = Calc_Stand_Dev(InFile);
cout << "The standard deviation is: " << sd << endl;
//cout << RecordCount << " numbers total are used to calculate the standard deviation. \n";
InFile.close();
if (InFile.fail()) {
cout << "Cannot close file: " << NameOfInputFile << "\d";
exit(-5);
}
cin.get();
return 0;
}
double Calc_Stand_Dev(ifstream & InFile)
{
double dataPoint[MAX_NUM], Avg, Variance, stdDev, sum = 0, sumSq = 0;
int RecordCount = 0;
for (int i = 0; i < MAX_NUM; i++)
{
InFile >> dataPoint[i];
Magic_Number();
sum += dataPoint[i]; //sums each new data point added from the file
sumSq += (dataPoint[i] * dataPoint[i]); //squares the data points and makes a sum out of it.
++RecordCount; // coutner for number of data points
if (InFile.eof())break;
}
Avg = sum / RecordCount;
Variance = (RecordCount * sumSq - sum * sum) / (RecordCount * (RecordCount - 1));
stdDev = sqrt(Variance);
cout << RecordCount << " numbers processed \n";
return stdDev;
}
void Magic_Number()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
}