Print the IQR
In this final step, we'll focus on formatting and presenting the Interquartile Range (IQR) results in a clear and informative manner.
Open the previous source file:
cd ~/project
nano iqr_calculation.c
Update the code to enhance IQR output and add some descriptive text:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_SIZE 100
// Previous functions remain the same (compare and calculateQuartile)
int main() {
int numbers[MAX_SIZE];
int n, i;
double q1, q3, iqr;
// Clear screen for better presentation
printf("\033[2J\033[1;1H");
// Introduction to IQR
printf("Interquartile Range (IQR) Calculator\n");
printf("=====================================\n\n");
// Read the number of elements
printf("Enter the number of elements (max %d): ", MAX_SIZE);
scanf("%d", &n);
// Input array elements
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}
// Sort the array
qsort(numbers, n, sizeof(int), compare);
// Calculate Q1 and Q3 positions
double q1_pos = 0.25 * (n + 1);
double q3_pos = 0.75 * (n + 1);
// Calculate Q1 and Q3
q1 = calculateQuartile(numbers, n, q1_pos);
q3 = calculateQuartile(numbers, n, q3_pos);
// Calculate IQR
iqr = q3 - q1;
// Detailed output
printf("\nData Analysis Results\n");
printf("--------------------\n");
printf("Original Data Set: ");
for (i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
printf("\n\n");
// Formatted IQR output
printf("Quartile Analysis:\n");
printf("1st Quartile (Q1): %.2f\n", q1);
printf("3rd Quartile (Q3): %.2f\n", q3);
printf("Interquartile Range (IQR): %.2f\n", iqr);
// Interpretation of IQR
printf("\nInterpretation:\n");
printf("The IQR represents the spread of the middle 50%% of the data.\n");
printf("A smaller IQR indicates more consistent data,\n");
printf("while a larger IQR suggests more variability.\n");
return 0;
}
Compile the updated program:
gcc -o iqr_calculation iqr_calculation.c -lm
Run the program and provide an example input:
./iqr_calculation
Example output:
Interquartile Range (IQR) Calculator
=====================================
Enter the number of elements (max 100): 7
Enter 7 integers:
12 15 18 22 25 30 35
Data Analysis Results
--------------------
Original Data Set: 12 15 18 22 25 30 35
Quartile Analysis:
1st Quartile (Q1): 15.00
3rd Quartile (Q3): 30.00
Interquartile Range (IQR): 15.00
Interpretation:
The IQR represents the spread of the middle 50% of the data.
A smaller IQR indicates more consistent data,
while a larger IQR suggests more variability.
Key improvements:
- Added clear screen command for better presentation
- Enhanced output formatting
- Included an interpretation of IQR
- Maintained previous calculation logic