2. C++ Style Data Structures:
• An ordered set (sequence) with a
fixed number of elements, all of the
same type,
where the basic operation is
Direct access to each element in
the array so values can be retrieved
from or stored in this element.
3. C++ Style Data Structures:
Properties:
Ordered so there is a first element, a second one, etc.
Fixed number of elements — fixed capacity
Elements must be the same type (and size);
use arrays only for homogeneous data sets.
Direct access: Access an element by giving its location
The time to access each element is the same for all
elements, regardless of position.
in contrast to sequential access (where to access an
element, one must first access all those that precede
it.)
4. Declaring arrays in C++
where
element_type is any type (data type)
array_name is the name of the array — any valid identifier
CAPACITY (a positive integer constant) is the number of
elements in the array
score[0]
score[1]
score[2]
score[3]
score[99]
.
.
.
.
.
.
element_type array_name[CAPACITY];
e.g., double score[100];
The elements (or positions) of the
array are indexed 0, 1, 2, . . .,
CAPACITY - 1.
Can't input the capacity,
Why?
The compiler reserves a block of “consecutive” memory locations, enough
to hold CAPACITY values of type element_type.
5. indices numbered 0, 1, 2, . . ., CAPACITY -
1
How well does C/C++ implement an array ADT?
As an ADT(Abstract Data Type) In C++
ordered
fixed size
same type
elements
direct access
element_type is the type of elements
CAPACITY specifies the capacity of the
array
subscript operator
[]
An Array ADT (Abstract Data Type) is a logical concept that
defines an ordered collection of elements of the same type,
where each element is accessed using an index.
6. an array literal
Array Initialization
Example:
double rate[5] = {0.11, 0.13, 0.16, 0.18, 0.21};
Note 1: If fewer values supplied than array's capacity, remaining
elements assigned 0.
double rate[5] = {0.11, 0.13, 0.16};
Note 2: It is an error if more values are supplied than the declared size of the
array.
How this error is handled, however, will vary from one compiler to
another.
rate
0 1 2 3 4
0.11 0.13 0.16 0 0
rate
0 1 2 3 4
0.11 0.13 0.16 0.18 0.21
In C++, arrays can be initialized when they are declared.
Numeric arrays:
element_type num_array[CAPACITY] = {list_of_initial_values};
7. Note 1: If fewer values are supplied than the declared size of the array,
the zeroes used to fill un-initialized elements are interpreted as
the null character '0' whose ASCII code is 0.
const int NAME_LENGTH = 10;
char collegeName[NAME_LENGTH]={'C', 'a', 'l', 'v', 'i', 'n'};
vowel
0 1 2 3 4
A E I O U
char vowel[5] = {'A', 'E', 'I', 'O', 'U'};
Character Arrays:
Character arrays may be initialized in the same manner as numeric
arrays.
declares vowel to be an array of 5 characters and initializes it as
follows:
collegeName
0 1 2 3 4 5 6 7 8 9
C a l v i n 0 0 0 0
8. Addresses:
When an array is declared, the address of the first byte (or word) in the
block of memory associated with the array is called the base address of
the array.
Each array reference must be translated into an offset from this base
address.
For example, if each element of array score will be stored in 8 bytes and
the base address of score is 0x1396. A statement such as
cout << score[3] << endl;
requires that array reference
score[3]
be translated into a memory address:
0x1396 + 3 * sizeof
(double)
= 0x1396 + 3 * 8
= 0x1396 + 0x18
= 0x13ae
The contents of the memory word with this
address 0x13ae can then be retrieved and
displayed.
An address translation like this is carried out
each time an array element is accessed.
score[3]
[0]
[1]
[2]
[3]
[99]
.
.
.
.
.
.
score 0x1396
0x13ae
What will be the
time complexity
9. The value of array_name is actually the base address of array_name
array_name + index is the address of array_name[index].
An array reference array_name[index]
is equivalent to
For example, the following statements of pseudocode are
equivalent:
print score[3]
print *(score + 3)
Note: No bounds checking of indices is done!
* is the dereferencing operator
*ref returns the contents of the memory location with
address ref
*(array_name + index)
What will happen
incase
of going overboard
10. Problems with Arrays
When we say “The capacity of an array cannot change
during program execution”, two main problems arise:
1.Memory Wastage️🗑️
•If the array is declared too large, unused elements
waste memory.
•Example: int arr[1000]; but you only ever use 10
elements → 990 elements wasted.
2.Out of Range Errors 🚫
•If the array is declared too small, and your program
tries to store more elements than the capacity, you’ll get
an out-of-bounds error (undefined behavior in C/C++).
•Example: int arr[5]; but you try arr[7] = 10; → invalid
memory access.
11. Problems with Arrays
3. Arrays are NOT self-contained objects
🔴 Problem 1: No way to find the last value stored
•In C/C++, an array is just a block of memory.
•The compiler does not keep track of how many elements you’ve
inserted.
•Example:
int arr[10]; arr[0] = 5; arr[1] = 8; // How do we know this is the "last"
filled index?
👉 There is no built-in mechanism to check how many slots are used.
You must manually maintain a separate counter (e.g., int size;).
12. Problems with Arrays
4: Not a self-contained object (OOP issue)
•In Object-Oriented Programming (OOP), a self-contained
object should encapsulate:
•Data (the actual values).
•Metadata (like size, capacity).
•Methods (operations such as insert, delete, find, etc.).
•Arrays in C/C++ only store data → they don’t store their own
size or provide methods.
•This violates encapsulation and abstraction principles in
OOP.
13. C++ Style Multidimensional Arrays
Most high level languages support arrays with more than one
dimension.
2D arrays are useful when data has to be arranged in tabular form.
Higher dimensional arrays appropriate when several
characteristics associated with data.
Test 1 Test 2 Test 3 Test 4
Student 1 99.0 93.5 89.0 91.0
Student 2 66.0 68.0 84.5 82.0
Student 3 88.5 78.5 70.0 65.0
: : : : :
: : : : :
Student-n 100.0 99.5 100.0 99.0
For storage and processing, use a two-dimensional
array.
Example: A table of test scores for several different
students on
several different tests.
14. Declaring Two-Dimensional
Arrays
Standard form of declaration:
element_type array_name[NUM_ROWS][NUM_COLUMNS];
Example:
double scoresTable[30][4];
Initialization
List the initial values in braces, row by row;
May use internal braces for each row to improve readability.
Example:
double rates[][] = {{0.50, 0.55, 0.53}, // first row
{0.63, 0.58, 0.55}}; // second row
[0]
[1]
[2]
[3]
[29]
[0] [[1] [2] [3]
15. Processing Two-Dimensional
Arrays
Remember: Rows (and) columns are numbered from zero!!
Use doubly-indexed variables:
scoresTable[2][3] is the entry in row 2 and column 3
row index column index
Use nested loops to vary the two indices, most often in a rowwise
manner.
Counting
from 0
16. Processing Two-Dimensional
Arrays
int arr[4][2] = {
{1234, 56},
{1212, 33},
{1434, 80},
{1312, 78}
} ;
#include <iostream>
using namespace std;
int main() {
int arr[4][2] = {
{ 10, 11 },
{ 20, 21 },
{ 30, 31 },
{ 40, 41 }
};
int i, j;
cout << "Printing a 2D Array:
n";
for (i = 0; i < 4; i++)
{
for (j = 0; j < 2; j++)
{
cout << "t" << arr[i][j];
}
cout << endl;
}
return 0;
}
int arr[4][2] = {1234,
56, 1212, 33, 1434, 80,
1312, 78};
17. Higher-Dimensional Arrays
(3D)
The methods for 2D arrays extend in the obvious way to 3D arrays.
A 3D array is basically an "array of arrays of arrays."
It extends the concept of 2D arrays (matrix) into another dimension.
Useful for storing data in cubes, 3D grids, or multi-layered structures.
Syntax:
data_type array_name[x][y][z];
x → number of blocks (layers)
y → number of rows per block
z → number of columns per row
So total memory = x * y * z * sizeof(data_type)
Example of Declaration
int arr[2][3][4];
•2 blocks
•3 rows in each block
•4 columns in each row
👉 Total elements = 2 × 3 × 4 = 24
18. Higher-Dimensional Arrays
(3D)
🔹 Accessing Elements
arr[block][row][column];
Example:
cout << arr[1][0][2];
// Access block 2, row 1, column
3 Output: 9
→
.
int arr[2][2][3] = {
{
{1, 2, 3},
{4, 5, 6}
},
{
{7, 8, 9},
{10, 11, 12}
}
};
Use Cases of 3D Arrays
•3D graphics / voxel data (like in games, simulations).
•Scientific computations (storing values in 3D grids).
•Storing multiple matrices (like multiple layers of image data).
•Scheduling problems (day × time × task).
19. Higher-Dimensional Arrays
(3D)
🔹 Basic Operations: 1- Traversal (Printing all elements)
#include <iostream>
using namespace std;
int main() {
int arr[2][2][3] = {
{{1,2,3}, {4,5,6}},
{{7,8,9}, {10,11,12}}
};
for (int i = 0; i < 2; i++) { // block
for (int j = 0; j < 2; j++) { // row
for (int k = 0; k < 3; k++) { // column
cout << "arr[" << i << "][" << j << "][" << k << "] = "
<< arr[i][j][k] << endl;
}
}
}
return 0;
}
20. Higher-Dimensional Arrays
(3D)
🔹 Basic Operations: 2. Insertion)
arr[1][1][2] = 50; // insert 50 into block=1, row=1, column=2
🔹 Basic Operations: 3. Updation
arr[0][0][1] = 99; // update value at block=0, row=0, col=1 to 99
21. Higher-Dimensional Arrays
(3D)
🔹 Basic Operations: 4. Searching
int x = 9, found = 0;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
for(int k=0;k<3;k++){
if(arr[i][j][k]==x){
cout<<"Found at ["<<i<<"]["<<j<<"]["<<k<<"]"<<endl;
found = 1;
}
}
}
}
if(!found) cout<<"Not Found!";
22. Arrays of Arrays
🔹 What is an Array of Arrays?
An array of arrays means storing arrays inside
another array.
This structure allows us to represent
multidimensional data (like tables, grids, or even
cubes).
In C/C++, a 2D array is the most common form of
array of arrays.
Example:
int arr[3][4];
This means:
•An array of 3 elements
•Each element itself is an array of 4 integers
So, you can think of it as:
less
arr[0] → [ ? , ? , ? , ? ] // first row (array of 4 ints)
arr[1] → [ ? , ? , ? , ? ] // second row
arr[2] → [ ? , ? , ? , ? ] // third row
23. scoresTable[i] is the i-th row of the table
Address Translation:
Address Translation:
The array-of-arrays structure of multidimensional arrays explains
address translation.
Suppose the base address of scoresTable is 0x12348:
scoresTable[10] 0x12348 + 10*(sizeof RowOfTable)
In general, an n-dimensional array can be viewed (recursively) as a
one-dimensional array whose elements are (n - 1)-dimensional arrays.
In any case:
scoresTable[i][j] should be thought of as (scoresTable[i])[j]
that is, as finding the j-th element of scoresTable[i].
scoresTable[10]
[3] base(scoresTable[10]) + 3*(sizeof double)
scoresTable[10]
[4]
[3]
[0]
[1]
[9]
[10]
= 0x12348 + 10 * (4 * 8) + 3 * 8
= 0x124a0
= 0x12348 + 10 * (4 * 8)
24. Implementing Multidimensional
Arrays
More complicated than one dimensional arrays.
Memory is organized as a sequence of memory locations, and is
thus 1D
How to use a 1D structure to store a MD structure?
A B C D
E F G H
I J K L
A character requires a single byte
Compiler instructed to reserve 12 consecutive bytes
Two ways to store consecutively i.e. rowwise and columnwise.
25. Implementing Multidimensional
Arrays
A B C D
E F G H
I J K L
RowWise
A
B
C
D
E
F
G
H
I
J
K
L
ColumnWise
A
E
I
B
F
J
C
G
K
D
H
L
A B C D
E F G H
I J K L
A B C D
E F G H
I J K L