[ create a new paste ] login | about

Link: http://codepad.org/nImJn1UQ    [ raw code | output | fork ]

C, pasted on Jul 21:
#include <stdio.h>
#include <float.h>

#define DOUBLE_ELEMENT		(5)

// 最大値を探索する関数
// 戻り値が最大値
double serchMaxValue(double *pList, int length)
{
	int i;
	double max	= DBL_MIN;

	for(i=0; i<length; ++i)
	{
		if(max < *pList) {
			max = *pList;
		}
		++pList;
	}

	return max;
}
// 最小値を探索する関数
// 戻り値が最小値
double serchMinValue(double *pList, int length)
{
	int i;
	double min	= DBL_MAX;

	for(i=0; i<length; ++i)
	{
		if(min > *pList) {
			min = *pList;
		}
		++pList;
	}

	return min;
}
// 平均値を算出する関数
// 戻り値が平均値
double calcAverageValue(double *pList, int length)
{
	int i;
	double avrg	= 0.0;

	for(i=0; i<length; ++i)
	{
		avrg+=*pList;
		++pList;
	}

	return avrg/length;
}

// メイン関数
int main(void)
{
	// 要素数nの配列
	double ldList[DOUBLE_ELEMENT] = { 2.0, 3.0, 1.0, 5.0, 4.0 };

	double lMaxValue = 0.0;		// 最大値
	double lMinValue = 0.0;		// 最小値
	double lAvrgValue = 0.0;	// 平均値
	int lListLength = 0;

	lListLength = sizeof(ldList) / sizeof(double);			// リストの要素数を算出
	
	lMaxValue = serchMaxValue( ldList, lListLength );		// 最大値を探索
	lMinValue = serchMinValue( ldList, lListLength );		// 最小値を探索
	lAvrgValue = calcAverageValue( ldList, lListLength );	// 平均値を算出

	printf("最大値は%.3fです。\n", lMaxValue);
	printf("最小値は%.3fです。\n", lMinValue);
	printf("平均値は%.3fです。\n", lAvrgValue);

	return 0;
}


Output:
1
2
3
最大値は5.000です。
最小値は1.000です。
平均値は3.000です。


Create a new paste based on this one


Comments: