Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

E166457 DiTEC195-B

Self-studies 10.2
E166457 DiTEC195-B

1) What is an Array? What the benefits of using them? In what type of situation would you use
them?
What is an array?

In C#, an array is a structure that represents an ordered collection of fixed-length values or objects of the
same type. Arrays make it easy to organize and manipulate large amounts of data. For example, instead
of creating 100 integer variables, you can create an array to store all those integer values.

C# array variables are declared like non-array variables, with square brackets ([]) added after the type
specifier to indicate that they are arrays.

The new keyword is required when instantiating a new array to assign to a variable, along with the
length of the array in square brackets. Arrays can also be instantiated by value using curly braces ({}). In
this case you don't need the length of the array.

In C#, you can simultaneously declare and initialize an array by assigning the newly declared array to a
comma-separated list of values enclosed in braces ({}). Note that this syntax allows you to omit the type
signature and new keyword on the right side of the assignment. This is only possible while declaring the
array.

The benefits of using arrays

1. It is used to store similar types of multiple data items using a single name.
2. We can use arrays to implement other data structures such as linked lists, trees, graphs, stacks,
queues, etc.
3. The two-dimensional arrays in C# are used to represent matrices.
4. The Arrays in C# are strongly typed. That means they are used to store similar types of multiple
data items using a single name.
E166457 DiTEC195-B

2) Explain the differences between a single-dimensional array and a multi-dimensional array. Write
3 code examples to illustrate the differences.

1D array or single dimensional array stores a list of variables of the same data type. It is possible to
access each variable using the index.

This array is capable of storing 10 integer values.

We can combine the above two statements together and write as follows.

int numbers = new int[10];

Below is an example of assigning values to the array.

numbers ={1,2,3,4,5,6,7,8,9,10};

The starting index of an array is 0. Therefore, the element in the 0th index is 1. The element in the 1st
index is 2. The element in the 2nd index is 3, etc. The index of the final element is 9.

If the programmer wants to store number 50 on the 2nd index, he can write the statement as follows.

numbers[2] = 50;

2D array or multi-dimensional array stores data in a format consisting of rows and columns.

For example, int[][] numbers; declares a 2D arrays.

numbers = new int [2][3];

The above statement allocates memory for a 2D array of 2 rows and 3 columns.

We can combine the above two statements together and write the statement as follows.

int[][] numbers = new int[2][3];

Below is an example of assigning values to the 2D array.

int[][] numbers = { {10,20,30}, {50,60,70}};

Similar to a 1D array, the starting index of the 2D array is also 0. This array has two rows and three
columns. The indexes of the rows are 0 and 1 while the indexes of columns are 0, 1 and 2. The element
10 is in the 0th row 0th column position. Number 20 is in the 0th row, 1st column position. Number 70 is
in 1st row, 2nd column position.

numbers[1][2] = 50;

Above statement assigns the number 50 to the 1st row, 2nd column position.
E166457 DiTEC195-B

Basis One Dimension Array Two Dimension Array

Store a single list of the


Store a ‘list of lists’ of the element of a
Definition element of a similar data
similar data type.
type.

Representatio Represent multiple data Represent multiple data items as a


n items as a list. table consisting of rows and columns.

The declaration varies for


different programming The declaration varies for different
language: programming language:

1. For C++, 1. For C++,


datatype datatype
Declaration variable_name[ro variable_name[row][colum
w] n]
2. For Java, 2. For Java,
datatype [] datatype [][]
variable_name= variable_name= new
new datatype[row][column]
datatype[row]

Dimension One Two

size of(datatype of the size of(datatype of the variable of the


Size(bytes) variable of the array) * size of array)* the number of rows* the
the array number of columns.

Address of a[i][j] can be calculated in


two ways row-major and column-
major
Address of a[index] is equal
Address to (base Address+ Size of 1. Column Major: Base
calculation. each element of array * Address + Size of each
index). element (number of rows(j-
lower bound of the
column)+(i-lower bound of
the rows))
E166457 DiTEC195-B

2. Row Major: Base Address


+ Size of each element
(number of columns(i-lower
bound of the row)+(j-lower
bound of the column))

int arr[5]; //an array with one int arr[2][5]; //an array with two rows
row and five columns will be and five columns will be created.
Example created. a b c d e
{a , b , c , d , e} f g h i j

3) Study different types of properties and methods available in C#.Net when working with arrays.
Give code samples for each one that you explain and show the output.

C# Array Properties

Property Description

IsFixedSize It is used to get a value indicating whether the Array has a fixed size or not.

IsReadOnly It is used to check that the Array is read-only or not.

IsSynchronized It is used to check that access to the Array is synchronized or not.

Length It is used to get the total number of elements in all the dimensions of the Array.

LongLength It is used to get a 64-bit integer that represents the total number of elements in all the dimensions
of the Array.

Rank It is used to get the rank (number of dimensions) of the Array.

SyncRoot It is used to get an object that can be used to synchronize access to the Array.
E166457 DiTEC195-B

C# Array Methods

Method Description

AsReadOnly<T>(T[]) It returns a read-only wrapper for the specified array.

BinarySearch(Array,Int32,Int32,Object) It is used to search a range of elements in a one-dimensional sorted array for


a value.

BinarySearch(Array,Object) It is used to search an entire one-dimensional sorted array for a specific


element.

Clear(Array,Int32,Int32) It is used to set a range of elements in an array to the default value.

Clone() It is used to create a shallow copy of the Array.

Copy(Array,Array,Int32) It is used to copy elements of an array into another array by specifying starting
index.

CopyTo(Array,Int32) It copies all the elements of the current one-dimensional array to the specified
one-dimensional array starting at the specified destination array index

CreateInstance(Type,Int32) It is used to create a one-dimensional Array of the specified Type and length.

Empty<T>() It is used to return an empty array.

Finalize() It is used to free resources and perform cleanup operations.

Find<T>(T[],Predicate<T>) It is used to search for an element that matches the conditions defined by the
specified predicate.

IndexOf(Array,Object) It is used to search for the specified object and returns the index of its first
occurrence in a one-dimensional array.

Initialize() It is used to initialize every element of the value-type Array by calling the
default constructor of the value type.

Reverse(Array) It is used to reverse the sequence of the elements in the entire one-dimensional
Array.

Sort(Array) It is used to sort the elements in an entire one-dimensional Array.

ToString() It is used to return a string that represents the current object.


E166457 DiTEC195-B

C# Array Example

using System;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
// Creating an array
int[] arr = new int[6] { 5, 8, 9, 25, 0, 7 };
// Creating an empty array
int[] arr2 = new int[6];
// Displaying length of array
Console.WriteLine("length of first array: "+arr.Length);
// Sorting array
Array.Sort(arr);
Console.Write("First array elements: ");
// Displaying sorted array
PrintArray(arr);
// Finding index of an array element
Console.WriteLine("\nIndex position of 25 is "+Array.IndexOf(arr,25));
// Coping first array to empty array
Array.Copy(arr, arr2, arr.Length);
Console.Write("Second array elements: ");
// Displaying second array
PrintArray(arr2);
Array.Reverse(arr);
Console.Write("\nFirst Array elements in reverse order: ");
PrintArray(arr);
}
// User defined method for iterating array elements
static void PrintArray(int[] arr)
{
E166457 DiTEC195-B

foreach (Object elem in arr)


{
Console.Write(elem+" ");
}
}
}
}

Output :
E166457 DiTEC195-B

4) Develop code that outputs the following Star Patterns using C#.Net Console applications.
A)
E166457 DiTEC195-B

B)
E166457 DiTEC195-B

C)
E166457 DiTEC195-B

D)
E166457 DiTEC195-B

You might also like