Advertisement
Kostiggig

Untitled

Mar 29th, 2023
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4.  
  5. using namespace std;
  6.  
  7. #define INPUT_DATA_SIZE 1024
  8. #define SEPARATOR ","
  9.  
  10. bool num_is_valid(char *num) {
  11.     for(int i = 0; i < strlen(num); i++) {
  12.         if (isalpha(num[i])) return false;
  13.     }
  14.     return true;
  15. }
  16.  
  17. double median(const double sorted[], int start, int end) {
  18.    
  19.  
  20.     int size = end - start + 1;
  21.     int middle = start + (size - 1) / 2;
  22.    
  23.     if (size % 2 == 0) {
  24.         return (sorted[middle] + sorted[middle + 1]) / 2.0;
  25.     } else {
  26.         return sorted[middle];
  27.     }
  28. }
  29.  
  30. double median_value(const double population[], const int population_size) {
  31.     double sorted[population_size];
  32.     for(int i = 0; i < population_size; i++) sorted[i] = population[i];
  33.  
  34.     int n = sizeof(sorted) / sizeof(sorted[0]);
  35.     sort(sorted, sorted + n);
  36.  
  37.     return median(sorted, 0, population_size - 1);
  38. }
  39.  
  40. void calculate_quartiles(const double population[], const int population_size, double *q1, double *q3) {
  41.     double sorted[population_size];
  42.     for(int i = 0; i < population_size; i++) sorted[i] = population[i];
  43.  
  44.     int n = sizeof(sorted) / sizeof(sorted[0]);
  45.     sort(sorted, sorted + n);
  46.  
  47.     double median_value = median(sorted, 0, population_size - 1);
  48.     int middle_index = population_size / 2;
  49.  
  50.     (*q1) = median(sorted, 0, middle_index - 1);
  51.  
  52.     if(population_size % 2 != 0) (*q3) = median(sorted, middle_index + 1, population_size - 1);
  53.     else (*q3) = median(sorted, middle_index, population_size - 1);
  54.  
  55. }
  56.  
  57. double mode(const double population[], const int population_size) {
  58.     if (population_size == 1) return population[0];
  59.    
  60.     double mode_value;
  61.     unordered_map<double,int>map;
  62.     int max_seen_count_of_num = INT32_MIN;
  63.  
  64.     for(int i = 0; i < population_size; i++) {
  65.         map[population[i]]++;
  66.         if(map[population[i]] > max_seen_count_of_num) {
  67.             max_seen_count_of_num = map[population[i]];
  68.             mode_value = population[i];
  69.         }
  70.     }
  71.  
  72.     bool mode_exists = false;
  73.     for (int i = 0; i < population_size; i++) {
  74.         if(map[population[i]] < max_seen_count_of_num) {
  75.             mode_exists = true;
  76.             break;
  77.         }
  78.     }
  79.  
  80.     if(!mode_exists) return -0.0; else return mode_value;
  81. }
  82.  
  83. double variance(const double population[], const int population_size, const double mean) {
  84.     double sum_of_squares = 0.0;
  85.  
  86.     for(int i = 0; i < population_size; i++) {
  87.         sum_of_squares += (population[i] - mean) * (population[i] - mean);
  88.     }
  89.  
  90.     return sum_of_squares / population_size;
  91. }
  92.  
  93. void dispersion(const double population[], const int length) {
  94.     double min_value = population[0];
  95.     double max_value = population[0];
  96.     double sum = 0.0;
  97.  
  98.     for (int i = 0; i < length; i++) {
  99.         double curr = population[i];
  100.         sum += curr;
  101.  
  102.         if(curr < min_value) min_value = curr;
  103.         if(curr > max_value) max_value = curr;
  104.     }
  105.  
  106.     double mean_value = sum / length;
  107.     double median = median_value(population, length);
  108.     double mode_value = mode(population, length);
  109.     double variance_value = variance(population, length, mean_value);
  110.     double first_quartile_value;
  111.     double third_quartile_value;
  112.    
  113.     calculate_quartiles(population, length, &first_quartile_value, &third_quartile_value);
  114.  
  115.     cout << "\n";
  116.     cout << "Population size: " << length << endl;
  117.     cout << "Lower value: " << min_value << endl;
  118.     cout << "Highest value: " << max_value << endl;
  119.     cout << "Mean(average): " << mean_value << endl;
  120.     cout << "Median: " << median << endl;
  121.     cout << "Range: " << max_value - min_value << endl;
  122.     cout << "Variance: " << variance_value << endl;
  123.     cout << "Standard deviation: " << sqrt(variance_value) << endl;
  124.     cout << "First quartile: " << first_quartile_value << endl;
  125.     cout << "Third quartile: " << third_quartile_value << endl;
  126.     cout << "Interquartile range: " << third_quartile_value - first_quartile_value << endl;
  127.     cout << "Quartile deviation: " << (third_quartile_value - first_quartile_value) / 2 << endl;
  128.    
  129.  
  130.     if(mode_value == -0.0) cout << "Mode: does not exist" << endl;
  131.     else cout << "Mode: " << mode_value << endl;
  132. }
  133.  
  134.  
  135.  
  136. int main() {
  137.     char data_string[INPUT_DATA_SIZE];
  138.     cout << "Enter comma separeted data: ";
  139.     cin >> data_string;
  140.  
  141.     double buffer_population[INPUT_DATA_SIZE];
  142.     int population_size = 0;
  143.  
  144.     char *character;
  145.     character = strtok (data_string, SEPARATOR);
  146.     while (character != NULL) {
  147.         if(num_is_valid(character)) {
  148.             buffer_population[population_size++] = atof(character);
  149.             character = strtok(NULL, SEPARATOR);
  150.         } else {
  151.             cout << "Input data should contain only comma(,) and nums";
  152.             return -1;
  153.         }
  154.     }
  155.  
  156.     double population[population_size];
  157.     for(int i = 0; i < population_size; i++) population[i] = buffer_population[i];
  158.    
  159.     dispersion(population, population_size);
  160.     return 0;
  161. }
  162.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement