0% found this document useful (0 votes)
195 views8 pages

Java 2: Quarter 3 - Module 1: DATA ARRAYS: Importance of Arrays

Uploaded by

Randy Mercado
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
195 views8 pages

Java 2: Quarter 3 - Module 1: DATA ARRAYS: Importance of Arrays

Uploaded by

Randy Mercado
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Mind and Integrity College, Inc.

San Cristobal, Calamba City


In collaboration with
Department of Education
Region IV – CALABARZON

JAVA 2
Quarter 3 – Module 1:
DATA ARRAYS: Importance of Arrays

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: [email protected]

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 1 of 8
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 8
What This Module is About
Computer programs often manage many objects of the same type, e.g., a bank’s accounting
program must manage hundreds of customer accounts. It is inconvenient and usually impossible to
declare distinctly named variables for each of the customer accounts; instead, one constructs a new
form of object—a data structure—to collectively hold and name the customer accounts. The most
popular form of data structure is the array, and this chapter introduces standard uses of arrays.

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. Data Arrays

What I Need to Know

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


1. Know and understand the importance of arrays.

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
What’s In This part connects previous lesson with that of
the current one.

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 3 of 8
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

Lesson 1: INTRODUCTION TO LIMITS

What I Need to Know

WHY WE NEED ARRAYS


When a program manipulates many variables that contain “similar” forms of data,
organizational problems quickly arise. Here is an example: In an ice-skaking competition, each
skater’s performance is judged by six judges, who assign fractional scores. The six scores must be
collected and manipulated in various ways, e.g., printed from highest to lowest, averaged,
multiplied by weighting factors, and so on.
Say that the six scores are saved in these variables,

and say that you must write a method that locates and prints the highest score of the six. How will
you do this? Alas, the method you write almost certainly will use a sequence of conditional
statements, like this:

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 4 of 8
cout << “High Score: “ << high_score << endl;
This unpleasant approach becomes even more unpleasant if there are more even scores to compare
or if a more difficult task, such as ordering the scores from highest to lowest, must be performed.
Some other approach is required.
Can we employ a loop to examine each of score0 through score6? But the six variables have
distinct names, and a loop has no way of changing from name to name. Ideally, we wish to use
“subscripts” or “indexes,” so that we may refer to score0 as score0, score1 as score1, and so on. The
notation would be exploited by this for-loop,

cout << high_score << endl;


which would examine all six variables. Java uses array variables, for indexing like this. An array
variable names a collection of individual variables, each of which possesses the same data type. For
example, we can declare and initialize an array variable that holds six doubles by stating the
following:

The name of this variable is score, and its declared data type is double[] (read this as “double
array”). The data type indicates that score is the name of a collection of doubles, and the doubles
named by the array variable are score[0], score[1], ..., score[5].
The initialization statement’s right-hand side, new double[6], constructs a new form of
object that holds six elements, each of which is a double variable.
It is traditional to draw an array like this:

The diagram shows that a newly constructed array that holds numbers starts with zeros in all the
elements.
When we wish to refer to one of the elements in the array named score, we use an index
(also known as subscript) to identify the element. The elements are indexed as score[0], score[1],
and so on, up to score[5]. For example, we can print the number held in element 3 of score by
writing,

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 5 of 8
cout << score [3] << endl;
C++ requires that an array’s indexes must be integers starting with zero.
It is helpful to think of variable score as the name of a “hotel” that has six “rooms,” where the rooms
are labelled 0 through 5. By stating score[3], we specify the precise address of one of the hotel’s
rooms, where we locate the room’s “occupant.” Of course, each of the elements of an array is itself a
variable that can be assigned. For example, say that the leading judge gives the score, 5.9, to a
skater. We insert the number into the array with this assignment:

If the skater’s next score is 4.4, then we might write,

Here is a picture that shows what we have accomplished:

We can insert values into all the array’s elements in this fashion. But most importantly, the index
contained within the square brackets may be a variable or even an integer-valued arithmetic
expression. For example,
int i = 3;
cout << score[i] << endl;
cout << score [i + 1] << endl;
locates and prints the doubles held in elements 3 and 4 of score. By using variables and expressions
as indexes, we can write intelligent loops, such the one that locates and prints a skater’s highest
score:

cout << high_score << endl;

The phrase, score[i - 1], refers to the element that immediately precedes element score[i]. For
example, for the array pictured earlier and when i holds 2, the result of executing the above
assignments produces this array:

If variable i held 0 (or a negative number), then score[i - 1] would be a nonsensical reference, and
the execution would halt with a run-time exception, called an ArrayIndexOutOfBoundsException.

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 6 of 8
The above example showed how an array might hold a set of numbers. But arrays can hold
characters, booleans, strings, and indeed, any form of object whatsoever. For example, the words of
a sentence might be stored into an array like this:

Array word is declared so that it keeps strings in its elements. Second, if we have written a class,
say, class BankAccount, then we can declare an array to hold objects constructed from the class:

The previous sequence of statements constructs two BankAccount objects and assigns them to
array r. Because they can hold numbers, booleans, characters, and objects, arrays are heavily used
in computer programming to model sets or collections. The collection of skating scores seen at the
beginning of this section is one simple example, but there are many others:
• a bank’s customer accounts
• a library’s books
• playing pieces or players for an interactive game
• a table of logarithms or solutions to an algebraic equation
• indeed, any “data bank,” where multiple objects are held for reference
The sections that follow show how to use arrays to model these and other examples.

What’s New

Activity 1: ARRAYS (20 pts.)


INSTRUCTION: Read the questions very carefully. Write your answer on a yellow sheet of pad
paper.

1. Say that we declare this array: int r[4] = {12,0,3,45}; What do each of these loops print? (Hint: It
will be helpful to draw a picture of array r, like the ones seen in this section, and update the
picture while you trace the execution of each loop.) Look at the first one for an example.
EXAMPLE:
cout << r[3] << endl;
ANSWER: 3
SOLUTION:
12 0 3 45
r[0] r[1] r[3] r[4]

©2020 Mind and Integrity College, Inc. All Rights Reserved Page 7 of 8
A. for (int i = 0; i < 4; i++)
{ cout << r[i] << endl; }
B. int r[4] = {12,0,3,45};
int i = 1;
r[i] = 10;
r[i+2] = r[i] + 2;
for (int i = 0; i < 4; i++)
{
cout << r[i] << endl;
}
C. for ( int i = 3; i >= 0; i = i - 1 )
{
r[i] = i * 2;
}
for ( int i = 0; i < 4; i = i + 1 )
{
cout << r[i] << endl;
}
D. r[0] = 10;
for ( int i = 1; i != 4; i = i + 1 )
{
r[i] = r[i - 1] * 2;
}
for ( int i = 0; i < 4; i = i + 1 )
{
cout << r[i] << endl;
}

References
“Data Structures: Arrays”
http://people.cs.ksu.edu/~schmidt/PPJv12pdf/ch8V12.pdf

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

You might also like