目录

Average

一组数字的平均值是它们的总和除以它们的数量。 它可以定义为 -

average = sum of all values/number of values

在这里,我们将学习如何以编程方式计算平均值。

算法 (Algorithm)

这个程序的算法很简单 -

START
   Step 1 → Collect integer values in an array A of size N
   Step 2 → Add all values of A
   Step 3 → Divide the output of Step 2 with N
   Step 4 → Display the output of Step 3 as average
STOP

伪代码 (Pseudocode)

让我们为驱动算法写伪代码 -

procedure average()
   Array A
   Size  N
   FOR EACH value i of A
      sum ← sum + A[i]
   END FOR
   average = sum/N
   DISPLAY average
end procedure

实现 (Implementation)

该算法的实现如下 -

#include <stdio.h>
int main() {
   int i,total;
   int a[] = {0,6,9,2,7};
   int n = 5;
   total = 0;
   for(i = 0; i < n; i++) {
      total += a[i];
   }
   printf("Average = %f\n", total/(float)n);
   return 0;
}

输出 (Output)

该方案的产出应该是 -

Average = 4.800000
↑回到顶部↑
WIKI教程 @2018