Agapay TN08 M2-Forma Arrays
Agapay TN08 M2-Forma Arrays
CCS007L
(COMPUTER
PROGRAMMING 2)
EXERCISE
2
ARRAYS
Members (if
Group):
TN08
Section:
Alejandro Legazpi
Professor:
I. PROGRAM OUTCOME/S (PO) ADDRESSED BY THE LABORATORY
EXERCISE
CCS007L-Computer Page 2
An array consists of a name and the number of elements of the array.
You can refer to a specific array element by the array name and the
element number, which is known as the index number. Array index
element number always starts with 0(zero).
CCS007L-Computer Page 3
1. You can refer to a large number of elements by just specifying the
index number and the array name.
2. Arrays make it easy to do calculations in a loop.
A. One-Dimensional Arrays
Declaring Arrays
Initializing Arrays
In both cases, local and global, when we declare an array, we have the
possibility to assign initial values to each one of its elements by enclosing
the values in braces { }.
data_type
array_name[n]={val[0],val[1],..,val[n
elements
For example:
int num[5] = { 16, 2, 77, 40, 120 };
char word[3]={‘J’,’A’,’Y’};
CCS007L-Computer Page 4
array_name[index] or
variable_object_identifier=array_n
Examples:
ame[index];
y=
Number[0];
x=
Pressure[8];
letter =
Word[1];
CCS007L-Computer Page 5
The format is as simple as:
array_name[index]=elem
ent value;
Examples:
Number[0]=-5;
Traffic[1]=“Green”;
Word[1]=‘J’;
B. Multidimensional Arrays
data_type array_name[row][column];
For examples:
int x[3][4];
float matrix[20][25];
CCS007L-Computer Page 6
form:
array_name[row_index][column_index];
For example, the way to reference the second element vertically and
V. LABORATORY ACTIVITY
CCS007L-Computer Page 7
Write a program that identifies the position of all even numbers in
Write a program that will ask the user to enter the size of the array n <
100. Once the array size is validated, it is required that the user will enter
the elements of the array. Finally, display the sorted array elements using
CCS007L-Computer Page 8
any sorting algorithm from smallest to greatest.
Sample Output:
Enter the size of the
array: 4 Array element
1: 6
Array element 2: 4
Array element 3: 5
Array element 4: 2
The sorted array is: 2 4 5 6
CCS007L-Computer Page 9
Output:(screenshot of the output)
CCS007L-Computer Page 10
all capital letters only.
Sample Output:
Enter a string: WELCOME TO
A 2
E 2
I 0
O 2
U 0
Whitespaces – 2
CCS007L-Computer Page 11
Output:(screenshot of the output)
CCS007L-Computer Page 12
VI. QUESTION AND ANSWER
CCS007L-Computer Page 13
Why is it necessary, being a programmer, to use arrays? What are the
risks of not using arrays?
Arrays are critical for managing multiple related values efficiently,
allowing for organized code, quick indexed access, and streamlined
operations such as sorting and searching. Without arrays, code becomes
cluttered, harder to maintain, and less scalable, leading to inefficient
memory use and error-prone programming.
VII.REFERENCES
Abraham (2015). Coding for dummies. John Wiley and Sons: Hoboken, NJ
Zak, D (2015). An Introduction to Programming with C++. 8th Edition
Cadenhead, R et. Al. (2016). C++ in 24 Hours, Sams Teach Yourself (6th
Edition).Sams Publishing
McGrath, M. (2017). C++ programming in easy steps (5th ed.).
Warwickshire, United Kingdom: Easy Steps Limited
CCS007L-Computer Page 14