POP Lab Manual

Program 11

Develop a program using pointers to compute the sum, mean and standard deviation of all elements stored in an array of N real numbers. This program demonstrates advanced pointer arithmetic, statistical calculations, and memory management techniques for numerical data analysis.
Statistical Formulas:
Sum = Σ(xi) where i = 1 to N
Mean = Sum / N
Standard Deviation = √(Σ(xi - mean)² / N)
Pointer Access: *(p + i) equivalent to arr[i]

Algorithm

  1. Input Array Size: Read N (number of elements) from user
  2. Declare Array: Create array of N double elements
  3. Initialize Pointer: Set pointer p to point to array base address
  4. Input Elements: Use pointer arithmetic to read N real numbers
  5. Calculate Sum: Traverse array using pointer and accumulate sum
  6. Compute Mean: Divide total sum by number of elements
  7. Calculate Variance: Find sum of squared differences from mean
  8. Compute Standard Deviation: Take square root of variance
  9. Display Results: Print sum, mean, and standard deviation

Flowchart

START
Input N
Initialize Pointer
Read Array Elements
Calculate Sum
Compute Mean
Calculate SD
Display Results
STOP

Code

#include <stdio.h>
#include <math.h>

int main() {
    int n, i;
    double sum = 0, mean, sd = 0;

    printf("Enter number of elements: ");
    scanf("%d", &n);

    double arr[n];
    double *p = arr;  // pointer to array

    printf("Enter %d real numbers:\n", n);
    for (i = 0; i < n; i++) {
        scanf("%lf", (p + i));  // using pointer
        sum += *(p + i);
    }

    // Calculate mean
    mean = sum / n;

    // Calculate standard deviation
    for (i = 0; i < n; i++) {
        sd += pow(*(p + i) - mean, 2);
    }
    sd = sqrt(sd / n);

    // Output
    printf("\nSum = %.2lf\n", sum);
    printf("Mean = %.2lf\n", mean);
    printf("Standard Deviation = %.2lf\n", sd);

    return 0;
}

Output


// Sample Output 1 - Basic Statistical Analysis
Enter number of elements: 5
Enter 5 real numbers:
10.5
20.3
15.7
25.1
18.4

Sum = 90.00
Mean = 18.00
Standard Deviation = 5.02

// Sample Output 2 - Uniform Data Set
Enter number of elements: 4
Enter 4 real numbers:
100.0
100.0
100.0
100.0

Sum = 400.00
Mean = 100.00
Standard Deviation = 0.00

// Sample Output 3 - Wide Range Data
Enter number of elements: 6
Enter 6 real numbers:
5.5
12.8
45.2
67.9
23.1
89.7

Sum = 244.20
Mean = 40.70
Standard Deviation = 29.85

// Sample Output 4 - Small Decimal Values
Enter number of elements: 3
Enter 3 real numbers:
0.25
0.75
0.50

Sum = 1.50
Mean = 0.50
Standard Deviation = 0.20

// Sample Output 5 - Negative and Positive Mix
Enter number of elements: 7
Enter 7 real numbers:
-10.5
15.2
-5.8
22.7
-12.3
8.9
-3.4

Sum = 14.80
Mean = 2.11
Standard Deviation = 13.45

// Sample Output 6 - Large Numbers
Enter number of elements: 4
Enter 4 real numbers:
1250.75
2340.50
1890.25
2100.00

Sum = 7581.50
Mean = 1895.38
Standard Deviation = 389.42

// Sample Output 7 - Single Element
Enter number of elements: 1
Enter 1 real numbers:
42.5

Sum = 42.50
Mean = 42.50
Standard Deviation = 0.00