0% found this document useful (0 votes)
40 views

Module 3 IT 104 Arrays and Collections - 051541

The document discusses arrays in C# programming. It defines arrays as a collection of elements of the same type that stores data in a fixed sequence. The document explains how to declare, initialize, access, and iterate through arrays using for loops and the foreach construct. It provides examples of C# programs that demonstrate declaring, populating, and iterating over one-dimensional and two-dimensional arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Module 3 IT 104 Arrays and Collections - 051541

The document discusses arrays in C# programming. It defines arrays as a collection of elements of the same type that stores data in a fixed sequence. The document explains how to declare, initialize, access, and iterate through arrays using for loops and the foreach construct. It provides examples of C# programs that demonstrate declaring, populating, and iterating over one-dimensional and two-dimensional arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

1T 104

MODULE 3
COMPUTER PROGRAMMING 2

Arrays and Collections

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

Intended Learning Outcomes:

• Describe the fundamentals of array.


• Analyze properly the use of array in C# application program.
• Demonstrate a program that requires an appropriate application of the
different array structures

Enabled Learning Outcomes

1. Apply and use array in a program.


2. Create, declare and initialize an array.
3. Populate an array with a set of data items.
4. Access the data items held in an array.
5. Iterate through the data items in an array.

SLSU CCSIT MAIN CAMPUS 1


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

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.

An array’s elements in C# are numbered with 0, 1, 2, … N-1. Those numbers are


called indices. The total number of elements in a given array we call length of an array.

Arrays can be in different dimensions, but the most used are the one-dimensional and
the two-dimensional arrays.

• One-dimensional arrays are also called vectors.


• Two-dimensional arrays are also known as matrices.

Declaration and Allocation of Memory for 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.

SLSU CCSIT MAIN CAMPUS 2


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

Declaring an Array

Example: int[] myArray;

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).

SLSU CCSIT MAIN CAMPUS 3


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

Creation of an Array – the Operator "new"

In C# we create an array with the help of the keyword new, which is used to
allocate memory.

The syntax for creating an array is:

• 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).

SLSU CCSIT MAIN CAMPUS 4


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

Array Initialization and Default Values

Before we can use an element of a given array, it has to be initialized or to have


a default value.

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.

Here is one more example how to declare and initialize an array:

In this case we allocate an array of seven (7) elements of type string.

On this figure we see how the array is allocated in the memory:

SLSU CCSIT MAIN CAMPUS 5


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

Boundaries of an Array

Arrays are by default zero-based, which means the enumeration of the


elements starts from 0. The first element has the index 0, the second is 1, etc. In an array
of N elements, the last element has the index N-1.

Access to the Elements of an Array

• We access the array elements directly using their indices.


• Each element can be accessed through the name of the array and the element’s
index placed in the brackets. Example: array_name[index]
• We can access given elements of the array both for reading and for writing.

Example 1:

Result Output:

Example 2:

Result Output:

SLSU CCSIT MAIN CAMPUS 6


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

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:

Reading an Array from the Console

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:

SLSU CCSIT MAIN CAMPUS 7


1T 104
MODULE 3
COMPUTER PROGRAMMING 2
Example 2: (C# Program that will ask to input a number for five (5) times)

Result Output:

Example 3: (C# Program that will ask to input a grade for three (3) times)

Result Output:

SLSU CCSIT MAIN CAMPUS 8


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

Iteration through Elements of an Array

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.

Iteration with a For Loop

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:

SLSU CCSIT MAIN CAMPUS 9


1T 104
MODULE 3
COMPUTER PROGRAMMING 2
Example 2: (C# Program to display list of students stored from different arrays)

Result Output:

Example 3: (C# Program to ask name of countries to be stored in the array and
display these elements afterwards)

SLSU CCSIT MAIN CAMPUS 10


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

Result Output:

Example 4: (C# Program to ask employee’s information to be stored in the array and
display these elements afterwards)

SLSU CCSIT MAIN CAMPUS 11


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

Result Outputs:

SLSU CCSIT MAIN CAMPUS 12


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

Iteration with "foreach" Loop

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.

The loop variable in foreach-loops is read-only so we cannot modify the current


loop item from the loop body.
The foreach-loop statement is used, when we do not need to change the elements,
but just to read them.

In the next example we will learn how to use the foreach loop to iterate through the
array:

Result Output:

SLSU CCSIT MAIN CAMPUS 13


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

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).

This example shows that:

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.

Multidimensional Array Declaration and Allocation

We declare multidimensional arrays in a way similar to one-dimensional arrays.


Each dimension except the first is marked with comma:

We are allocating memory for multidimensional arrays by using the keyword


new and for each dimension we set a length in the brackets as shown:

We can imagine the elements of these two-dimensional arrays as two-dimensional


matrices, which have rows and columns for the dimensions:

SLSU CCSIT MAIN CAMPUS 14


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

Two-Dimensional Array Initialization

We initialize two-dimensional arrays in the same way as we initialize one-


dimensional arrays. We can list the element values straight after the declaration:

If we will imagine these elements in a two-dimensional matrix, this will be the


content of the rows and column:

Accessing the Elements of a Multidimensional Array

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:

SLSU CCSIT MAIN CAMPUS 15


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

Result Output:

Length of Multidimensional Arrays

Each dimension of a multidimensional array has its own length, which can be
accessed during the execution of the program.

You may use the following to get the length:


• array_name.GetLength(0) -to get the number of rows of the two-dimensional array.
• array_name.GetLength(1) -to get the number of all columns per row.

Let see the example below using the profile array from the previous example:

Result Output:

SLSU CCSIT MAIN CAMPUS 16


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

Reading Matrices from the Console

Example 1: (C# Program to ask id number, name, course, year level and section of
students)

SLSU CCSIT MAIN CAMPUS 17


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

Result Outputs:

SLSU CCSIT MAIN CAMPUS 18


1T 104
MODULE 3
COMPUTER PROGRAMMING 2
Example 2: (C# Program that will ask and compute student grades and stored it in
the array)

SLSU CCSIT MAIN CAMPUS 19


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

Result Outputs:

SLSU CCSIT MAIN CAMPUS 20


1T 104
MODULE 3
COMPUTER PROGRAMMING 2

REFERENCES

1. Stephens,R. (2014). C# 5.0 Programmer’s Reference. John Wiley & Sons,


Inc.
2. Nakov, S., & Kolev, V. (2013). Fundamentals of Computer Programming
with C#: The Bulgarian C# Book. Faber Publishing.
3. Sharp, J. (2015). Microsoft Visual C# Step by Step, 8th Edition. Microsoft
Press
4. Learn C# Programming [internet]: c 2022.
Available from: https://www.tutorialspoint.com/csharp/index.htm
5. C# Tutorial [internet]:C1999-2023.
Available from: https://www.w3schools.com/cs/index.php

SLSU CCSIT MAIN CAMPUS 21

You might also like