CSharp examples for Language Basics:Array
Calculate array value average
using System;/* w ww .j a v a 2 s .c o m*/ class ArrayMath { public static double ArrayAverage(double [] tempArray) { double sum = 0; foreach(double temp in tempArray) { sum += temp; } return sum / tempArray.Length; } public static int [] ArraySum(int [] tempArray1, int [] tempArray2) { int [] sumArray = new int [tempArray1.Length]; for(int i = 0; i < tempArray1.Length; i++) { sumArray[i] = tempArray1[i] + tempArray2[i]; } return sumArray; } public static int ArrayMax(int [] tempArray) { int maxValue = -2147483648; foreach(int temp in tempArray) { if(temp > maxValue) maxValue = temp; } return maxValue; } } class Tester { public static void Main() { double [] distances = {100, 200, 300}; int [] agesTeam1 = {10, 20, 30}; int [] agesTeam2 = {34, 38, 31}; int [] sumArray; Console.WriteLine("Average distance of distances array: {0}", ArrayMath.ArrayAverage(distances)); Console.WriteLine("Max age in agesTeam1 array: {0}", ArrayMath.ArrayMax(agesTeam1)); sumArray = ArrayMath.ArraySum(agesTeam1, agesTeam2); Console.WriteLine("sumArray's element values: {0} {1} {2}", sumArray[0], sumArray[1], sumArray[2]); } }