You are on page 1of 11

Mind and Integrity College, Inc.

San Cristobal, Calamba City


In collaboration with
Department of Education
Region IV – CALABARZON

JAVA 2
Quarter 3 – Module 2:
TWO – DIMENSIONAL ARRAYS AND POINTERS

SELF LEARNING MODULE


GRADE 11

Development Team:
Writer: Randy T. Mercado
Reviewer: Marife P. De Castro
Layout: Paulo Stephen Cadawas
Management: Dr. Edwin T. Casila, MCL – Principal
Christian D. Manalansan – President

Mind and Integrity College, Inc.


Selina-Liz Bldg. National Hi-way, San Cristobal, Calamba City, Laguna
Contact #: 049-531-1604 / 0908-965-0010
Email Address: mai.school@yahoo.com

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 1 of 11
Dear Parents,

Mind and Integrity College, Inc. is one with every Filipino family in coping with the
demands of our modern times amidst the threat of COVID-19 pandemic.

The school initiated the distribution of a Self-Learning Module (SLM) in order to


meet the essential learning competencies required to be learned by your child whether
your child opts for online, modular, or blended learning modality. The learning activities
in this SLM are arranged chronologically from simple to complex that will lead your child
to think critically, act skillfully, and reflect deeply on each lesson and to practice them
into real life settings. Most importantly, this SLM promotes self-paced learning as your
child can always review the least understood lessons as often as he/she pleases.

Thank you in advance for being one with us! Together, let us envision that, by the
end of this school year, we will see your child as one responsible young person with a
heart and mind for humanity, for nature, for the country, and for God.

Dear Learner,

Welcome to a brand-new year of learning!

This is our gift to you. The school initiated the distribution of Self-Learning
Modules (SLM) that will help you keep up with the lesson whether you opted for online,
modular, or blended learning as a modality.

Please take time to read and do the activities in these SLM as if you are reporting
in school. Set a regular study schedule for you as much as possible, but keep in mind
that these SLM will enable you to learn at your own pace. If you do not understand a
lesson, the SLM would not mind you flipping back the pages repeatedly for review. Also,
remember to keep in touch with your teachers. Send them a message through your
online sessions or write them a note as you do your modular activities.

We wish you good luck in your studies, and we hope that you will remain happy
and enthusiastic in learning!

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 2 of 11
What This Module is About
Fast and efficient processing of two-dimensional arrays is essential in land-change modeling
because of the substantial computational effort expended by some computer land change models in
processing raster-graphic images and other two-dimensional arrays. Elapsed, wall-clock runtimes
for spatially explicit land-change models often span days or weeks, so improvements in the speed
and efficiency of a land-change model’s executable computer code can reduce its runtime by hours
or even days. The speed of execution can make the difference between a model that is fast enough
to be used and one that is not. Computational speed and efficiency in array processing are
fundamental, threshold concerns in land-change modeling and other areas of scientific
computation.

NOTE: Prepare yellow pad papers where you would write all your outputs for this
module. Do not forget to label your works properly corresponding to the title of each
activity. Also, please label your work with module number and module title. Do not forget
to write your name, section and the date of first entry.

Make sure to clip/staple your works so that they will not easily be separated. It is advised
to take down notes about the important information from each lesson because of the
modules will be returned at the end of every week. Please do not write anything on
module.

The following are the lessons contained in this module:


1. Multi – Dimensional Array
2. Pointers

What I Need to Know

At the end of this module, you should be able to:


1. Know and apply the use of multi – dimensional array.

How to Learn from this Module


To achieve the objectives cited above, you are to do the following:
 Take your time reading the lessons carefully.
 Follow the directions and/or instructions in the activities and exercises diligently.
 Answer all the given tests and exercises.
Icons of this Module
What I Need to This part contains learning objectives that are
Know set for you to learn as you go along the
module.

What I know This is an assessment as to your level of


knowledge to the subject matter at hand,
meant specifically to gauge prior relatedt
Knowledge

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 3 of 11
What’s In This part connects previous lesson with that of
the current one.

What’s New An introduction of the new lesson through


various activities, before it will be presented to
you

What is It These are discussions of the activities as a


way to deepen your discovery and under-
standing of the concept.

What’s More These are follow-up activities that are in-


tended for you to practice further in order to
master the competencies.

What I Have Activities designed to process what you


Learned have learned from the lesson

What I can do These are tasks that are designed to show-


case your skills and knowledge gained, and
applied into real-life concerns and situations.

Assessment This is a task which aims to evaluate your level


of mastery in achieving the learning competency

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 4 of 11
Lesson 1: TWO – DIMENSIONAL ARRAY

What I Need to Know

INTRODUCTION
Thus far, you have used one-dimensional arrays to model linear collections of elements. You
can use a two-dimensional array to represent a matrix or a table. For example, the following table
that describes the distances between the cities can be represented using a two - dimensional array.

What Is It
TWO – DIMENSIONAL ARRAY
 You can use a two-dimensional array to represent a matrix or a table.
 Occasionally, you will need to represent n-dimensional data structures. In Java, you can
create n-dimensional arrays for any integer n.

The simplest form of the multidimensional array is the two-dimensional array. A two-dimensional
array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of
size x,y, you would write something as follows:

Where type can be any valid C++ data type and arrayName will be a valid C++ identifier. A two-
dimensional array can be think as a table, which will have x number of rows and y number of
columns. A 2-dimensional array a, which contains three rows and four columns can be shown as
below:

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 5 of 11
Thus, every element in array a is identified by an element name of the form a[ i ][ j ], where a is the
name of the array, and i and j are the subscripts that uniquely identify each element in a.

INITIALIZING TWO-DIMENSIONAL ARRAYS:

Multi - dimensioned arrays may be initialized by specifying bracketed values for each row.
Following is an array with 3 rows and each row have 4 columns.

The nested braces, which indicate the intended row, are optional. The following initialization is
equivalent to previous example:

ACCESSING TWO-DIMENSIONAL ARRAY ELEMENTS:

An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and column
index of the array. For example:

The above statement will take 4th element from the 3rd row of the array. You can verify it in the
above diagram.

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 6 of 11
When the above code is compiled and executed, it produces the following result:

As explained above, you can have arrays with any number of dimensions, although it is likely that
most of the arrays you create will be of one or two dimensions.

What’s New

Activity 1: ARRAY OUTPUT


Instruction: What is the output of the given source code below? Write your answer on your yellow
pad paper.

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 7 of 11
Lesson 2: POINTERS
What I Need to Know

INTRODUCTION
What Is Pointer?
Variable in a program is something with a name, the value of which can vary. The way the compiler
and linker handles this is that it assigns a specific block of memory within the computer to hold the
value of that variable.

DEREFERENCING
Dereferencing means taking away the reference and giving you what it was actually referring
to.
Dereferencing is used to access or manipulate data contained in memory location pointed to by a
pointer. *(asterisk) is used with pointer variable when dereferencing the pointer variable, it refers
to variable being pointed, so this is called dereferencing of pointers.
 int bar = *foo_ptr;
 *foo_ptr = 42; // set foo to 42 which is also effect bar = 42

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 8 of 11
Difference Between & and *
& is the reference operator and can be read as "address of“
* is the dereference operator and can be read as "value pointed by"

A variable referenced with&can be dereferenced with *


• andy = 25;
• ted = &andy; All expressions below are true:
• andy == 25 // true
• &andy == 1776 // true
• ted == 1776 // true
• *ted == 25 // true

How to declare pointer?


• Type + “*” + name of variable.
• Example: int * number;
• char * c;
• number or c is a variable is called a pointer variable

How to use a pointer?


 int foo;
 int *foo_ptr = &foo;
 foo_ptr is declared as a pointer to int. We have initialized it to point to foo.
 foo occupies some memory. Its location in memory is called its address. &foo is the address
of foo

USING POINTERS IN C++


There are few important operations, which we will do with the pointers very frequently.
a) we define a pointer variables
b) assign the address of a variable to a pointer and
c) finally access the value at the address available in the pointer variable. This is done by using
unary operator * that returns the value of the variable located at the address specified by its
operand. Following example makes use of these operations:

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 9 of 11
When the above code is compiled and executed, it produces result something as follows:

C++ Pointers in Detail:


Pointers have many but easy concepts and they are very important to C++ programming. There are
following few important pointer concepts which should be clear to a C++ programmer:
CONCEPT DESCRIPTION
C++ Null Pointers C++ supports null pointer, which is a constant with a value of zero
defined in several standard libraries.
C++ Pointer Arithmetic There are four arithmetic operators that can be used on pointers: ++,
--, +, -
C++ Pointers vs. Arrays There is a close relationship between pointers and arrays. Let us
check how?
C++ Arrays of Pointers You can define arrays to hold a number of pointers.
C++ Pointer to Pointer C++ allows you to have pointer on a pointer and so on.
Passing pointers to Passing an argument by reference or by address both enable the
functions passed argument to be changed in the calling function by the called
function.
Return Pointer from C++ allows a function to return a pointer to local variable, static
functions variable and dynamically allocated memory as well.

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 10 of 11
What’s New

ACTIVITY 2: What is the output of the given source code below? Write your answer on your yellow
pad paper.

References
“Chapter 7: Multi – dimensional Array”
https://www2.southeastern.edu/Academics/Faculty/kyang/2010/Fall/CMPS161/ClassNotes/CM
PS161ClassNotesChap07.pdf

“Pointer (in C/C++)”


https://web.iit.edu/sites/web/files/departments/academic-affairs/academic-resource-
center/pdfs/pointer_cs.pdf

“C++ Pointers”
https://www.tutorialspoint.com/cplusplus/pdf/cpp_pointers.pdf

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 11 of 11

You might also like