Print the Range
In this final step, you'll learn how to format and display the range calculation results in a user-friendly manner, adding some statistical context to the output.
Let's create an enhanced version of our range calculation program:
#include <stdio.h>
#define MAX_SIZE 100
// Function to swap two elements
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Bubble sort function to sort the array
void sortArray(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
// Function to calculate the range
int calculateRange(int arr[], int size) {
// If array is empty, return 0
if (size == 0) return 0;
// Range is the difference between last (max) and first (min) elements
return arr[size - 1] - arr[0];
}
// Function to print detailed range information
void printRangeAnalysis(int arr[], int size, int range) {
printf("\n--- Statistical Range Analysis ---\n");
printf("Dataset Size: %d\n", size);
printf("Minimum Value: %d\n", arr[0]);
printf("Maximum Value: %d\n", arr[size - 1]);
printf("Range: %d\n", range);
// Additional insights
printf("\nInterpretation:\n");
printf("The range represents the spread of values in the dataset.\n");
printf("A larger range indicates more variability in the data.\n");
}
int main() {
int numbers[MAX_SIZE];
int 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 (int i = 0; i < n; i++) {
scanf("%d", &numbers[i]);
}
// Sort the array
sortArray(numbers, n);
// Calculate the range
int range = calculateRange(numbers, n);
// Print detailed range analysis
printRangeAnalysis(numbers, n, range);
return 0;
}
Key improvements in this step:
- Added
printRangeAnalysis()
function to provide a comprehensive output
- Included additional context about the range and data variability
- Formatted the output to be more informative and readable
Example compilation and execution:
gcc -o range_analysis range_analysis.c
./range_analysis
Example output:
Enter the number of elements (max 100): 6
Enter 6 integers:
10 25 7 42 15 33
--- Statistical Range Analysis ---
Dataset Size: 6
Minimum Value: 7
Maximum Value: 42
Range: 35
Interpretation:
The range represents the spread of values in the dataset.
A larger range indicates more variability in the data.
The program now provides a more comprehensive view of the dataset's range, helping users understand the statistical significance of the calculation.