Я предполагаю прочитать числа из файла .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;
}