CP1 - Unit 10 Array
CP1 - Unit 10 Array
UNIT 12 : Array
An array is a group or collection data of same data types. For example an int array
holds the elements of int types while a double array holds the elements of double types.
https://beginnersbook.com/2014/01/c-arrays-example/
Two-Dimensional Array
https://www.geeksforgeeks.org/multidimensional-arrays-c-cpp/
99
Array
Three-Dimensional Array
https://www.geeksforgeeks.org/multidimensional-arrays-c-cpp/
Learning Objectives
Course Materials
12.1 An array is a collection of data of the same type stored at contiguous memory location.
Each data can be accessed individually by using an index. The size of the array is declared at
the beginning of the program and cannot be change during the program execution. Array must
accommodate all data to be processed, so, it is better to declare an array of bigger size if you
do not know exactly the number of data items to process.
98 78 83 88 79 93 89
0 1 2 3 4 5 6
Array indices
100
Array
An array index always starts with 0. So, if the size of the array is 7 the last index is 6 or if
size is 100 then last index is 99.
More often than not program process the same type of data repeatedly. Let’s say, there
are 50 students in the class and you need to compute their final grade and then later on you
want to list their names with their grades alphabetically or in descending (highest to lowest)
order based on the grade. Before, we only use one variable for the name and one variable for
the grade that can only hold one value at time and then we use looping so we could enter
another set of name and grade, but when another value was entered the previous value is
erased in the memory and will be replaced by the new value entered. We can declare 50
variables for the name, 50 for the grade, but, that will be tedious. The solution for this kind of
process is to use an array. We use the same name of variable for all students just imagine that
it will be divided into how many times you want it to be.
Syntax:
data_type arrayName[size];
Examples:
int age[10];
0 1 2 3 4 5 6 7 8 9
6 72 47 13 23
0 1 2 3 4
101
Array
0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
2
6 72 47 13 0 0 0 0 0
3
0 1 2 3 4 5 6 7 8 9
0 1 2
0. 0. 0. 0. 0.
0 0 0 0 0
0 1 2 3 4
0 1 2 3 4 5 6 … 48 49
Direct:
studName[3] = “Beniah”;
102
Array
You can ask the user to enter the exact or estimated size of the array.
103
Array
104
Array
12.7 Counting – based on the criteria count how many in the array met the criteria.
12.7 Searching an array – finding data that match the given requirement.
Note: Declare variable/s of same data type with the array to hold the data temporarily
when swapping the values. For ascending order, use > in the if statement and < for descending
order.
There are different methods of sorting data, one is Bubble Sorting, It is the simplest sorting
algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.
int j;
int holdAge=0;
float holdGrade=0.0;
string holdName = " ";
105
Array
bool swapped;
stud_name
Zel Ina Roi Bea
0 1 2 3
age
19 20 17 21
0 1 2 3
grade
97 90 88 80
0 1 2 3
holdAge= age[j]; // 17 20
age[j] = age[j+1];
age[j+1] = holdAge;
holdGrade= grade[j]; // 88 90
grade[j] = grade[j+1];
grade[j+1] = holdGrade;
swapped = true;
}
} // for j
if (swapped == false)
break;
} //for i
106
Array
Activities
1. 50 letter grades
2. commissions of ten salesmen
3. daily temperature in Celsius for the month of August
4. number of population of each region in the Philippines
5. names of 50 students
Programming
One-dim Array
1. Write a program that will ask the user to enter a name and then display the letters that
appeared more than once and the number of their occurrence.
107
Array
2. Profiling of Customer – write a C++program that will store the profile of 15 customers in
arrays. Input the name of the customer, gender (F and M only) and the age. After
inputting the data, display the following information – how many are male, how many are
female, how many are minors (less than 18years old), senior (60 years and above) and
adults (those that were not considered as minor and senior).
Profiling of Customer
Input:
Name:_____
Gender(M or F):____
Age( in number):____
Output:
# of Male Customer:___
# of Female Customer:___
Minor s(Less than 18 years old):___
Adults(19-59 years old):____
Seniors(60 and above): _____
3. Monthly Household Expense – Create arrays (size 15) that will hold the following
information: category and amount). Allow the user to enter the category (ex. Water bill)
and the corresponding amount spent on it. After the data entry, display the household
categories and its corresponding amount in descending order (highest amount first) and
the total amount spent in a month.
Category Amount
______ ______
______ ______
: :
108
Array
______ ______
Total: ______
4. There are 15 candidates for the position of Barangay Captain. Using an array, write a
program that will ask the user to enter the names of the candidate and the total votes
he/she received. After the data entry, display the data in descending order based on the
votes of the candidates. Compute and display also the total votes cast for this election.
Online References
https://beginnersbook.com/2014/01/c-arrays-example/
https://www.geeksforgeeks.org/arrays-in-c-cpp/
https://www.tutorialspoint.com/cplusplus/cpp_arrays.htm
109