5ii.cpp

来自「《C/C++程序设计导论(第二版)》一书的程序源文件」· C++ 代码 · 共 33 行

CPP
33
字号
#include <iostream.h>

// GetAveMaxMin()  Calculate the average, max, and min. of
// a list of pos. values terminating with a negative number
// ASSUMPTION: List must contain one positive value
//	OUT: 		max, min will be the list maximum & minimums
//	Returns: The average of the list

float GetAveMaxMin (float& max, float& min)
{ 	float sum, count, value;
	sum = 0.0;
	count = 0.0;
	cin >> value;					// get the first value
	max = value;
	min = value;					// assume it is both the max and min
	while (value > 0.0)					// check for sentinel
	{	sum = sum + value;
		count = count + 1.0;				// sum and count values in loop...
		if (value > max)				// compare against previous
			max = value;			//   assumptions for max and min..
		if (value < min)				//  (and change if needed)
			min = value;
		cin >> value;
	}
	return (sum / count);					// calculate and return average
}

void main ()
 { float ave, max, min;
	ave = GetAveMaxMin (max, min);
	cout << ave << " " << max << " " << min << endl;
 }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?