// Sums the values in an array using a foreach loop
using System;
public class InitArr
{
static public void Main ()
{
DateTime now = DateTime.Now;
Random rand = new Random ((int) now.Millisecond);
int [] Arr = new int []
{rand.Next () % 100, rand.Next () % 100,
rand.Next () % 100, rand.Next () % 100,
rand.Next () % 100, rand.Next () % 100,
rand.Next () % 100, rand.Next () % 100,
rand.Next () % 100, rand.Next () % 100
};
int Total = 0;
Console.Write ("Array values are ");
foreach (int val in Arr)
{
Total += val;
Console.Write (val + ", ");
}
Console.WriteLine ("and the average is {0,0:F1}",
(double) Total / (double) Arr.Length);
}
}
|