#include <iostream>
usingnamespace std;
double average(int a[], int n);
int main(void)
{
int a[5] = {2, 0, -4, -5, 20};
double r = average(a, 5);
if (r == -1) cout << "The array has no positive integers" <<
endl;
else cout << "The average of positive integers in the array is "
<< r << endl;
return 0;
}
double average(int a[], int n)
{
int sum = 0;
int i = 0;
int count = 0;
while(true) {
if (a[i] > 0) {
sum += a[i];
count++;
}
elsecontinue;
i++;
if (i == n) break;
}
if (count > 0) return sum/count;
return -1;
}
If the program just closes, it's because maybe you're not blocking the program at the end (It does everything you tell him: Display text and end the function, but once the function ends it closes itself, it doesn't wait for anything unless you tell him!). You should put a getch() or whatever that function was, before the "return 0" in your main() function.