C 语言的求极值求平均代码,网上也有!
// 求平均
float Average(float temp[])
{
float sum, average;
for (int i = 0; i < sizeof(temp); i++)
{
sum += temp[i];
}
average = sum / sizeof(temp); //求平均
return average;
}
// 去极值后求平均
float ExtremumAverage(float temp[])
{
float sum, average, max, min;
//获取到极值
for (int i = 0; i < sizeof(temp); i++)
{
if (max < temp[i]) //最大值
{
max = temp[i];
}
if (min > temp[i]) //最小值
{
min = temp[i];
}
}
//排除极值后求和
for (int i = 0; i < sizeof(temp); i++)
{
if (max != temp[i] && min != temp[i]) //去极值
{
sum += temp[i];
}
}
average = sum / (sizeof(temp) - 2); //求平均
return average;
}
// 去极值后求总和
float ExtremumSum(float temp[])
{
float sum, max, min;
//获取到极值
for (int i = 0; i < sizeof(temp); i++)
{
if (max < temp[i]) //最大值
{
max = temp[i];
}
if (min > temp[i]) //最小值
{
min = temp[i];
}
}
//排除极值后求和
for (int i = 0; i < sizeof(temp); i++)
{
if (max != temp[i] && min != temp[i]) //去极值
{
sum += temp[i];
}
}
return sum;
}
// 极值的最大值
float ExtremumMax(float temp[])
{
float max;
for (int i = 0; i < sizeof(temp); i++)
{
if (max < temp[i])
{
max = temp[i];
}
}
return max;
}
// 极值的最小值
float ExtremumMin(float temp[])
{
float min;
for (int i = 0; i < sizeof(temp); i++)
{
if (min > temp[i])
{
min = temp[i];
}
}
return min;
}
版权属于:zgcwkj
本文链接:https://blog.zgcwkj.cn/archives/144.html
转载声明:请注明本文章的标题及内容的出处和声明,谢谢
评论已关闭