Save
MIDTERM_2ND SEM.1
Data Structure & Algorithm Analysis
ARRAY
Save
Share
Learn
Content
Leaderboard
Share
Learn
Created by
rein
Visit profile
Cards (30)
ARRAY
a special type of data that use to collect elements of the
same
type
ARRAY
allow for easy
data
management, especially when dealing with multiple
values
ARRAY
each
element
is accessed using an index
VARIABLE
can hold
one
value
ex.
int
a = 12
ARRAY
can hold
multiple
values
ex:
int
a[] = {4,5}
2 TYPES OF ARRAY
1D ARRAY
MULTIDIMENSIONAL ARRAY
ONE DIMENSIONAL ARRAY (1D)
type of
linear
array, that access collection of elements in a
single index
value.
DECLARATION OF AN ARRAY
the format of declaring an array is
(data type array-name[size]
A)
format
1
data type array-name
[size]
how to declare an array?
accessing the element of an array
cout << num[
index
];
choose the correct output
The array elements are: 5 7 9 1 0 100 200 300
Traversing
is Iterating through an
array
using a
loop
.
for
(int i = 0; i < 5; i++) {
cout
<< arr[i]; }
Updating
Changing
an existing
element’s
value.
arr[1] = 100;
Appending
Adding
an element to the end (only for
vector
).
vector<int> v = {1, 2};
v.push_back
(3);
Inserting
Adding an
element
at a specific position (for
vector
).
v.insert
(v.begin() + 1, 50);
Deleting
Removing an element by shifting elements (
array
) or using erase() (
vector
).
v.erase
(v.begin() + 1);
Searching
Finding an
element
using
find()
function.
auto it = find(v.begin(), v.end(), 3);
Sorting
Arranging
elements
in ascending/
descending
order.
sort
(arr, arr + 5);
Size
Getting the
number
of elements in a vector.
cout <<
v.size
();
Resizing
Changing the
size
of a vector dynamically.
v.resize
(10);
Clearing
Removing
all elements
from a vector.
v.clear
();
vector
automatically
resize
, dynamic array.
standard template library
.
#
include
vector.
v.push_back;
add
an element at the
very end
Multidimensional Array
An array with
multiple
dimensions.
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
Pointer Arrays
Storing
an array dynamically using
pointers.
int* ptr = new int[5]; delete[]
ptr
;
Dynamic Allocation
Allocating memory at
runtime
.
int* arr = new int[10]; delete[] arr;
Copying Arrays
Copying
elements
from one
array
to another.
copy(begin(arr1), end(arr1), begin(arr2));
TWO DIMENSIONAL ARRAY (
2D
)
collection of a fixed no. of components (of the same
type
) arranged in two dimensions
DECLARATION
SYNTAX
nested because there’s
2
position needed (row, and
columns
) para hindi malito and ma-define or ma-specify maigi yung
rows
and column
TWO DIMENSIONAL ARRAY
sometimes called
matrices
or tables