Interquartile Range Iqr In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Interquartile Range Iqr In C++

Interquartile Range Iqr In C++

BLUF: Mastering Interquartile Range Iqr In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Interquartile Range Iqr In C++

C++ is renowned for its efficiency. Learn how Interquartile Range Iqr In C++ enables low-level control and high-performance computing in the tutorial below.

In C++, the IQR acronym denotes the interquartile range and serves as a statistical measure focused on determining the central tendency of a dataset. It is expressed mathematically as the difference between two values: IQR = Q3−Q1, where IQR represents the dispersion between the first quartile (Q1) and the third quartile (Q3). To begin the implementation in C++, it is necessary to organize the data in a specific order. Subsequently, the dataset is divided into two segments, commonly using the median to locate Q1 and Q3. The IQR's reduced sensitivity to extreme values makes it a valuable metric for measuring variability, enabling users to pinpoint outliers and provide detailed insights into the data's spread.

Formulae,

Example

IQR=Q3-Q1
  • Q1 (First Quartile): A data subset wherein 25% of the members are found below this value.
  • Q3 (Third Quartile): A data set value at which 75% of the members are found below this value.

IQR demonstrates characteristics of being less affected by extreme values, enhancing its resilience when detecting outliers and examining the distribution of data across various segments.

C++ implementation:

To compute the IQR in C++, we typically follow these steps:

  • Sort the Data: Arrange the dataset in ascending order.
  • Find Quartiles: Divide the sorted dataset into two halves to find Q1 and Q3.
  • Calculate IQR: Subtract Q1 from Q3.
  • Pseudo code:

Example

FUNCTION calculateMedian(data):
    SORT data in ascending order
    size ← length of data
    IF size is even THEN
        RETURN (data[size / 2 - 1] + data[size / 2]) / 2
    ELSE
        RETURN data[size / 2]

FUNCTION calculateIQR(data):
    SORT data in ascending order
    size ← length of data

    // Split data into lower and upper halves
    lowerHalf ← data[0] to data[size / 2 - 1]
    IF size is odd THEN
        upperHalf ← data[(size / 2) + 1] to data[size - 1]
    ELSE
        upperHalf ← data[size / 2] to data[size - 1]

    // Calculate Q1 and Q3
    Q1 ← calculateMedian(lowerHalf)
    Q3 ← calculateMedian(upperHalf)

    // Compute and return IQR
    RETURN Q3 - Q1

BEGIN MAIN:
    data ← INPUT dataset (e.g., {6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49})
    IQR ← calculateIQR(data)
    PRINT "The Interquartile Range (IQR) is: ", IQR
END MAIN

Example 1:

Let's consider a scenario to demonstrate the calculation of the interquartile range (IQR) in C++.

Example

#include <iostream>
#include <vector>
#include <algorithm>
double findMedian(const std::vector<int>& data) {
    int n = data.size();
    if (n % 2 == 0) {
        return (data[n / 2 - 1] + data[n / 2]) / 2.0;
    } else {
        return data[n / 2];
    }
}
double calculateIQR(std::vector<int> data) {
    // Sort the data
    std::sort(data.begin(), data.end());
    int n = data.size();
    std::vector<int> lowerHalf(data.begin(), data.begin() + n / 2);
    std::vector<int> upperHalf(data.begin() + (n + 1) / 2, data.end());
    double Q1 = findMedian(lowerHalf);
    double Q3 = findMedian(upperHalf);
    return Q3 - Q1;
}
int main() {
    std::vector<int> data = {10, 15, 14, 12, 18, 20, 22, 24, 25};
    double IQR = calculateIQR(data);
    std::cout << "The Interquartile Range (IQR) is: " << IQR << std::endl;
    return 0;
}

Output:

Output

The Interquartile Range (IQR) is: 10

Explanation:

  • Classifying: Using the std::sort function, data is sorted in ascending order.
  • Median's calculation: The median is computed using the find Median function to ascertain Q1 and Q3.
  • Lower and Upper Halves: Two portions of the dataset are used to calculate Q1 and Q3. If the number of elements is odd, the middle number is not part of either side.
  • How to calculate IQR: Calculate the IQR by deducting Q1 from Q3.
  • Example 2:

Let's consider another instance to demonstrate the interquartile range (IQR) within the C++ programming language.

Example

#include <iostream>
#include <vector>
#include <algorithm>

// Function to calculate the median of a given dataset
double calculateMedian(const std::vector<int>& data) {
    int size = data.size();
    if (size % 2 == 0) {
        return (data[size / 2 - 1] + data[size / 2]) / 2.0;
    } else {
        return data[size / 2];
    }
}

// Function to calculate the IQR
double calculateIQR(std::vector<int> data) {
    // Sort the dataset
    std::sort(data.begin(), data.end());

    int size = data.size();
    // Split the dataset into lower and upper halves
    std::vector<int> lowerHalf(data.begin(), data.begin() + size / 2);
    std::vector<int> upperHalf(data.begin() + (size + 1) / 2, data.end());

    // Compute Q1 and Q3
    double Q1 = calculateMedian(lowerHalf);
    double Q3 = calculateMedian(upperHalf);

    // Return the interquartile range
    return Q3 - Q1;
}

int main() {
    // Example dataset
    std::vector<int> data = {6, 7, 15, 36, 39, 40, 41, 42, 43, 47, 49};

    // Calculate the IQR
    double IQR = calculateIQR(data);

    // Display the result
    std::cout << "The Interquartile Range (IQR) is: " << IQR << std::endl;

    return 0;
}

Output:

Output

The Interquartile Range (IQR) is: 28

Explanation:

  • Sorting: The data is ensured to be organized in ascending order by the std::sort function.
  • Calculating the Median: In order to find Q1 and Q3, the calculateMedian function calculates the median value.
  • Dataset Splitting: The dataset will be divided evenly in half if its size is even. The centre element is left out of both halves if it is unusual.
  • Calculating IQR: ICR is calculated as follows: ICR = Q3−Q1.
  • Conclusion:

In summary, the IQR, which is short for the interquartile range, serves as a statistical measure that sheds light on how data values are spread out and the level of variability within the middle 50% of the dataset. To mitigate the impact of extreme values or outliers, the interquartile range is precisely calculated as the variance between Q1 and Q3. Through the utilization of C++, various sorting techniques, and the determination of the median, we can systematically analyze our data sets. This approach proves valuable in pinpointing outliers and understanding the data's diversity, particularly in domains like data analysis, machine learning, and scientific research.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience