Java 2: Quarter 3 - Module 1: DATA ARRAYS: Importance of Arrays
Java 2: Quarter 3 - Module 1: DATA ARRAYS: Importance of Arrays
JAVA 2
Quarter 3 – Module 1:
DATA ARRAYS: Importance of Arrays
©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.
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,
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.
©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
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,
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:
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:
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
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