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