Showing posts with label Array Example. Show all posts
Showing posts with label Array Example. Show all posts

Tuesday, 27 January 2015

Arrays using Command - Line Arguments

Introduction :

In many system it is possible to pass argument from the command line (known as command line argument) to an application by including a parameter of type string[] (i.e. an array of string) in the parameter list of Main. We named this parameter args. When an application is executed from the the command prompt, the execution environment the command-line argument that appear after the application name the application's main method as string in the one dimensional array args. The Number of argument passed from the command line is obtained from the accessing the arrays length property. For example the command  "MyApplication a b"  passes two command line argument to application MyApplication. Note that command line argument separated by white space not commas.

Example

  1. using System;  
  2. public class InitArray  
  3. {  
  4.     public static void Main(string[] args)  
  5.     {  
  6.         if(args.Length != 3)  
  7.             Console.WriteLine("Please re-enter the entire command including\n An array size, Initial Value and Increament");  
  8.   
  9.         else  
  10.         {  
  11.             int arrayLength = Convert.ToInt32( args[0] );  
  12.             int[] array = new int[ arrayLength ];  
  13.   
  14.             int initialValue = Convert.ToInt32( args[1] );  
  15.             int increament = Convert.ToInt32( args[2] );  
  16.   
  17.             forint counter = 0; counter<array.Length; counter++ )  
  18.                 array[counter] = initialValue + increament * counter;  
  19.   
  20.             Console.WriteLine( "{0}{1,8}""Index""Value" );  
  21.   
  22.             for(int counter = 0; counter < array.Length; counter++)  
  23.   
  24.             Console.WriteLine("{0,3}{1,8}", counter, array[counter]);  
  25.         }  
  26.     }  
  27. }  


In the above example it takes three command line arguments to initialize the array. First is the length of the array Second is the initial value of the array third is the Increment between values in the array.


Output of Command line argument
Fig: Screenshot of output


In this Output the size of array is 5 the initial value of array is 0 and there is increment of 8.



Variable-Length Argument Lists (VARARGS)

Introduction:

Variable - length argument lists allow you to create methods that receive an arbitrary number of arguments. A one-dimensional array-type argument preceded by the keyword params in a method's parameter list indicates that the method receives a variable number of argument with the type of array elements. This use of params modifiers can occurs only in the last entry of the parameter list. While you can use method overloading and arraypassing to accomplish much of what is accomplished with "varargs"- another name for variable length argument lists-using the param modifiers is more concise.

Example:

  1. using System;  
  2. public class VarargsTest  
  3. {  
  4.     public static double Average(params double[] numbers)  
  5.     {  
  6.         double total = 0.0;  
  7.         foreach(double d in numbers)  
  8.             total += d;  
  9.   
  10.         return total/numbers.Length;  
  11.     }  
  12.   
  13.     public static void Main()  
  14.     {  
  15.         double d1 = 10.0;  
  16.         double d2 = 12.5;  
  17.         double d3 = 15.6;  
  18.         double d4 = 14.8;  
  19.   
  20.         Console.WriteLine( "d1 = {0:F2}\nd2 = {1:F2}\nd3 = {2:F2}\nd4 = {3:F2}", d1, d2, d3, d4 );  
  21.   
  22.         Console.WriteLine("Average of d1 and d2 is {0:F2}", Average(d1, d2));  
  23.         Console.WriteLine("Average of d1, d2 and d3 is {0:F2}", Average(d1, d2, d3));  
  24.         Console.WriteLine("Average of d1, d2, d3 and d4 is {0:F2}", Average(d1, d2, d3, d4));  
  25.   
  26.         Console.ReadLine();  
  27.     }  
  28. }  


Output of varargs
Fig: Screenshot of Output




Monday, 26 January 2015

Array Basic Programming Questions

Question 1:  Program to print sum of array

Code Snippet:


  1. using System;  
  2. public class SumArray  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int[] array = {87,85,98,25,45,63,45,86,36,52};  
  7.         int total = 0;  
  8.   
  9.         for(int counter = 0; counter<array.Length; counter++)  
  10.         {  
  11.             total += array[counter];  
  12.         }  
  13.   
  14.         Console.WriteLine("Total of array = {0}", total);  
  15.     }     
  16. }


Output of SumOfArray
Fig: Screenshot of Output


Question 2: Program to print BarChart.

Code Snippet:
  1. using System;    
  2. public class BarChart    
  3. {    
  4.     public static void Main()    
  5.     {    
  6.         int[] array = {0,1,1,2,0,0,0,1,4,10,5};    
  7.     
  8.         Console.WriteLine("Grade Distribution");    
  9.     
  10.         for(int counter = 0; counter<array.Length; counter++)    
  11.         {    
  12.             if(counter == 10)    
  13.             {    
  14.                 Console.Write( "100  :" );    
  15.             }    
  16.             else    
  17.             {    
  18.                 Console.Write("{0:D2}-{1:D2}:", counter*10, counter*10+9);    
  19.             }    
  20.     
  21.             for(int stars = 0; stars<array[counter]; stars++)    
  22.                 Console.Write( "*" );    
  23.     
  24.             Console.WriteLine();    
  25.         }    
  26.     }    
  27. }

Output of BarChart
Fig: Screenshot of output



Question 3: Program to Count Frequency of a rolling die 6000 times.

Code Snippet:
  1. using System;  
  2. class RollDie  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         Random randomNumbers = new Random();  
  7.         int[] frequency = new int[7];  
  8.   
  9.         for(int roll = 1; roll<=6000; roll++)  
  10.         ++frequency[randomNumbers.Next(1,7)];  
  11.   
  12.         Console.WriteLine("{0}{1,15}","Face""Frequency");  
  13.   
  14.         for(int face=1; face<frequency.Length; face++)  
  15.         Console.WriteLine("{0,3}{1,13}", face, frequency[face]);  
  16.   
  17.         Console.ReadLine();  
  18.     }  
  19. }  


Output of  RollDie
Fig: Screenshot of output


Question 4: Forty Student were asked to rate the quality of the food in the cafeteria on a scale 1 to 10 (where 1 means awful and 10 means excellent). Place the 40 response response in the integer array and summarize the result of poll.

Code Snippet:

  1. using System;  
  2. class RollDie  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         Random randomNumbers = new Random();  
  7.         int[] frequency = new int[7];  
  8.   
  9.         for(int roll = 1; roll<=6000; roll++)  
  10.         ++frequency[randomNumbers.Next(1,7)];  
  11.   
  12.         Console.WriteLine("{0}{1,15}","Face""Frequency");  
  13.   
  14.         for(int face=1; face<frequency.Length; face++)  
  15.         Console.WriteLine("{0,3}{1,13}", face, frequency[face]);  
  16.   
  17.         Console.ReadLine();  
  18.     }  
  19. }  

Fig: Screenshot of output



Question 5: Program to calculate sum of array using for each loop.

Code Snippet:

  1. using System;  
  2. public class ForEach  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int[] array = {2,1,45,78,56,4,2,6,7,8,4,2};  
  7.   
  8.         int total = 0;  
  9.   
  10.         foreach(int i in array)  
  11.         total += i;  
  12.   
  13.         foreach(int number in array)  
  14.         Console.Write("{0} ", number);  
  15.   
  16.         Console.WriteLine();  
  17.   
  18.         Console.WriteLine("Sum of array= {0}", total);  
  19.   
  20.         Console.ReadLine();  
  21.     }  
  22. }  


Fig: Screenshot of output


Question 6: Program to initializing jagged array.

Code Snippet:

  1. using System;  
  2. public class InitArray  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int[,] rectangular = {{1,2,3},{4,5,6}};  
  7.   
  8.         int[][] jagged = {  new int[] {1,2},  
  9.                     new int[] {3},  
  10.                     new int[] {4,5,6}   };  
  11.   
  12.         OutputArray( rectangular );  
  13.         Console.WriteLine();  
  14.         OutputArray( jagged );  
  15.   
  16.         Console.ReadLine();  
  17.     }  
  18.   
  19.     public static void OutputArray(int[,] array)  
  20.     {  
  21.         Console.WriteLine("Value of the rectangular array by row are");  
  22.   
  23.         for(int row = 0; row<array.GetLength(0); row++)  
  24.         {  
  25.             for(int column = 0; column<array.GetLength(1); column++)  
  26.                 Console.Write("{0} ", array[row,column]);  
  27.                 Console.WriteLine();  
  28.         }  
  29.     }  
  30.   
  31.     public static void OutputArray(int[][] array)  
  32.     {  
  33.         Console.WriteLine("Value of jagged array by row are: ");  
  34.   
  35.         for(int row = 0; row < array.Length; row++)  
  36.         {  
  37.             for(int column = 0; column < array[row].Length; column++)  
  38.                 Console.Write("{0} ",array[row][column]);  
  39.   
  40.             Console.WriteLine();  
  41.         }  
  42.     }  
  43. }  


Output of jagged array
Fig: Screenshot of output