arrays as parameters

Arrays are a collection of like data with a single name.
Elements of the array are accessed using the array name and a unique array index.

 


. arrays as parameters
Remember... int [] numbers = null; // creates a REFERENCE called numbers. numbers = new int[100]; // When an array is passed, the reference to the array is passed
   class ArrayClass  // FROM MSDN  http://msdn.microsoft.com/en-us/library/hyfeyz71(v=vs.80).aspx
   {
       static void PrintArray(string[] arr)
       {
           for (int i = 0; i < arr.Length; i++)
           {
               // put a space after Sun thru Fri, but not after Sat
               System.Console.Write(arr[i] + "{0}", i < arr.Length - 1 ? " " : "");
           }
           System.Console.WriteLine();
       }

       static void Main()
       {
           // Declare and initialize an array:
           string[] weekDays = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

           // Pass the array as a parameter:
           PrintArray(weekDays);
       }
   }   
   
----------------------------------- OUTPUT: Sun Mon Tue Wed Thu Fri Sat

. fibonaccis
   
   /*
    create a method called fib that has 2 params:
      an int array
      an int telling how many cells in the array to fill

      start with 0, 1  (0, 1, 1, 2, 3, 5, 8, etc...)
      fill in the array with the fib sequence

    create a method called fibStart that has 3 params:
      an int array
      an int telling how many cells in the array
      an int starting num

      fill in the array with the fib sequence
      
     create a method to print specified number in array
   */

   using System;
   class Demo
   {
      static void Main( )
      {
        int[] fibby10 = new int[ 10 ];
        int[] fibby20 = new int[ 20 ];

        // 10 fibbys starting at 0
        fib( fibby10, 10 );
        print( fibby10, 10 );

        // 20 fibs starting at 2
        fibStart( fibby20, 20, 2 );
        print( fibby20, 20 );
        
        //15 fibs starting at 3, print only 10
        fibStart( fibby20, 15, 3 );
        print( fibby20, 10 );

        }
   
      // creates fibbys starting at 0
      static void fib( int[] list, int sz )
      {
        // if request is too big, adjust
        if (sz > list.Length)
           sz = list.Length;
           
        // prime the pump and adjust sz
        list[ 0 ] = 0;
        list[ 1 ] = 1;
        // sz -= 2; // good job wyatt from jordan b (limit of size should not be adjusted)
        for ( int i=2; i<sz; i++ )
           list[ i ] = list[ i-1 ] + list[ i-2 ];
        return;
      }
      
      //create fibbys start at 'start'
      static void fibStart( int[] list, int sz, int start )
      {
        // if request is too big, adjust
        if (sz > list.Length)
           sz = list.Length;      
      
        // prime the pump and adjust sz
        list[ 0 ] = start;
        list[ 1 ] = start + 1;
        //sz -= 2;
        for ( int i=2; i<sz; i++ )
           list[ i ] = list[ i-1 ] + list[ i-2 ];
        return;
      }
      
      // print the specified amount of list of fibbys      
      static void print( int[] list, int sz )
      {
        // if request is too big, adjust
        if (sz > list.Length)
           sz = list.Length;
           
        for ( int i=0; i<sz; i++ )
           Console.WriteLine(list[ i ] + " ");
        Console.WriteLine("\n\n");
        return;
      }
   }
   

. parallel arrays
   
   using System;
   ///////////////////////////////////////////////////////////////////
   // parallel arrays of random integers and bools that indicate if even
   // messing around with 
   //      output formatting (padding and commas etc)
   //      summing & checking for overflow
   //      averaging
   class Program
   {
       static void Main()
       {
           //release the hounds!
           const int SIZE = 1000; // array size, loop counter, etc...
           int biggest = int.MinValue, smallest = int.MaxValue;
           long sum=0;

           //arrays
           bool[] evenList = new bool[SIZE];
           int[] list = new int[SIZE];

           // random object
           Random rnd = new Random();

           // fill the array with random nums 0 to 2^31-1
           for (int i=0; i < SIZE; i++)
               list[i] = rnd.Next();

           // go thru & calculate sum & find biggest & smallest
           for (int i=0; i < SIZE; i++) 
           {
               if (list[i] > biggest)
                   biggest = list[i];
               if (list[i] < smallest)
                  smallest = list[i];            
           
               // checks for OVERFLOW! throws exception if too big
               checked 
               { 
                  sum += list[i]; // should have a try-catch
               } 
           }

           // fill parallel array telling if each number in list  is even
           for (int i=0; i < SIZE; i++)
               evenList[i] = IsItEven(list[i]);

           // print arrays UGH
           for (int i=0; i < SIZE; i++)
               Console.WriteLine( i.ToString("#,##0").PadRight(6) + " " + 
                                  evenList[i].ToString().PadRight(7) + "\t " +  
                                  list[i].ToString("#,###").PadLeft(15) );

           // output all sorts of numbers playing with formatting
           Console.WriteLine("\nMin and max int are:\t" + int.MinValue.ToString("0,000") + 
                             " and  " + int.MaxValue.ToString("0,000"));
           Console.WriteLine("\nSmallest and biggest nums generated are: " + 
                             smallest.ToString("#,##0") + " and " + 
                             biggest.ToString("#,000"));
           Console.WriteLine("\n\tSum is:\t\t\t\t\t" + sum.ToString("#,###.00").PadLeft(30));
       }

       static bool IsItEven(int num)
       {
         //                
         return (num%2 == 0);
       }  
    }