You are on page 1of 14

PRE-REQUISITE TOPICS OF DIFFERENT SUBJECTS AS REQUIRED IN MASTERY PATH

Subject Data Structure and Algorithm


Subject Code ITEC 104
Prerequisite Intermediate Programming
Subject

TOPIC PRE-REQUISITE TOPICS REQUIRING MASTERY


Data Types and Variables
Control Structures (Loop)
Topic 1: Arrays
Basic Computations and Operations
Pre-defined functions and methods
CURRICULUM MAP
1st Semester, Academic Year 2020-2021

Subject Area Subject Description

Data Structure The course covers the standard data representation and algorithms to solve computing
and Algorithm problems efficiently (with respect to space requirements and time complexity of
algorithm). This covers the following: Array, Stacks, Queues, Trees, Graphs, Maps,
Time Frame
and Sets. Thorough discussion of sorting and searching algorithms and hashing is
90 hrs covered.
Unit Number Unit Title and Description Unit Duration

Three Array 2 weeks (10 hours)


This is a unit that focused on the definition and elements of
array, how to access and initializing array elements including its
operations and dimensions.
STAGE ONE
DESIRED RESULTS
Quarter Transfer

First The students will be able to independently use their learning to…

Produce a program that will apply arrays in a real life situations

Topic Outline Meaning

● Definition and Understanding Essential Questions


elements of an The students will understand that… The students will keep considering
array
● Accessing and Array is one of the fundamental data 1. What are the uses of array?
initializing structure in programing languages. It 2. What are the differences between one dimensional and
array elements can be declared and initialize its multi-dimensional array?
● Array values and its elements can be 3. How can you access an element of an array considering
manipulated using methods, its location?
operations
properties and array operations. 4. How to initialize the elements of array?
● Array 5. How to apply different array operations?
dimensions 6. When to use array in programming?
7. Why knowledge in array is important in programming?

Acquisition

Knowledge Skills Attitude Academic


(Synthesis/Evaluation) Vocabulary
(Knowledge and (Application/Analysis)
Comprehension)

The students will know… The students will be The students will be Array
skilled at… able to…
Array index
1. the definition and 1. declaring and 1. Explain array in data
elements of an array initializing arrays structure and its Array elements
2. how to access and using appropriate different operations Array
initialize array data types. dimensions
elements 2. manipulating the 2. decide what
3. methods and elements of an array dimension of array to IsFixedSize
properties of an array using methods, use in a specific
properties and array problem IsReadOnly
operations. 3. understand the IsSynchronized
3. developing importance of array in
programs involving programming Length
arrays using C# LongLength
programming
language.

MODULE 3

ARRAYS

Learning Targets

After finishing this module, you are expected to:


● define and understand the elements of an array
● learn how to access and initialize array elements
● be familiarized with the methods and properties of an array
● declare and initialize arrays using appropriate data types.
● manipulate the elements of an array using methods, properties and array operations.
● develop programs involving arrays using C# programming language.
● decide what dimension of array to use in a specific problem
● understand the importance of array in programming

Warm Up

Give three things that you expect you will learn in this module.
1. ________________________________________________________________
2. ________________________________________________________________
3. ________________________________________________________________

Answer the following:


1. Is it possible to store multiple values using a single variable?
2. How do you manipulate an array?
3. What are the advantages of applying array in a program?

Overview

An array is a group of similar typed variables that are referred to by a common name. Arrays of any
type can be created and may have one or more dimensions. A specific element in an array is accessed by
its index. The array is a simple type of data structure which can store primitive variable or objects.

Lesson Proper
Array Definition

An array is an indexed sequence of components. Typically, the array occupies sequential storage
locations. The length of the array is determined when the array is created, and cannot be changed. Each
component of the array has a fixed, unique index.
Arrays works as collections of items. You can use them to gather items in a single group, and
perform various operations on them, e.g. sorting. Besides that, several methods within the framework work
on arrays, to make it possible to accept a range of items instead of just one.
Array Index
Array indices are integers. The bracket notation a[ i ] is used (and not overloaded). Bracket
operator performs bounds checking. An array of length n has bounds 0 and n-1. Any component of the
array can be inspected or updated by using its index

An array is a group of similar typed variables that are referred to by a common name. Arrays of any type
can be created and may have one or more dimensions. A specific element in an array is accessed by its
index. The array is a simple type of data structure which can store primitive variable or objects.
For example, imagine if you had to store the result of six subjects we can do it using an array. To create an
array value in C#, you use the new keyword, just as you do to create an object.

Array Index

Array indices are integers. The bracket notation a[i] is used (and not overloaded). Bracket operator
performs bounds checking. An array of length n has bounds 0 and n-1. Any component of the array can be
inspected or updated by using its index

int[] a = new int[10];


int[] b = a;
int[] c = new int[3] {1,2,3};

Arrays are homogeneous. However, an array of an object type may contain objects of any subtype
of that object. For example, an array of Animal may contain objects of type Cat and objects of type Dog. An
array of Object may contain any type of object (but cannot contain primitives)

Declaring Array
Arrays are declared much like variables, with a set of [] brackets after the datatype, like this:

string[] names;

You need to instantiate the array to use it, which is done like this:

string[] names = new string[2];

The number (2) is the size of the array, that is, the amount of items we can put in it. Putting items into the
array is pretty simple as well:

names[0] = "John Doe";


Sample Program 1

using System;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
  {
static void Main(string[] args)
{
    string[] names = new string[2];

    names[0] = "John Doe";


    names[1] = "Jane Doe";

    foreach(string s in names)
Console.WriteLine(s);

    Console.ReadLine();
}
  }
}

We use the foreach loop, because it's the easiest, but of course we could have used one of the
other types of loop instead. The for loop is good with arrays as well, for instance if you need to count each
item, like this:

for(int i = 0; i < names.Length; i++)


    Console.WriteLine("Item number " + i + ": " + names[i]);

It's actually very simple. We use the Length property of the array to decide how many times the
loop should iterate, and then we use the counter (i) to output where we are in the process, as well as get
the item from the array. Just like we used a number, a so called index, to put items into the array, we can
use it to get a specific item out again.

 The Array class contains a bunch of smart methods for working with arrays. This example will use
numbers instead of strings, just to try something else, but it could just as easily have been strings.

Take a look:

int[] numbers = new int[5] { 4, 3, 8, 0, 5 };

With one line, we have created an array with a size of 5, and filled it with 5 integers. By filling the array like
this, you get an extra advantage, since the compiler will check and make sure that you don't put too many
items into the array. Try adding a number more - you will see the compiler complain about it.

Actually, it can be done even shorter, like this:


int[] numbers = { 4, 3, 8, 0, 5 };

This is short, and you don't have to specify a size. The first approach may be easier to read later on though.
Let's try sorting the array - here's a complete example:

Sample Program 2

using System;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
  {
static void Main(string[] args)
{
    int[] numbers = { 4, 3, 8, 0, 5 };

    Array.Sort(numbers);

    foreach(int i in numbers)
Console.WriteLine(i);

    Console.ReadLine();
}
  }
}

The only real new thing here is the Array.Sort command. It can take various parameters, for
various kinds of sorting, but in this case, it simply takes our array. As you can see from the result, our array
has been sorted. The Array class has other methods as well, for instance the Reverse() method. You can
look it up in the documentation to see all the features of the Array class.
The arrays we have used so far have only had one dimension. However, C# arrays can be
multidimensional, sometimes referred to as arrays in arrays. Multidimensional arrays come in two
flavors with C#: Rectangular arrays and jagged arrays. The difference is that with rectangular arrays, all the
dimensions have to be the same size, hence the name rectangular. A jagged array can have dimensions of
various sizes. 

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.

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.

Sample Program 3
1. using System;  
2. namespace CSharpProgram  
3. {  
4.     class Program  
5.     {  
6.         static void Main(string[] args)  
7.         {  
8.             // Creating an array  
9.             int[] arr = new int[6] { 5, 8, 9, 25, 0, 7 };  
10.             // Creating an empty array  
11.             int[] arr2 = new int[6];  
12.             // Displaying length of array  
13.             Console.WriteLine("length of first array: "+arr.Length);  
14.             // Sorting array  
15.             Array.Sort(arr);  
16.             Console.Write("First array elements: ");  
17.             // Displaying sorted array  
18.             PrintArray(arr);  
19.             // Finding index of an array element  
20.             Console.WriteLine("\nIndex position of 25 is "+Array.IndexOf(arr,25));  
21.             // Coping first array to empty array  
22.             Array.Copy(arr, arr2, arr.Length);  
23.             Console.Write("Second array elements: ");  
24.             // Displaying second array  
25.             PrintArray(arr2);  
26.             Array.Reverse(arr);  
27.             Console.Write("\nFirst Array elements in reverse order: ");  
28.             PrintArray(arr);  
29.         }  
30.         // User defined method for iterating array elements  
31.         static void PrintArray(int[] arr)  
32.         {  
33.             foreach (Object elem in arr)  
34.             {  
35.                 Console.Write(elem+" ");  
36.             }  
37.         }  
38.     }  
39. }  
Output:
length of first array: 6
First array elements: 0 5 7 8 9 25
Index position of 25 is 5
Second array elements: 0 5 7 8 9 25
First Array elements in reverse order: 25 9 8 7 5 0

C# Multidimensional Arrays
The multidimensional array is also known as rectangular arrays in C#. It can be two dimensional or three
dimensional. The data is stored in tabular form (row * column) which is also known as matrix.

To create multidimensional array, we need to use comma inside the square brackets. For example:

int[,] arr=new int[3,3];//declaration of 2D array


int[,,] arr=new int[3,3,3];//declaration of 3D array
C# Multidimensional Array Example
Let's see a simple example of multidimensional array in C# which declares, initializes and traverse two
dimensional array.

using System;
public class MultiArrayExample
{
public static void Main(string[] args)
{
int[,] arr=new int[3,3];//declaration of 2D array
arr[0,1]=10;//initialization
arr[1,2]=20;
arr[2,0]=30;
//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();//new line at each row
}
}
}
Output:

0 10 0
0 0 20
30 0 0

C# Multidimensional Array Example: Declaration and initialization at same time


There are 3 ways to initialize multidimensional array in C# while declaration.

int[,] arr = new int[3,3]= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };


We can omit the array size.
int[,] arr = new int[,]{ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
We can omit the new operator also.

int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Let's see a simple example of multidimensional array which initializes array at the time of declaration.

using System;
public class MultiArrayExample
{
public static void Main(string[] args)
{
int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };//declaration and initialization

//traversal
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
Console.Write(arr[i,j]+" ");
}
Console.WriteLine();//new line at each row
}
}
}
Output:

123
456
789

C# Jagged Arrays
In C#, jagged array is also known as "array of arrays" because its elements are arrays. The element size of
jagged array can be different.

Declaration of Jagged array


Let's see an example to declare jagged array that has two elements.

int[][] arr = new int[2][];


Initialization of Jagged array
Let's see an example to initialize jagged array. The size of elements can be different.

arr[0] = new int[4];


arr[1] = new int[6];
Initialization and filling elements in Jagged array

Let's see an example to initialize and fill elements in jagged array.


arr[0] = new int[4] { 11, 21, 56, 78 };
arr[1] = new int[6] { 42, 61, 37, 41, 59, 63 };
Here, size of elements in jagged array is optional. So, you can write above code as given below:
arr[0] = new int[] { 11, 21, 56, 78 };
arr[1] = new int[] { 42, 61, 37, 41, 59, 63 };
C# Jagged Array Example
Let's see a simple example of jagged array in C# which declares, initializes and traverse jagged arrays.

public class JaggedArrayTest


{
public static void Main()
{
int[][] arr = new int[2][];// Declare the array

arr[0] = new int[] { 11, 21, 56, 78 };// Initialize the array
arr[1] = new int[] { 42, 61, 37, 41, 59, 63 };

// Traverse array elements


for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr[i].Length; j++)
{
System.Console.Write(arr[i][j]+" ");
}
System.Console.WriteLine();
}
}
}
Output:

11 21 56 78
42 61 37 41 59 63
Initialization of Jagged array upon Declaration
Let's see an example to initialize the jagged array while declaration.

int[][] arr = new int[3][]{


new int[] { 11, 21, 56, 78 },
new int[] { 2, 5, 6, 7, 98, 5 },
new int[] { 2, 5 }
};

Do It Yourself
Identification

Multiple Choice:

Assessment

PERFORMANCE TASK

Develop a program that prompts the user to input series of numbers that must be stored in an
array. Populate the array and present it using histogram.

TARGET SKILLS

Declaring an array and manipulating its elements using methods, properties and array operations;
logical thinking; programming skills

LEARNING ACTIVITIES

Problem definition, planning, coding, alpha testing, debugging, deployment

ASSESSMENT TASK

Design a flow chart to show the solution to the problem. Write the program. Create a
documentation including the problem given, program code, errors produced and solutions
implemented, and sample output.

References

ACTIVITY (OBJECTIVE - ASSESSMENT ALIGNMENT)

Revisit the objectives of your learning module and apply what you have learned in the lecture. Use the
following format in presenting your output.

Learning Objectives Assessment Specific type of Technology that will


assessments be used in the
(If the students are expected (Evaluate/Assess their (objective test, assessment (if any)
to…) ability to...) problem sets,
projects, etc.)
Example) Construct a decision Essay test Word/
chart that illustrates
Apply lab safety procedures correct procedures to PowerPoint
follow in the event of
an accident or
emergency
Activity (Managing and Evaluating Learning Activities)

Based on the course syllabus, create a sequence or storyboard that specifies the activities that will be carried out and
the materials that will be provided to students in each session using the format below.

 Program:  IT

Course Title: Web Systems and Technologies

Schedule of Activities

Week Topic Activity


Week 1 Course Overview 1. Read the course syllabus.

2. Familiarize yourself with the course site.

3. Introduce yourself in the Getting to know you forum.

Module 1 COMPUTER 4. Read Module 1 (PDF)


CONCEPTS
5. Submit the performance task thru the course site
 Information
Technology 6. Take Quiz No.1 in the course site
 Components of
Computer
 Software
 Hardware
 Computer Related
Issues
 Importance of
managing information
for businesses
 Use of Internet and
associated issues

Week 2
Week 3

Week 4
Week 5

CURRICULUM MAP
2nd Semester, Academic Year 2019-2020

Subject Area Subject Description

Time Frame

Unit Number Unit Title and Description Unit Duration


STAGE ONE
DESIRED RESULTS
Quarter Established Goals Transfer
Content Standard The students will be able to independently use their learning to…

Topic Outline Meaning


 Understanding Essential Questions
The students will The students will keep considering
understand that… 1.

Performance Acquisition
Standard
Knowledge Skills Attitude Academic
Vocabulary
The students will The students The students
know… will be skilled will be able to…
at…
1. 1. 1.

You might also like