/***************************************************
Average of numbers
Write a program that reads in positive integers from
the user, each separated by a space, and the whole
terminated by a negative number (e.g. 3 22 10 -2),
and returns the average of the numbers entered (not
including the terminated negative number!). Note
that the average might not be an integer!
***************************************************/
#include <iostream>
using namespace std;
int main()
{
// initialization
cout << "Enter numbers separated by spaces"
<< " and terminated with a negative number."
<< endl;
int sum, k, num;
sum = 0;
num = 0;
cin >> k;
// Loop while the user enters more data
while (k >= 0)
{
sum = sum + k;
num = num + 1;
cin >> k;
}
// write out result
cout << "The sum is " << sum / double(num) << endl;
return 0;
}