Arrays and Random Numbers


 ///////////////////////////////////////////////
// Main program class
// arrays
//
// create array
// put numbers into array: seq, zero, random
// print array
//
////////////////////////////////////////////////
using System;   

   class Program
   {
      static void Main(string[] args)
      {
         int size; // size of array
         ReadNums Num = new ReadNums( );  
      
         // get size of array from user
         size = Num.ReadInt( "Size of random number array" );
         if ( size > 0 )
         {
            // create new array object
            Array myArray1 = new Array( size );
            
            // call methods to fill and print arrray with random nums
            myArray1.GenerateRands( );
            myArray1.PrintNums( );
         }

         // get size of array from user
         size = Num.ReadInt( "Size for sequential number array" );
         if ( size > 0 )
         {
            // create new array object
            Array myArray2 = new Array( size );
            // call methods to fill and print arrray with seq nums
            myArray2.GenerateNums( );
            myArray2.PrintNums( );
         }

         // get size of array from user
         size = Num.ReadInt( "Size for zeroed array" );
         if ( size > 0 )
         {
            // create new array object
            Array myArray3 = new Array( size );
            // call methods to fill and print arrray with zeroes
            myArray3.GenerateZeroes( );
            myArray3.PrintNums( );
         }
         
         // hold...
         Console.ReadKey() ;
      }
   }

 

   //////////////////////////////////////////////
   // creates array , fills with numbers  prints
   ////////////////////////////////////////////
   public class Array
   {
      // vars used by many methods
      int Count;     // # elements in the array (set in constructor) 
                     // (array already has this property - just for demonstartion
      int[] Nums;    // actual array of ints
      int i;         // counter

      // constructor
      public Array( int NumberofItems )
      {
         Count = NumberofItems; // same as Length
         Nums = new int[ Count ];
      }

      // method to fill array with 0s
      public void GenerateZeroes( )
      {
         // fill array
         for ( i = 0; i < Count; i++ )
            Nums[ i ] = 0;
      }

      // method to fill array with seq #s
      public void GenerateNums( )
      {
         // fill array
         for ( i = 0; i < Count; i++ )
            Nums[ i ] = i;
      }

      // method to fill array with random #s
      public void GenerateRands( )
      {
         // range of randoms
         int min, max;
         int maxsofar = int.MinValue; // make max SMALL
         int minsofar = int.MaxValue; // make min BIG
         
         // create objects
         Random rnd = new Random( );      // random number obj     
                  
         // fill array
         for ( i = 0; i < Count; i++ )
         {
            // get random num and place in array at element indexed by i
            Nums[ i ] = rnd.Next(  );            
            
            // see if new element is biggest or smallest
            if (Nums[i] > maxsofar)
               maxsofar = Nums[i];   
            if (Nums[i] < minsofar)
               minsofar = Nums[i];               
         }
         
         // formatted:    force commas
         //               # means show digit if not zero 
         //               0 means always show a digit 
         Console.WriteLine("Biggest: i: {0:##,##0}", maxsofar);
         Console.WriteLine("Smallestt: i: {0:##,##0}", minsofar);
      }

      // method to print numbers
      public void PrintNums( )
      {
         // print array
         for ( i = 0; i < Count; i++ )  // could have used Nums.Length rather than Count
         {
             Console.WriteLine( i.ToString( ) + "  " +  Nums[i].ToString( ) ) ;
         }
      }
   }

 
   ////////////////////////////////////////////
   //  gets input from user
   //  in a loop keeps asking
   //  until valid integer is entered
   /////////////////////////////////////////////
   public class ReadNums
   {
      public int ReadInt( string prompt )
      {
         string s;
         int num;
         do
         {
            Console.Write( prompt + ": " );
            s =  Console.ReadLine( );
           // keep looping if it can't convert the input to an integer
         } while ( int.TryParse( s, out num ) ==  false );
         return num;
      }
   }