0% found this document useful (0 votes)
4 views4 pages

Central Tendency and Dispersion Calculator

A c++ code
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Central Tendency and Dispersion Calculator

A c++ code
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <iostream>

#include <vector>

#include <cmath>

#include <algorithm>

class CentralTendency {

private:

std::vector<double> data;

int length;

public:

CentralTendency(const std::vector<double>& input) : data(input), length([Link]()) {}

double calculateMean() {

double sum = 0.0;

int i = 0;

while (i < length) {

sum += data[i];

++i;

return sum / length;

double calculateMedian() {

std::sort([Link](), [Link]());

if (length % 2 == 0) {

return (data[length / 2 - 1] + data[length / 2]) / 2.0;

} else {

return data[length / 2];


}

};

class Dispersion {

private:

std::vector<double> data;

int length;

public:

Dispersion(const std::vector<double>& input) : data(input), length([Link]()) {}

double calculateVariance() {

double mean = CentralTendency(data).calculateMean();

double sumSquaredDiff = 0.0;

int i = 0;

while (i < length) {

sumSquaredDiff += std::pow(data[i] - mean, 2);

++i;

return sumSquaredDiff / length;

double calculateStdDev() {

return std::sqrt(calculateVariance());

};

int main() {
std::vector<double> inputVector; // Initialize with user input

CentralTendency central(inputVector);

Dispersion dispersion(inputVector);

int choice;

do {

std::cout << "\nMenu:\n";

std::cout << "1. Calculate Mean\n";

std::cout << "2. Calculate Median\n";

std::cout << "3. Calculate Variance\n";

std::cout << "4. Calculate Standard Deviation\n";

std::cout << "0. Exit\n";

std::cout << "Enter your choice: ";

std::cin >> choice;

switch (choice) {

case 1:

std::cout << "Mean: " << [Link]() << std::endl;

break;

case 2:

std::cout << "Median: " << [Link]() << std::endl;

break;

case 3:

std::cout << "Variance: " << [Link]() << std::endl;

break;

case 4:

std::cout << "Standard Deviation: " << [Link]() << std::endl;

break;
case 0:

std::cout << "Exiting. Goodbye!\n";

break;

default:

std::cout << "Invalid choice. Try again.\n";

} while (choice != 0);

return 0;

You might also like