Print the Value
In this final step, we'll enhance our Normal Distribution PDF program by adding more detailed output formatting and visualization options.
Update the normal_pdf.c
file:
nano ~/project/normal_pdf.c
Replace the previous content with:
#include <stdio.h>
#include <math.h>
double compute_normal_pdf(double x, double mean, double std_dev) {
double pi = 3.14159265358979323846;
double coefficient = 1.0 / (std_dev * sqrt(2 * pi));
double exponent = -pow(x - mean, 2) / (2 * pow(std_dev, 2));
return coefficient * exp(exponent);
}
void print_pdf_details(double x, double mean, double std_dev, double pdf) {
printf("\n--- Normal Distribution PDF Details ---\n");
printf("Point (x) : %.2f\n", x);
printf("Mean (μ) : %.2f\n", mean);
printf("Standard Deviation (σ) : %.2f\n", std_dev);
printf("Probability Density (f(x)): %.6f\n", pdf);
// Add interpretation of the PDF value
if (pdf > 0.3) {
printf("Interpretation: High probability density\n");
} else if (pdf > 0.1) {
printf("Interpretation: Moderate probability density\n");
} else {
printf("Interpretation: Low probability density\n");
}
}
int main() {
double x, mean, std_dev, pdf;
printf("Normal Distribution Probability Density Function Calculator\n");
printf("Enter the point x: ");
scanf("%lf", &x);
printf("Enter the mean (μ): ");
scanf("%lf", &mean);
printf("Enter the standard deviation (σ): ");
scanf("%lf", &std_dev);
// Compute PDF
pdf = compute_normal_pdf(x, mean, std_dev);
// Print detailed results
print_pdf_details(x, mean, std_dev, pdf);
return 0;
}
Compile the program:
gcc ~/project/normal_pdf.c -o ~/project/normal_pdf -lm
Run the program:
~/project/normal_pdf
Example output:
Normal Distribution Probability Density Function Calculator
Enter the point x: 2.5
Enter the mean (μ): 0
Enter the standard deviation (σ): 1
--- Normal Distribution PDF Details ---
Point (x) : 2.50
Mean (μ) : 0.00
Standard Deviation (σ) : 1.00
Probability Density (f(x)): 0.017528
Interpretation: Low probability density
Key enhancements in this implementation:
- Added a dedicated function
print_pdf_details()
for formatted output
- Included a simple interpretation of the PDF value
- Improved user interface with a descriptive title
- Maintained the core PDF calculation logic from previous steps