Problem 4 Solution: Average With A For-Loop

One easy way to avoid integer division is to define the running total (i.e., the accumulator) variable as a double. Although the user enters integers, the program automatically converts or promotes them to doubles before adding them to the running total. Alternatively, you can also write the last statement as return (double)sum / 10;

double ave1()
{
	double	sum = 0;

	for (int i = 0; i < 10; i++)
	{
		int	number;

		cout << "Enter a number: ";
		cin >> number;
		sum += number;
	}

	return sum / 10;
}
ave1: for-loop based function calculating a mean or average.