Sums the values in an array using a foreach loop 1 : Array : Collections Data Structure C# Source Code


Custom Search

C# Source Code » Collections Data Structure » Array »

 

Sums the values in an array using a foreach loop 1









    

// 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);
        }
    }


           
       
    
   
  
   







HTML code for linking to this page:

Follow Navioo On Twitter

C# Source Code

 Navioo Collections Data Structure
» Array