Module 3 IT 104 Arrays and Collections - 051541
Module 3 IT 104 Arrays and Collections - 051541
MODULE 3
COMPUTER PROGRAMMING 2
MODULE GUIDE
In this module, we will learn about arrays as a way to work with sequences of
elements of the same type. We will explain what arrays are, and how we declare, create,
instantiate, and use them. We will examine one-dimensional and multidimensional
arrays. We will learn different ways to iterate through the array, read from the standard
input and write to the standard output.
In this module, C# Array will be given emphasis. Please be guided that learning
this module will be an advantage on your part specifically, in understanding the basic
context of programming specifically Array. Constant practice using visual studio and
applications will surely improve your programming skills.
LEARNING PLAN
Arrays
An array stores a fixed-size sequential collection of elements of the same type. 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, which we call elements.
Arrays can be in different dimensions, but the most used are the one-dimensional and
the two-dimensional arrays.
In C# the arrays have fixed length, which is set at the time of their instantiation
and determines the total number of elements. Once the length of an array is set we cannot
change it anymore.
Declaring an Array
In this example:
• array variable myArray is the name of the array, which is of integer type (int[ ]).
When we declare an array type variable, it is a reference, which does not have
a value (it points to null). This is because the memory for the elements is not allocated
yet.
The figure below shows how a declared array variable looks, when the memory
for elements of the array is not allocated yet:
In the program’s execution stack the variable with the name myArray is created
and its value is set to null (meaning it holds no value).
In C# we create an array with the help of the keyword new, which is used to
allocate memory.
• The new operator would create an array of type with length slots.
• The type of the elements is written after the reserved word new, so we indicate
what type of elements are going to be allocated in the memory.
• Set the total number of the elements in the brackets, defining its length.
Example:
In this example:
• we allocate an array with length of 6 elements of type int.
• this means that in the dynamic memory (heap) an area of 6 integer numbers is
allocated and they all are initialized with the value 0
The figure shows, that after the allocation of memory for the array the variable
myArray points to an address in the dynamic memory, where the values are. In C#, the
elements of an array are always stored in the dynamic memory (called also heap).
In C# all variables, including the elements of arrays have a default initial value.
This value is either 0 for the numeral types or its equivalent for the non-primitive types
(for example null for a reference type and false for the bool type).
Indeed, we can set initial values explicitly. We can do this in different ways.
Here are some examples:
With this syntax we use curly brackets instead of the operator new. Between the
brackets we list the initial values of the array, separated by commas. Their count
defines the length of the array.
Boundaries of an Array
Example 1:
Result Output:
Example 2:
Result Output:
ARRAY'S LENGTH
In C#, each array has a length property. Using the property Length returns the
total count of the elements of the array.
Example:
Result Output:
Initially we read a line from the console using Console.ReadLine( ). Let us see
how we can read values of an array from the console in different ways through the
following examples given:
Example 1:
Result Output:
Result Output:
Example 3: (C# Program that will ask to input a grade for three (3) times)
Result Output:
As we can see, the iteration through the elements of an array is one of the most
used techniques when we work with arrays. Consecutive iterating using a loop will
allow us to access each element through its index and we will be able to modify it as we
want.
We can do that with different loop constructs, but the most appropriate loop is the
for-statement. We will examine in details how this type of iteration works.
It is a good practice to use for-loops, when we work with arrays and structures
with indices. Using a for-loop we keep track of the current index of the array and we
access the elements as needed.
Let us examine the following examples using for loop to manipulate array in a program.
Example 1: (C# Program to display different motor brands stored in the array)
Result Output:
Result Output:
Example 3: (C# Program to ask name of countries to be stored in the array and
display these elements afterwards)
Result Output:
Example 4: (C# Program to ask employee’s information to be stored in the array and
display these elements afterwards)
Result Outputs:
One of the most used constructs for iterating through elements of an array is
foreach. The foreach-loop construct in C# is as follows:
Keypoints:
• var is the type of the elements, which we iterate through.
• collection is the array (or any other collection of elements)
• item is the current element of the array on each step.
In the next example we will learn how to use the foreach loop to iterate through the
array:
Result Output:
Multidimensional Arrays
• The one-dimensional arrays are known also as vectors in mathematics.
• We declare a one-dimensional array of integer numbers using int[ ], and we
declare a two-dimensional with int[ , ].
• Those arrays we will call two-dimensional, because they have two dimensions.
They are also known as matrices (it is mathematical term).
In general arrays with more than one dimension we will call multidimensional.
In theory there is no limit for an array dimensions, but in practice we do not use much
arrays with more than two dimensions therefore we will focus on two-dimensional
arrays.
Matrices have two dimensions and respectively we access each element by using
two indices: one for the rows and one for the columns. Multidimensional arrays have
different indices for each dimension.
Example:
Result Output:
Each dimension of a multidimensional array has its own length, which can be
accessed during the execution of the program.
Let see the example below using the profile array from the previous example:
Result Output:
Example 1: (C# Program to ask id number, name, course, year level and section of
students)
Result Outputs:
Result Outputs:
REFERENCES