SLM-Computer-Programming-Q4 MODULE 3
SLM-Computer-Programming-Q4 MODULE 3
Computer
Programming
Quarter IV – Module 3:
Concepts on Single and Multidimensional Arrays
Republic Act 8293, section 176 states that: No copyright shall subsist in any work
of the Government of the Philippines. However, prior approval of the government agency or
office wherein the work is created shall be necessary for exploitation of such work for profit.
Such agency or office may, among other things, impose as a condition the payment of
royalties.
Borrowed materials (i.e., songs, stories, poems, pictures, photos, brand names,
trademarks, etc.) included in this module are owned by their respective copyright holders.
Every effort has been exerted to locate and seek permission to use these materials from
their respective copyright owners. The publisher and authors do not represent nor claim
ownership over them.
Each SLM is composed of different parts. Each part shall guide you step-by-
step as you discover and understand the lesson prepared for you.
At the end of each module, you need to answer the test to self-check your
learning. Answer keys are provided for each activity and test. We trust that you will
be honest in using these.
In addition to the material in the main text, Notes to the Teacher are also
provided to our facilitators and parents for strategies and reminders on how they
can best help you on your home-based learning.
Please use this module with care. Do not put unnecessary marks on any
part of this SLM. Use a separate sheet of paper in answering the exercises and
tests. And read the instructions carefully before performing each task.
If you have any questions in using this SLM or any difficulty in answering
the tasks in this module, do not hesitate to consult your teacher or facilitator.
Thank you.
ii
For the learner:
This module was designed to provide you with fun and meaningful opportunities
for guided and independent learning at your own pace and time. You will be
enabled to process the contents of the learning resource while being an active
learner.
iii
Learners can share their thoughts and feeling
about the lessons.
1. Use the module with care. Do not put unnecessary mark/s on any part of
the module. Use a separate sheet of paper in answering the exercises.
2. Read the instruction carefully before doing each task.
3. Observe honesty and integrity in doing the tasks and checking your
answers.
4. Finish the task at hand before proceeding to the next.
5. Return this module to your teacher/facilitator once you are through with it.
If you encounter any difficulty in answering the tasks in this module, do not
hesitate to consult your teacher or facilitator. Always bear in mind that you are
not alone.
We hope that through this material, you will experience meaningful learning
and gain deep understanding of the relevant competencies. You can do it!
iv
Explore
Introduction:
Previous lessons have shown how to process primitive data like integers in
computer programming. That is useful, but certainly not sufficient to handle
bigger amounts of data. In most cases, organizing numerous data would be
uncomplicated using an array which is a collection of values or elements of the
same data type together into one variable. Rather than having separate variables
for many different values in a form of integers or strings or other data types, you
could collect all of them together into one variable. This does not mean that you
will want to have all of the values with the same data type in any computer
program united into one array. However, there will be times when certain values
are all related to one another in a way appropriate for an array.
The important detail to understand about arrays is that the values are
placed as elements in the array to be access, manage, or store from the array using
an index. You can think of an array as a list of elements where each of the
elements has a unique place in the list, such as 1st, 2nd, 3rd and so on. You can
access any element from the list using its location or index in the list. The order of
the elements in the array is unimportant.
Subtask:
1. Differentiate variable, single dimensional and multidimensional array in
using them creating computer program code.
2. Compare how a variable, single dimensional and multidimensional array is
programmed in Java, C and Python programming languages.
3. Create a computer program using single dimensional and multidimensional
array.
4.
WHAT IS AN ARRAY?
The variable allows us to store a single value at a time, what if we want to
store more than 1 values. Then, we can make use of arrays.
Array is a data structure that is used to store multiple values in a single
variable, instead of declaring separate variables for each value. Each value is called
an element of the array. The elements of the array share the same variable name,
but each element has its own unique index number.
Typically, these elements are all the same data type, like an integer or string.
Arrays are commonly used in computer programs to organize data so that a related
set of values can be easily sorted or searched.
Figure 1
The java code snippet in figure 1, shows how to assign a value of 5 at first
position or index 0 in the array with number as array variable name.
Arrays can be single or multidimensional. The number of subscript or
index determines the dimensions of the array. An array of one dimension is known
as a one-dimensional array or 1-D array, while an array of two dimensions is
known as a two-dimensional array or 2-D array.
ONE-DIMENSIONAL ARRAY
Conceptually, you can think of a one-dimensional array as a row, where
elements are stored one after another.
Figure 2
Figure 2 shows how we perceive a one-dimensional array as a single row.
There are 6 elements in the array with index starting from 0. Commonly, an array
index always starts at 0.
To declare a variable:
Figure 3
Figure 4
Figure 5
Figure 6
Python does not have a concept of Array, instead Python provides another
data structure called List, which provides similar functionality as arrays in any
other language like Java and C.
Figure 7
Figure 8
Q4_STE_Computer_Programming_ Module 3 Page 9 of 35
In figure 8, name is the array variable with datatype String and “Gomez”,
“Burgos”, “Zamora” are the array elements. Individually, we can say that:
name[0] = “Gomez”
name[1] = “Burgos”
name[2] = “Zamora”
Figure 9
Figure 10
Figure 11
Figure 12
Figure 12 shows a sample Java code to print the first element of the array
which is at index 0, thus, number [0] is how we access the first element and
number [1] is how to access the second element and so on.
If we compile and execute the sample code in figure 12, it will display 5 on
the output screen as the first element at index 0 as shown in figure 13.
Figure 13
Figure 14
Figure 15
Figure 16
If we compile and execute the sample code in figure 15, it will display 100 on
the output screen as the second element at index 1 as shown in figure 16.
Figure 17
Array Length
In Java, to find out how many elements an array has, use the length
property.
Figure 18
Array with 6
elements
Formula to
compute the size
of the Array
Figure 19
In Python, there is a built-in function called len( ) for getting the total
number of items in a list.
Figure 20
As a solution, we make use of for loop as shown in figure 22, to avoid using
the printing command multiple times to print or access the elements of an array.
Figure 22
Python Code
C Code
Figure 23
Figure 24
If we compile and execute the Java code in figure 24, the output will be 35
as the largest element in the Array as shown in figure 25.
Figure 25
Figure 27
Figure 28
You can also specify the size of the elements upon declaration.
int [ ] [ ] number = new int [3] [4];
This code means that there are 3 sets of arrays with 4 elements each.
Conceptually, a two-dimensional array can be represented as a single table with
rows and columns.
In the example, the length of the 2D Array is 12. The array can contain up
to 12 elements because there are 3 rows and 4 columns.
Array length of each set of 1D Array is 4. The first, second and third set
have the same length which is 4. It is because we specify the size of the array upon
declaration.
Figure 29
You can also type the code in a way the elements are arrange in rows and
columns for easy identification of indexes during access and modification of values
in the array like in figure 30.
Figure 30
You will notice that each row of multidimensional array in Java can have
different lengths upon assigning initial values. From the sample code, the first set
{5, 10, 15, 30} of array has 4 length, and the second set {100, 200, 300} has 3
length.
To access the elements in a 2D array, specify two indexes: one for the set
of array, and one for the element inside that array.
The index of the first set of array is 0, and the index of its elements also
starts at 0. The index of the second set is 1, the third set is 2, and so on if there
are more sets of arrays. Thus, if we are going to access or print the first element in
the first array, the code will be as follows:
Figure 32
If we are going to access or display the value 300 which is the third element
in the second set of array, the code will be as follows:
Figure 33
If the code in figure 33 is compiled and executed, it will display 300 in the
output screen as shown in figure 34.
Figure 34
The following Java Code displays all the elements in a 2D Array using the
nested for-loop statement.
Figure 35
C Two-Dimensional Array
Figure 36 shows a sample C code to declare, initialize a value, access a
single element and print all the elements in a 2D Array.
Print all the elements using for loop Declare and Initialize
Figure 37
Main array
First table (index 0)
First set of Array (index 0)
Second set of Array (index 1)
Element
Set of Array
Table
Figure 38
In figure 38, the Java sample code contains 2 tables, and each table has 2
two sets of arrays.
For the address index [1] [1] [1], it means Second Table, Second set of Array,
and Second Element, that’s why the output element is 112. Remember the index of
a set of array or the element always starts at 0.
Loop through all the elements in a 3D Array using the for-each loop:
C Three-Dimensional Array
Figure 41 shows a sample C code to declare, initialize a value, access a
single element and print all the elements in a 3D Array.
Figure 40
Figure 41
Apply
2. Create a program that outputs 2 sets of arrays. The first array contains 5 names
and replace the last element with your name for the second array.
Sample Output:
3. Create a program that contains at least 10 integers in an array and outputs all
the odd numbers in the array.
Sample Output:
MULTIPLE CHOICE:
Instructions: Choose the letter of the best answer. Write the chosen letter on a
separate sheet of paper.
a. 76 b. 28
c. 26 d. 74
a. 3 b. 2
c. 1 d. 0
5. These are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
a. Arrays b. For-Loop
a. 1 2 3 4 5 b. 6 7 8 9 10
c. 2 3 4 5 6 7 8 9 10 d. 1 2 3 4 5 6 7 8 9 10
a. 6 b. 7
c. 11 d. 10
a. 2 b. 9
c. 235789 d. 273589
9. What are the indexes for this array? int [ ] k = { 11, 12, 13, 14, 15};
a. 0, 1, 2, 3, 4 b. 1, 2, 3, 4, 5
c. 11, 12, 13, 14, 15 d. 10, 11, 12, 13, 14
a. 1 5 b. 6
c. 1 7 d. 8
3. Where did you encounter difficulties in this lesson or module, and what did you
do to deal with it?
B.
6. Three-dimensional Array APPLY: Creating an Array (1)
7. 5
8. 2
9. 3
10. 3
1. a
2. b
3. a
4. d
5. a
6. d
7. c
8. b APPLY: Creating an Array (2)
9. a
10. c