Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
3.8 kB
7
Indexable
Never
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>

class DataSet {
	int		*dataset;	//holds the data
	int		size;		//size of data in the set
	int		limit;		//size of the array

public:
	double mean();		//returns the mean of dataset
	int min();			//returns the minimum value of dataset
	int max();			//returns the maximum value of dataset
	double standev();		//returns the standard deviation of dataset
	double median();		//returns the median value of dataset
	double variance();		//returns the variance of dataset 
	int getSize();		//returns the size of dataset
	DataSet(int s);		//constructs the dataset array
	//PrintAll prints the dataset in either a table=true 
       //or one column if table =false
	void PrintAll(bool table);	
	                            
	/* LoadData checks if the file "in" is open and attempts to 
	   load the dataset from the input file. If the file is not 
	   open print "file not open". If the file contains more data the 
	   limit entered, stop attempting to read and return.
	*/
	void LoadData(std::ifstream &in); 	
	void sort();
};

DataSet::DataSet(int s)
{
    if (s < 1 || s > 100000000) {
        std::cout << "Size must be between 1 and 100000000." << std::endl;
        exit(1);
    }
    size = 0;
    limit = s;
    dataset = new int[limit];
}

void DataSet::LoadData(std::ifstream &in)
{
    if (!in.is_open())
    {
        std::cout << "file not open" << std::endl;
        return;
    }
    int temp;
    while (in >> temp && size < limit)
    {
        dataset[size] = temp;
        size = size + 1;
    }
}


double DataSet::mean()
{
    double sum = 0;
    for (int i = 0; i < size; i++)
        sum += dataset[i];
    return sum / size;
}

int DataSet::min() {
    int min = INT_MAX;
    for (int i = 0; i < size; i++) {
        if (dataset[i] < min) {
            min = dataset[i];
        }
    }
    return min;
}


int DataSet::max() {
    int max = INT_MIN;
    for (int i = 0; i < size; i++) {
        if (dataset[i] > max) {
            max = dataset[i];
        }
    }
    return max;
}

double DataSet::variance()
{
    double meanVal = mean();
    double sum = 0;
    for (int i = 0; i < size; i++)
        sum += pow(dataset[i] - meanVal, 2);
    return sum / (size - 1);
}

double DataSet::standev()
{
    return sqrt(variance());
}

double DataSet::median()
{
    sort();
    if (size % 2 == 0)
        return (dataset[size / 2 - 1] + dataset[size / 2]) / 2.0;
    else
        return dataset[size / 2];
}

int DataSet::getSize()
{
    return size;
}

void DataSet::sort() {
    //Selection sort algorithm
    for (int i = 0; i < size - 1; i++) {
        int minIndex = i;
        for (int j = i + 1; j < size; j++) {
            if (dataset[j] < dataset[minIndex]) {
                minIndex = j;
            }
        }
        if (minIndex != i) {
            int temp = dataset[i];
            dataset[i] = dataset[minIndex];
            dataset[minIndex] = temp;
        }
    }
}

void DataSet::PrintAll(bool table)
{
    if (table)
    {
        int count = 0;
        std::cout << std::endl;
        std::cout << "================================================================================\n";
        for (int i = 0; i < size; i++)
        {
            std::cout << std::right << std::setw(6) << dataset[i];
            count++;
            if (count == 10)
            {
                std::cout << std::endl;
                count = 0;
            }
        }
        if (count != 0)
            std::cout << std::endl;
        std::cout << "================================================================================\n";
    }
    else
    {
        for (int i = 0; i < size; i++)
            std::cout << std::right << std::setw(6) << dataset[i] << std::endl;
    }
}