#include <stdio.h>
double average_positive(int* v, size_t n) {
double sum = 0.0;
size_t count = 0;
size_t i = 0;
if (v == NULL || n == 0) return 0;
do {
if (0 < v[i]) {
sum += v[i];
++count;
}
} while (++i != n);
return sum / count;
}
int main(void) {
int p[] = { 7, 5, 8, 7, 1, 4, -4, 0, 7, -8, 0, -9, 2 };
size_t n = sizeof(p) / sizeof(p[0]);
double ap = average_positive(p, n);
printf("Average: %.3lf\n", ap);
return 0;
}
#include <iostream>
void calculateAverageOfPositiveElements(const int* P, int size) {
int positiveCount = 0;
long totalSum = 0;
for (int i = 0; i < size; ++i) {
if (P[i] > 0) {
++positiveCount;
totalSum += P[i];
}
}
if (positiveCount == 0) {
std::cout << "Нет положительных элементов." << std::endl;
} else {
double average = static_cast<double>(totalSum) / positiveCount;
std::cout << "Среднее арифметическое положительных элементов: " << average << std::endl;
}
}
int main() {
int array[] = {7, 5, 8, 7, 1, 4, -4, 0, 7, -8, 0, -9, 2};
const int size = sizeof(array) / sizeof(array[0]);
calculateAverageOfPositiveElements(array, size);
return 0;
}