1. 程式人生 > >一個數組計算平均值

一個數組計算平均值

#include <iostream>
using namespace std;


double average(double sorce[], int n)
{
	double aver = 0;

	for (int i=0; i<n; i++)
	{
		aver = sorce[i] + aver;
	}
	aver = aver/n;
	return aver;
}

int main()
{
	double a[6] = {1, 2, 3, 4, 5, 6};
		
	cout << "average =" << average(a, 6) << endl;
	return 0;
}