Question 1: Program to print sum of array
Code Snippet:
Question 2: Program to print BarChart.
Code Snippet:
Code Snippet:
- using System;
- public class SumArray
- {
- public static void Main()
- {
- int[] array = {87,85,98,25,45,63,45,86,36,52};
- int total = 0;
- for(int counter = 0; counter<array.Length; counter++)
- {
- total += array[counter];
- }
- Console.WriteLine("Total of array = {0}", total);
- }
- }
Fig: Screenshot of Output |
Question 2: Program to print BarChart.
Code Snippet:
- using System;
- public class BarChart
- {
- public static void Main()
- {
- int[] array = {0,1,1,2,0,0,0,1,4,10,5};
- Console.WriteLine("Grade Distribution");
- for(int counter = 0; counter<array.Length; counter++)
- {
- if(counter == 10)
- {
- Console.Write( "100 :" );
- }
- else
- {
- Console.Write("{0:D2}-{1:D2}:", counter*10, counter*10+9);
- }
- for(int stars = 0; stars<array[counter]; stars++)
- Console.Write( "*" );
- Console.WriteLine();
- }
- }
- }
Fig: Screenshot of output |
Question 3: Program to Count Frequency of a rolling die 6000 times.
Code Snippet:
- using System;
- class RollDie
- {
- public static void Main()
- {
- Random randomNumbers = new Random();
- int[] frequency = new int[7];
- for(int roll = 1; roll<=6000; roll++)
- ++frequency[randomNumbers.Next(1,7)];
- Console.WriteLine("{0}{1,15}","Face", "Frequency");
- for(int face=1; face<frequency.Length; face++)
- Console.WriteLine("{0,3}{1,13}", face, frequency[face]);
- Console.ReadLine();
- }
- }
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:
- using System;
- class RollDie
- {
- public static void Main()
- {
- Random randomNumbers = new Random();
- int[] frequency = new int[7];
- for(int roll = 1; roll<=6000; roll++)
- ++frequency[randomNumbers.Next(1,7)];
- Console.WriteLine("{0}{1,15}","Face", "Frequency");
- for(int face=1; face<frequency.Length; face++)
- Console.WriteLine("{0,3}{1,13}", face, frequency[face]);
- Console.ReadLine();
- }
- }
Fig: Screenshot of output |
Question 5: Program to calculate sum of array using for each loop.
Code Snippet:
- using System;
- public class ForEach
- {
- public static void Main()
- {
- int[] array = {2,1,45,78,56,4,2,6,7,8,4,2};
- int total = 0;
- foreach(int i in array)
- total += i;
- foreach(int number in array)
- Console.Write("{0} ", number);
- Console.WriteLine();
- Console.WriteLine("Sum of array= {0}", total);
- Console.ReadLine();
- }
- }
Fig: Screenshot of output |
Question 6: Program to initializing jagged array.
Code Snippet:
- using System;
- public class InitArray
- {
- public static void Main()
- {
- int[,] rectangular = {{1,2,3},{4,5,6}};
- int[][] jagged = { new int[] {1,2},
- new int[] {3},
- new int[] {4,5,6} };
- OutputArray( rectangular );
- Console.WriteLine();
- OutputArray( jagged );
- Console.ReadLine();
- }
- public static void OutputArray(int[,] array)
- {
- Console.WriteLine("Value of the rectangular array by row are");
- for(int row = 0; row<array.GetLength(0); row++)
- {
- for(int column = 0; column<array.GetLength(1); column++)
- Console.Write("{0} ", array[row,column]);
- Console.WriteLine();
- }
- }
- public static void OutputArray(int[][] array)
- {
- Console.WriteLine("Value of jagged array by row are: ");
- for(int row = 0; row < array.Length; row++)
- {
- for(int column = 0; column < array[row].Length; column++)
- Console.Write("{0} ",array[row][column]);
- Console.WriteLine();
- }
- }
- }
Fig: Screenshot of output |