Dynamic Sensor Buffer

#include <iostream>

int main() {
    int n;
    // 1. Read the number of samples
    if (!(std::cin >> n) || n < 1) {
        return 0;
    }

    // 2. Dynamically allocate an array of size n
    int* nn = new int[n];
    
    int sum = 0;
    // 3. Read n readings into the buffer
    for(int i = 0; i < n; i++) {
        std::cin >> nn[i];
        sum += nn[i];
    }

    // 4. Compute and print the average
    int avg = sum / n;
    std::cout << avg << std::endl;

    // 5. Release memory correctly using delete[]
    delete[] nn; 

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

5 10 20 30 40 50

Expected Output

30