GVKun编程网logo

如何计算数组中元素的总和和平均值?(如何计算数组中元素的总和和平均值)

25

对于想了解如何计算数组中元素的总和和平均值?的读者,本文将提供新的信息,我们将详细介绍如何计算数组中元素的总和和平均值,并且为您提供关于awk计算数据的和和平均值、C/C++程序用于找到给定数组中元素

对于想了解如何计算数组中元素的总和和平均值?的读者,本文将提供新的信息,我们将详细介绍如何计算数组中元素的总和和平均值,并且为您提供关于awk 计算数据的和和平均值、C/C++程序用于找到给定数组中元素的总和、java – 数组中元素的总和、javascript – JS计算2d数组中相同元素的平均值的有价值信息。

本文目录一览:

如何计算数组中元素的总和和平均值?(如何计算数组中元素的总和和平均值)

如何计算数组中元素的总和和平均值?(如何计算数组中元素的总和和平均值)

我在添加数组的所有元素以及将它们取平均值时遇到了问题。我该怎么做,并用我现在拥有的代码实现它?这些元素应该定义如下。

<script type="text/javascript">//<![CDATA[var i;var elmt = new Array();elmt[0] = "0";elmt[1] = "1";elmt[2] = "2";elmt[3] = "3";elmt[4] = "4";elmt[5] = "7";elmt[6] = "8";elmt[7] = "9";elmt[8] = "10";elmt[9] = "11";// Problem herefor (i = 9; i < 10; i++){  document.write("The sum of all the elements is: " + /* Problem here */ + " The average of all the elements is: " + /* Problem here */ + "<br/>");}//]]></script>

答案1

小编典典

var sum = 0;
for( var i = 0; i < elmt.length; i++ ){
sum += parseInt( elmt[i], 10 ); //don’t forget to add the base
}

var avg = sum/elmt.length;document.write( "The sum of all the elements is: " + sum + " The average is: " + avg );

只需遍历数组,因为您的值是字符串,因此必须先将它们转换为整数。平均值就是值的总和除以值的数量。

答案2

小编典典

我认为更优雅的解决方案:

const sum = times.reduce((a, b) => a + b, 0);const avg = (sum / times.length) || 0;console.log(`The sum is: ${sum}. The average is: ${avg}.`);

awk 计算数据的和和平均值

awk 计算数据的和和平均值

awk 计算数据的和和平均值

2014年12月02日 21:11:12 HaveFunInLinux 阅读数:14487更多
个人分类: 小技巧
 

本文译至:http://d.hatena.ne.jp/leetmikeal/20130117/1358423717

有如下的log文件:


最右边的数值表示数据。计算一下数据的和。

最开始的{} ,定义sum变量,累加数据。

最后,在END内 print sum 输出和.

然后,计算一下平均值。

C/C++程序用于找到给定数组中元素的总和

C/C++程序用于找到给定数组中元素的总和

c/c++程序用于找到给定数组中元素的总和

所有数组元素的总和意味着将所有数组元素相加。假设我们有5个数组元素,我们想找到它们的总和。

arr[0]=1
arr[1]=2
arr[2]=3
arr[3]=4
arr[4]=5
登录后复制

以上所有元素的总和为

arr[0]+arr[1]+arr[2]+arr[3]+ arr[4]=1+2+3+4+5=15
登录后复制
登录后复制

立即学习“C++免费学习笔记(深入)”;

立即学习“C++免费学习笔记(深入)”;

Input:1,2,3,4,5
Output:15
登录后复制

Explanation

使用For循环来遍历每个索引元素并将它们相加

arr[0]+arr[1]+arr[2]+arr[3]+ arr[4]=1+2+3+4+5=15
登录后复制
登录后复制

示例

#include <iostream>
using namespace std;
int main() {
   int i,n,sum=0;
   int arr[]={1,2,3,4,5};
   n=5;
   for(i=0;i<n;i++) {
      sum+=arr[i];
   }
   cout<<sum;
   return 0;
}
登录后复制

以上就是C/C++程序用于找到给定数组中元素的总和的详细内容,更多请关注php中文网其它相关文章!

java – 数组中元素的总和

java – 数组中元素的总和

我正在为一个夏季 java课程做一个简单的任务,只是希望你们可以看看我的代码,看看我做的方式是否是最好的方法.目的是创建一个包含至少25个元素的简单int数组,并使用循环遍历它并添加所有元素.我遇到了一些问题,但看起来我已经开始工作了.在我解决之后,我做了一些研究,看到了一些类似的东西,人们使用For Each循环(增强循环).这会是一个更好的选择吗?我对使用反对常规for循环的最佳方法感到困惑.

无论如何,任何评论或批评,帮助我成为一个更好的程序员!

public class Traversals {

    public static void main(String[] args) {

        int absencetotal = 0;
        // initialize array with 30 days of absences.
        int absencesArr[] = { 1,3,9,8,23,1,11,5,6,7,10,14,2,4,2 };

        for (int i = 0; i < absencesArr.length; i++) {
            absencesArr[i] += absencetotal;
            absencetotal = absencesArr[i];
        }
        System.out.println("There were " + absencetotal + " absences that day.");
    }
}

解决方法

不要修改数组.我更喜欢 for-each loop.你应该考虑到可能会有很多学生,所以我可能会花很多钱.并格式化输出.将它们组合成类似的东西
long sum = 0;
for(int i : absencesArr) {       
    sum += i;
}   
// System.out.println("There were " + sum + " absences that day.");   
System.out.printf("There were %d absences that day.%n",sum);

javascript – JS计算2d数组中相同元素的平均值

javascript – JS计算2d数组中相同元素的平均值

我有一个由两列表示为数组的“表”.第一列是1到20的数字,它们是标签,第二列是相应的值(秒):
my_array = [ [ 3,4,5,3,2 ],[ 12,14,16,11,12,10,20 ] ];

我需要每个标签的平均值(平均值):

my_mean_array = [ [ 2,5 ],[ 20/1,(12+11)/2,(14+12)/2,(16+10)/2 ] ];
// edit: The mean should be a float - the notion above is just for clarification.
// Also the number 'labels' should remain as numbers/integers.

我的尝试:

var a = my_array[0];
var b = my_array[1];
m = [];
n = [];
for( var i = 0; a.length; i++){
    m[ a[i] ] += b[i]; // accumulate the values in the corresponding place
    n[ a[i] ] += 1; // count the occurences
}
var o = [];
var p = [];
o = m / n;
p.push(n);
p.push(o);

解决方法

怎么样(原生JS,不会破坏旧版浏览器):
function arrayMean(ary) {
  var index = {},i,label,value,result = [[],[]];

  for (i = 0; i < ary[0].length; i++) {
    label = ary[0][i];
    value = ary[1][i];
    if (!(label in index)) {
      index[label] = {sum: 0,occur: 0};
    }
    index[label].sum += value;
    index[label].occur++;
  }
  for (i in index) {
    if (index.hasOwnProperty(i)) {
      result[0].push(parseInt(i,10));
      result[1].push(index[i].occur > 0 ? index[i].sum / index[i].occur : 0);
    }
  }
  return result;
}

FWIW,如果你想要花哨我已经创造了一些其他方法来做到这一点.它们依赖于外部库,并且很可能比原生解决方案慢一个数量级.但他们看起来更好.

看起来像这样,用underscore.js:

function arrayMeanUnderscore(ary) {
  return _.chain(ary[0])
    .zip(ary[1])
    .groupBy(function (item) { return item[0]; })
    .reduce(function(memo,items) {
      var values = _.pluck(items,1),toSum = function (a,b) { return a + b; };

      memo[0].push(items[0][0]);
      memo[1].push(_(values).reduce(toSum) / values.length);
      return memo;
    },[[],[]])
    .value();
}

// --------------------------------------------

arrayMeanUnderscore([[3,2],[12,20]]);
// -> [[2,5],[20,11.5,13,13]]

或者像这样,真正伟大的linq.js(我用过v2.2):

function arrayMeanLinq(ary) {
  return Enumerable.From(ary[0])
    .Zip(ary[1],"[$,$$]")
    .GroupBy("$[0]")
    .Aggregate([[],[]],function (result,item) {
      result[0].push(item.Key());
      result[1].push(item.Average("$[1]"));
      return result;
    });
}

// --------------------------------------------

arrayMeanLinq([[3,20]]);
// -> [[3,[11.5,20]]

正如所怀疑的那样,“花哨”实现比本机实现慢一个数量级:jsperf comparison.

今天关于如何计算数组中元素的总和和平均值?如何计算数组中元素的总和和平均值的讲解已经结束,谢谢您的阅读,如果想了解更多关于awk 计算数据的和和平均值、C/C++程序用于找到给定数组中元素的总和、java – 数组中元素的总和、javascript – JS计算2d数组中相同元素的平均值的相关知识,请在本站搜索。

本文标签: