This code defines a program that accepts an array of...

August 26, 2025 at 10:30 AM

#include <stdio.h> #define NMAX 10 int input(int *a, int *n); void output(int *a, int n); int max(int *a, int n); int min(int *a, int n); double mean(int *a, int n); double variance(int *a, int n); void output_result(int max_v, int min_v, double mean_v, double variance_v); int main() { int n, data[NMAX]; if (input(data, &n) == 1) { output(data, n); output_result(max(data, n), min(data, n), mean(data, n), variance(data, n)); } else { printf("n/a"); } return 0; } int input(int *a, int *n) { if (scanf("%d", n) != 1 || *n <= 0 || *n > NMAX) { return 0; } for (int i = 0; i < *n; i++) { if (scanf("%d", &a[i]) != 1) { return 0; } } return 1; } void output(int *a, int n) { for (int i = 0; i < n; i++) { printf("%d", a[i]); if (i < n - 1) { printf(" "); } else { printf("\n"); } } } int max(int *a, int n) { int max_v = a[0]; for (int i = 1; i < n; i++) { if (a[i] > max_v) { max_v = a[i]; } } return max_v; } int min(int *a, int n) { int min_v = a[0]; for (int i = 1; i < n; i++) { if (a[i] < min_v) { min_v = a[i]; } } return min_v; } double mean(int *a, int n) { double sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; } return sum / (double)n; } double variance(int *a, int n) { double mean_v = mean(a, n); double sum = 0; for (int i = 0; i < n; i++) { sum += (a[i] - mean_v) * (a[i] - mean_v); } return sum / (double)n; } void output_result(int max_v, int min_v, double mean_v, double variance_v) { printf("%d %d %.6lf %.6lf\n", max_v, min_v, mean_v, variance_v); }

This code defines a program that accepts an array of integers, calculates statistical properties of the array (maximum, minimum, mean, and variance), and then outputs the results. Here is an explanation of its functionality, step by step:

  1. Includes and Definitions:

    • The program includes <stdio.h> for input and output.
    • It defines NMAX as 10, which is the maximum size of the array.
  2. Functions:

    • input: Reads the number of elements (n) and their values into the array (a). Returns 1 if the input is valid, otherwise 0.
    • output: Prints the elements of the array separated by spaces, followed by a newline.
    • max: Finds and returns the maximum value in the array.
    • min: Finds and returns the minimum value in the array.
    • mean: Calculates and returns the arithmetic mean of the array elements.
    • variance: Computes and returns the variance of the array elements.
    • output_result: Outputs the calculated results (maximum, minimum, mean, variance) with the specified formatting.
  3. Main Function:

    • Declares n (the number of elements) and an integer array data of size NMAX.
    • Calls the input function to get values for n and data. If successful, it:
      • Outputs the array using output.
      • Computes the maximum, minimum, mean, and variance of the array using respective functions.
      • Outputs the results using output_result.
    • If input is invalid, the program prints "n/a".
  4. Input Validation:

    • The input function first checks if n is valid (positive and less than or equal to NMAX), and then verifies that all n elements of the array have been properly provided.
  5. Statistical Computations:

    • The max function iterates through the array to find the largest value.
    • The min function iterates through the array to find the smallest value.
    • The mean function calculates the sum of the elements and divides by n to find the average.
    • The variance function uses the formula for variance: ((\text{sum of squared deviations from the mean}) / n).
  6. Output:

    • The array is output in a space-separated format.
    • The results are output as: <max> <min> <mean> <variance>, with the mean and variance formatted to 6 decimal places.

Example Execution:

Input:

5
1 2 3 4 5

Output:

1 2 3 4 5
5 1 3.000000 2.000000
  • The input function reads n = 5 and the array {1, 2, 3, 4, 5}.
  • The array is displayed as 1 2 3 4 5.
  • Maximum (max_v) is 5, minimum (min_v) is 1. Mean is (1+2+3+4+5)/5 = 3.000000. Variance is ((1−3)^2 + (2−3)^2 + (3−3)^2 + (4−3)^2 + (5−3)^2)/5 = 2.000000.

Invalid Input:

Input:

11

Output:

n/a
  • Here, n = 11 exceeds NMAX, so input is invalid, and the program outputs "n/a".

In summary, this program reads an integer array, computes its maximum, minimum, mean, and variance, and outputs these values. It validates the input and handles invalid cases gracefully.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node