Чтение чисел из файла .dat и вычисление стандартного отклонения - PullRequest
0 голосов
/ 12 декабря 2018

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

1 Ответ

0 голосов
/ 13 декабря 2018
#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); 
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...