SLG CS2 6.1 Declaration and Initialization

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Subject Code: CS2 Introduction to Computational Thinking

Module Code: 6.0 Array


Lesson Code: 6.1 Declaration and Initialization
Time Frame: 30 minutes

Time Allotment: 1 minute

After completing this module, you are expected to:


Represent homogeneous sets of data as arrays
Identify when to use arrays in a program effectively
Declare and reference elements in one-dimensional and two-dimensional arrays.

HOOK Time Allotment: 5 minutes

Consider this problem:

Given three (3) numbers [3, 1, 2], determine which among them is the lowest in value.

Recall that variables are used in programming to store or retrieve data. These data may be made up of
integers (e.g., 1, 2, 3), characters (e.g., ‘a’, ‘b’, ‘c’), or floating-point values (e.g, 1.414, 3.14, 6.02).

A typical solution for the given problem is to define three (3) variables, one for each data type. In this
case, the data type is an integer.

int num1 = 3;
int num2 = 1;
int num3 = 2;

To determine the smallest among these variables, we would need to compare each.

if( (num1 < num2) && (num1 < num3) ) {


cout << "The smallest number is: " << num1);
} else if ( (num2 < num1) && (num2 < num3) ) { cout <<
"The smallest number is: " << num2;
} else if ( (num3 < num1) && (num3 < num2) ) { cout <<
"The smallest number is: " << num3;
}

While the solution above is easy to understand, it takes up a lot of effort to write. If you were asked to
compare a hundred variables, how would you imagine solving it then? There MUST be a better way.

LG-QUITO-CS2-6.1 CS 3 Page 1 of 5
Notice that the problem requires us to store multiple variables of the same data type. In this case,
all of them are integers. We can place all of these variables in a single container, an array.

Time Allotment:20 minutes

WHAT IS AN ARRAY?
An array is a container that can store multiple data that have the same data type. Technically, an array is a
variable (i.e., storage for data). So, an array is a variable of variables. To eliminate confusion, we refer to
the “variables” in an array as its elements.

Consider a box of donuts:

image

source: https://www.freepik.com/free-
vector/realistic-donuts-box-holded-by-
hands_5971250.htm

In the image above, there is a box that has 6 donuts inside, each having a flavor. Think of the box as the
array (container), the individual donuts as the element of the array and the flavor of the donut as the data of
the array.

To give you a better understanding of what an array is, let us compare an array with a variable.

How are you going to declare and initialize a variable and an array that will hold/store the flavors of the
donuts?

Using a variable we can store the flavors of the donuts using the C++ statement:

string donutFlavor1 = "original glaze", donutFlavor2 = "alcapone", donutFlavor3 = "tiramisu",


donutFlavor4 = "strawberry filled" donutFlavor5 = "Bavarian", donutFlavor6 = "chocobutternut";

LG-QUITO-CS2-6.1 CS 3 Page 2 of 5
In the C++ statement above, we declared (created) six (6) variables with a string data type and we initialized
them with six (6) different values. We discussed a while ago that an array is a variable of variables, so if we
have six (6) variables with the same data type, we can actually store these variables inside an array. An
array can be a one-dimensional array and two-dimensional array. We are going to learn one-dimensional
arrays in this learning guide.

Using a one-dimensional array we can store the flavors of the donuts using the C++ statement below:

string donutFlavor[6] = {"original glaze", "alcapone","tiramisu", "strawberry filled" , "Bavarian",


"chocobutternut"};

DECLARING AND INITIALIZING AN ARRAY

To declare an array, we use the syntax:


dataType arrayName[numberOfElements]; //numberOfElements must be a whole number

To declare and initialize an array, we use the syntax:


dataType arrayName[numberOfElements] = {initialValues};

Like other variables, the values in the elements of an array can also be assigned when the array is declared.
The assigning of values to the elements of the array at the time of its declaration is called initializing of the
array

We can initialize the array elements at the same time we declare the array simply by entering one or
more values, separated by commas, in the initialValues section of the syntax. We enclose the initialValues
section in braces ({}).

string donutFlavor[6] = {"original glaze", "alcapone","tiramisu", "strawberry filled", "Bavarian",


"chocobutternut"};

In the C++ statement above, we declared an array named donutFlavor with a string data type. The
donutFlavor array has six (6) elements. It’s like having a one variable and inside that variable, there are six
(6) variables.

The variables in an array are stored in consecutive locations in the computer’s internal memory. Each
variable in an array has the same name and data type. We distinguish one variable in a one-dimensional
array from another variable in the same array using a unique number. The unique number, which
is always an integer, is called a subscript/index number.
The subscript/index number indicates the variable’s position in the array and is assigned by the computer
when the array is created in internal memory. The first variable in a one-dimensional array is assigned a
subscript of 0 / index 0, the second a subscript of 1 /index 1, and so on.

LG-QUITO-CS2-6.1 CS 3 Page 3 of 5
We refer to each variable in an array by the array’s name and the variable’s subscript, which is specified in
a set of square brackets immediately following the array name.

To refer to the first variable in a one-dimensional array named donutFlavor, we use donutFlavor[0]—read
“donut flavor sub zero.” Or “donut flavor index 0” Similarly, to refer to the second variable in the
donutFlavor array, we use donutFlavor[1]. If the donutFlavor array contains six (6) variables, we refer to
the sixth (and last) variable using donutFlavor[5].

More Examples:

int num[3]; // an array that can hold a maximum of 3 elements of int type
int is the data type of the element to be stored
num is the name of the array
3 is the size of the array

char fName[20]; // an array that can hold a maximum of 20 elements of char type

char is the data type of the element to be stored


fName is the name of the array
20 is the size of the array

int num[3] = {3, 1, 2};


// an array that can hold a maximum of 3 elements of int type

int is the data type of the element to be stored


num is the name of the array
3 is the size of the array
3,1,2 values /data of the elements inside the num array

string section[4] = {"champa", "dahlia", "ilang","sampa"}; string is

the data type of the element to be stored


section is the name of the array
3 is the size of the array
champa,dahlia
ilang,sampa values /data of the elements inside the section array

LG-QUITO-CS2-6.1 CS 3 Page 4 of 5
Time Allotment: 2 minutes

Create an array with colors as its name and string as its data type with 3 elements.

[ ] = { "red","blue", "yellow" };

Create an array with faveLetter as its name and char as its data type with 3 elements. Initialize
the array with valid values/data.

You may see the answers at the end of this Learning Guide.

Time Allotment:1 minute

An array is a group of variables that have the same name and data type.
To declare a one-dimensional array, we use the syntax:
dataType arrayName[numberOfElements]; //numberOfElements must be a whole number

To declare and initialize a one-dimensional array array, we use the syntax:


dataType arrayName[numberOfElements] = {initialValues};

Arrays. (n.d.). Cal Poly Pomona. https://www.cpp.edu/%7Eelab/ECE114/Array.html#:


%7E:text=Arrays%20in%20C%2B%2B,each%20with%20its%20own%20identifier).

Zak, D. (2016). An introduction to programming with C++. Boston, MA: Cengage Learning.

Prepared by: JOHN CARLO G. QUITO Reviewed by: Trextan Thaddeus Sanchez
Position: SSTIII
Position: SSTIII Campus: PSHS-SMC
Campus: PSHS-CLC

© 2020 Philippine Science High School System. All rights reserved. This document may contain proprietary information and may only be released to
third parties with the approval of management. Document is uncontrolled unless otherwise marked; uncontrolled documents are not
subject to update notification.

LG-QUITO-CS2-6.1 CS 3 Page 5 of 5

You might also like