Skip to content

Instantly share code, notes, and snippets.

@mariusvn
Created February 6, 2019 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariusvn/ed60bd1491f27aa16cff3fb7685bece3 to your computer and use it in GitHub Desktop.
Save mariusvn/ed60bd1491f27aa16cff3fb7685bece3 to your computer and use it in GitHub Desktop.
/*
** EPITECH PROJECT, 2018
** groundhog
** File description:
** main.cpp
*/
#include <string>
#include <iostream>
#include <vector>
#include <cmath>
void print_help(void)
{
std::cout << "SYNOPSIS" << std::endl;
std::cout << "\t./groundhog period" << std::endl << std::endl;
std::cout << "DESCRIPTION" << std::endl;
std::cout << "\tperiod\tthe number of days defining a period" << std::endl;
}
double average(const std::vector<float> &indexes, const int period)
{
unsigned long i;
double avg = 0;
if (indexes.empty())
return 0;
if (indexes.size() == 1)
return indexes[0];
for (i = 0; i < ((indexes.size() > period) ? period : indexes.size()); i++) {
avg += indexes[i];
}
avg /= i;
return avg;
}
double deviation(const std::vector<float> &indexes, const int period)
{
unsigned long i;
double dev = 0;
double avg = average(indexes, period);
if (indexes.empty())
return 0;
if (indexes.size() == 1)
return 0;
for (i = 0; i < ((indexes.size() > period) ? period : indexes.size()); i++) {
dev += pow(indexes[i] - avg, 2);
}
dev /= i;
return dev;
}
int main(int ac, char **av)
{
std::vector<float> dataList = {};
int period;
if (ac != 2) {
print_help();
return 84;
}
try {
period = std::stoi(av[1]);
} catch (std::invalid_argument &e) {
print_help();
return 84;
}
for (std::string line; getline(std::cin, line);) {
try {
float num = stof(line);
dataList.insert(dataList.begin(), num);
std::cout << average(dataList, period) << ", " << deviation(dataList, period) << std::endl;
if (dataList.size() > period)
dataList.pop_back();
} catch (std::invalid_argument &e) {
return 84;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment