Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- using namespace std;
- #define INPUT_DATA_SIZE 1024
- #define SEPARATOR ","
- bool num_is_valid(char *num) {
- for(int i = 0; i < strlen(num); i++) {
- if (isalpha(num[i])) return false;
- }
- return true;
- }
- double median(const double sorted[], int start, int end) {
- int size = end - start + 1;
- int middle = start + (size - 1) / 2;
- if (size % 2 == 0) {
- return (sorted[middle] + sorted[middle + 1]) / 2.0;
- } else {
- return sorted[middle];
- }
- }
- double median_value(const double population[], const int population_size) {
- double sorted[population_size];
- for(int i = 0; i < population_size; i++) sorted[i] = population[i];
- int n = sizeof(sorted) / sizeof(sorted[0]);
- sort(sorted, sorted + n);
- return median(sorted, 0, population_size - 1);
- }
- void calculate_quartiles(const double population[], const int population_size, double *q1, double *q3) {
- double sorted[population_size];
- for(int i = 0; i < population_size; i++) sorted[i] = population[i];
- int n = sizeof(sorted) / sizeof(sorted[0]);
- sort(sorted, sorted + n);
- double median_value = median(sorted, 0, population_size - 1);
- int middle_index = population_size / 2;
- (*q1) = median(sorted, 0, middle_index - 1);
- if(population_size % 2 != 0) (*q3) = median(sorted, middle_index + 1, population_size - 1);
- else (*q3) = median(sorted, middle_index, population_size - 1);
- }
- double mode(const double population[], const int population_size) {
- if (population_size == 1) return population[0];
- double mode_value;
- unordered_map<double,int>map;
- int max_seen_count_of_num = INT32_MIN;
- for(int i = 0; i < population_size; i++) {
- map[population[i]]++;
- if(map[population[i]] > max_seen_count_of_num) {
- max_seen_count_of_num = map[population[i]];
- mode_value = population[i];
- }
- }
- bool mode_exists = false;
- for (int i = 0; i < population_size; i++) {
- if(map[population[i]] < max_seen_count_of_num) {
- mode_exists = true;
- break;
- }
- }
- if(!mode_exists) return -0.0; else return mode_value;
- }
- double variance(const double population[], const int population_size, const double mean) {
- double sum_of_squares = 0.0;
- for(int i = 0; i < population_size; i++) {
- sum_of_squares += (population[i] - mean) * (population[i] - mean);
- }
- return sum_of_squares / population_size;
- }
- void dispersion(const double population[], const int length) {
- double min_value = population[0];
- double max_value = population[0];
- double sum = 0.0;
- for (int i = 0; i < length; i++) {
- double curr = population[i];
- sum += curr;
- if(curr < min_value) min_value = curr;
- if(curr > max_value) max_value = curr;
- }
- double mean_value = sum / length;
- double median = median_value(population, length);
- double mode_value = mode(population, length);
- double variance_value = variance(population, length, mean_value);
- double first_quartile_value;
- double third_quartile_value;
- calculate_quartiles(population, length, &first_quartile_value, &third_quartile_value);
- cout << "\n";
- cout << "Population size: " << length << endl;
- cout << "Lower value: " << min_value << endl;
- cout << "Highest value: " << max_value << endl;
- cout << "Mean(average): " << mean_value << endl;
- cout << "Median: " << median << endl;
- cout << "Range: " << max_value - min_value << endl;
- cout << "Variance: " << variance_value << endl;
- cout << "Standard deviation: " << sqrt(variance_value) << endl;
- cout << "First quartile: " << first_quartile_value << endl;
- cout << "Third quartile: " << third_quartile_value << endl;
- cout << "Interquartile range: " << third_quartile_value - first_quartile_value << endl;
- cout << "Quartile deviation: " << (third_quartile_value - first_quartile_value) / 2 << endl;
- if(mode_value == -0.0) cout << "Mode: does not exist" << endl;
- else cout << "Mode: " << mode_value << endl;
- }
- int main() {
- char data_string[INPUT_DATA_SIZE];
- cout << "Enter comma separeted data: ";
- cin >> data_string;
- double buffer_population[INPUT_DATA_SIZE];
- int population_size = 0;
- char *character;
- character = strtok (data_string, SEPARATOR);
- while (character != NULL) {
- if(num_is_valid(character)) {
- buffer_population[population_size++] = atof(character);
- character = strtok(NULL, SEPARATOR);
- } else {
- cout << "Input data should contain only comma(,) and nums";
- return -1;
- }
- }
- double population[population_size];
- for(int i = 0; i < population_size; i++) population[i] = buffer_population[i];
- dispersion(population, population_size);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement