Creating Arrays - CSharp Language Basics

CSharp examples for Language Basics:Array

Description

Creating Arrays

Demo Code

using System;//w  w w.  jav a2 s  .  c o  m
public class MainClass
{
   public static void Main()
   {
      decimal[] balances = new decimal[12];
      decimal ttl = 0m;
      System.Random rnd = new System.Random();
      // Put random values from 0 to 100000 into balances array
      for (int indx = 0; indx < 12; indx++ )
      {
         balances[indx] = (decimal) (rnd.NextDouble() * 10000);
      }
      //values are initialized in balances
      for( int indx = 0; indx < 12; indx++ )
      {
         Console.WriteLine("Balance {0}: {1}", indx, balances[indx]);
         ttl += balances[indx];
      }
      Console.WriteLine("Total of Balances = {0}", ttl);
      Console.WriteLine("Average Balance = {0}", (ttl/12));
   }
}

Result


Related Tutorials