In: Computer Science
C# Answer in discussion format
1. Why is an array such a useful tool?
An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Index associated with variable and they are being used to represent indiviual elements available in the array. All arrays consist of contigous memory locations.
2. What is a jagged array? Ask your students to describe some circumstances in which jagged arrays might be useful.
Jagged array is a array of arrays such that member arrays can be different size. In other words, the length each array index can differ. The elements of jagged array are refrence types and initialized to null by default. Jagged array can also be mixed with multidimensional arrays. Here, the number of rows will be fixed at the declaration time, but you can vary the number of columns.
In Jagged arrays, user has to provide the number of rows only. If the user is also going to provide the number of columns, then this array will be no more Jagged Array.
Syntax:
data_type[ ] [ ] name_of_array = new data_type[rows] [ ]
Jagged arrays are a special type of arrays that can be used to store rows of data of varying lengths to improve performance when working with multi-dimensional arrays.
3. the System.Array class contains a variety of useful, built-in methods, describe one of them and how you would use it.
System. Array is the base class for all arrays in the common language runtime. It has methods for creating, manipulating, searching, and sorting the arrays we have talked about so far. System. Array implements several interfaces, like ICloneable, IList, ICollection, and IEnumerable.
There are few built in methods of System.Array
IndexOf(Array, Object)
Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional Array.
Sort(Array)
Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array.
ToString
Returns a string that represents the current object. (Inherited from Object.)
4. Describe how to resize an array in C# using the new operator.
Example of C# program showing how to resize the array.
using System; class Program
{
static void Main()
{
// Step 1: initialize array for example.
char[] array = new char[4];
array[0] = 'p';
array[1] = 'e';
array[2] = 'r';
array[3] = 'l';
for (int i = 0; i < array.Length; i++)
{ Console.Write(array[i]); }
Console.WriteLine();
// Step 2: resize the array from 4 to 2 elements. Array.Resize(ref array, 2);
for (int i = 0; i < array.Length; i++)
{ Console.Write(array[i]); }
Console.WriteLine(); } }
Output perl pe
5. How would you declare an array if you do not know in advance how many elements you will have in each execution of a program?
In c#, Arrays are useful to store multiple elements of the same data type at contiguous memory locations and arrays will allow us to store a fixed number of elements sequentially based on the predefined number of items. An array can start storing the values from index 0. Suppose if we have an array with n elements, then it will start storing the elements from index 0 to n-1.