SlideShare a Scribd company logo
Mul$dimensional arrays
Ilio Catallo - info@iliocatallo.it
Outline
β€’ Arrays of arrays
β€’ Arrays of arrays vs. pointers
β€’ Pointer arithme4c
β€’ Arrays of arrays vs. func4ons
β€’ The array of arrays type
β€’ Bibliography
Arrays of arrays
How to define an array
Arrays are defined by specifying:
β€’ The type common to each element
β€’ The name of the array
β€’ Its size
How to define an array
int numbers[4];
β€’ Element type: int
β€’ Name: numbers
β€’ Size: 4
Array representa+on
0 ... 3
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
numbers: β”‚ β”‚ β”‚ β”‚ β”‚ int[4]
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
Type aliases
C++ supports type aliases, i.e., new names for previously defined
types1
using integer = int;
integer a = 5; // the same as: int a = 5
1
Star'ng from C++11, type aliases can be introduced by means of the using keyword (in addi'on to the already
available typedef keyword)
Type aliases for array types
We can introduce type aliases for array types as well.
For instance, let us introduce a type alias for the type int[4]
using int4 = int[4];
Type aliases for array types
We can now define numbers in terms of the type alias int4
using int4 = int[4];
int4 numbers = {1, 7, 13, 10};
Can we now take advantage of our newly-defined alias to define an
array of, e.g., 3 elements of type int4?
Array of int4's
int4 m[3];
β€’ Element type: ?
β€’ Name: ?
β€’ Size: ?
Array of int4's
int4 m[3];
β€’ Element type: int4
β€’ Name: m
β€’ Size: 3
Arrays of arrays
Since each element of m is in turn an array, we have effec6vely
defined an array of arrays
int4 m[3];
Arrays of arrays
Specifically, m is an array of 3 elements, where each element is in
turn an array of 4 elements
int4 m[3];
Array arrays representa+on
0 1 2
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” β”‚
m: β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ int4[3]
β”‚ β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Ini$alizing an array
We know that arrays can be ini1alized using an ini#alizer list
int numbers[4] = {1, 7, 13, 10};
Ini$alizing an array
In case of numbers, the ini.alizer list is composed of int values
// 1 is a int, 7 is a int, ...
int numbers[4] = {1, 7, 13, 10};
Ini$alizing an array of arrays
In case of m, the ini.alizer list is composed of int4 values
using int4 = int[4];
int4 m[3] = { {1, 7, 14, 2}, // {1, 7, 14, 2} is an int4
{8, 16, 12, 10}, // {8, 16, 12, 10} is an int4
{27, 32, 5, 15} }; // {27, 32, 5, 15} is an int4
Accessing inner elements
It would be convenient to be able to access inner elements in m
using int4 = int[4];
int4 m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
Accessing inner elements
For example, being able to access the element in (2, 1)
using int4 = int[4];
int4 m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
Accessing inner elements
In order to do so, it is sufficient to specify the right amount of
indices (as many as the number of dimensions)
m[2][1]
Accessing inner elements
In order to do so, it is sufficient to specify the right amount of
indices (as many as the number of dimensions)
m[2][1] β†’ {27, 32, 5, 15}[1]
Accessing inner elements
In order to do so, it is sufficient to specify the right amount of
indices (as many as the number of dimensions)
m[2][1] β†’ {27, 32, 5, 15}[1] β†’ 32
Accessing inner elements
using int4 = int[4];
int4 m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
std::cout << m[2][1]; // output: 32
Mul$dimensional arrays
Note that m can therefore act as a mul$dimensional array, and,
specifically, it can be treated as a matrix
Mul$dimensional arrays
Mul$dimensional arrays extend the concept of array to one or
more dimensions
Mul$dimensional arrays
For common arrays, elements are addressed by specifying one
index
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚ β”‚ β”‚ β”‚ β”‚
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
β–²
β”‚
i
Mul$dimensional arrays
For mul(dimensional arrays, each element is iden(fied by a tuple of
indexes (as many as the number of dimensions)
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
β”‚ β”‚ β”‚ β”‚ β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚ β”‚ β”‚ β”‚ β”‚
β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€
β”‚ β”‚ β”‚ β”‚ │◀─── i
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
β–²
β”‚
j
Arrays of arrays
C++ does not provide proper mul1dimensional arrays. Instead,
mul1dimensional arrays are approximated as arrays of arrays
Memory layout
Since mul*dim. arrays are approximated as arrays of arrays, it
follows that elements are organized in memory as a sequence
m[0] m[1] m[2]
◀──────────────▢◀────────────▢◀─────────────▢
β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
m: β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ int4[3]
β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
Arrays of arrays vs. pointers
Pointer to first element
Assume we want to store the address of the first element of m
// {1, 7, 14, 2} is the 1st element
int4 m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
Pointer to first element
We start by no,cing that the element type of m is int4
int4 m[3] = { {1, 7, 14, 2}, // {1, 7, 14, 2} is an int4
{8, 16, 12, 10}, // {8, 16, 12, 10} is an int4
{27, 32, 5, 15} }; // {27, 32, 5, 15} is an int4
Pointer to first element
Since the element type is equal to int4, a pointer2
to any element
in the array has to be of type int4*
using int4 = int[4];
int4* first = ...
2
The int4 type alias provides a convenient way to introduce pointers to arrays. Without the intermediate type alias,
one would define first to be of type int(*)[4], which is arguably less readable
What to write on the RHS?
using int4 = int[4];
int4 m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
int4* first = ???
What to write on the RHS?
using int4 = int[4];
int4 m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
int4* first = &m[0]; // the address of {1, 7, 14, 2}
Pointer decay
As with any array, pointer decay can be used
using int4 = int[4];
int4 m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
// array-to-pointer decay (int4[3] β†’ int4*)
int4* first = m;
Pointer decay
β”Œ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐
0 1 2
β”‚β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” β”‚
m: β”‚β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ int4[3]
β”‚ β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜ β”‚
β”‚β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─
β–²
β”‚
β”‚
β”‚
β”‚
β”Œβ”€β”€β”€β”
first:β”‚ β”‚ int4*
β””β”€β”€β”€β”˜
Pointer arithme,c
Pointer arithme,c
Given that arrays of arrays are only a par1cular case of arrays,
pointer arithme,c can be applied on them without any special
considera1on
Pointer arithme,c: addi,on
using int4 = int[4];
int4 m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
int4* first = m; // ptr to {1, 7, 14, 2}
int4* second = m + 1; // ptr to {8, 16, 12, 10}
int4* third = m + 2; // ptr to {27, 32, 5, 15}
Pointer arithme,c
No#ce that pointer arithme#c allows moving between the rows of
our 2-dimensional array m
int4* first = m; // ptr to {1, 7, 14, 2}
int4* second = m + 1; // ptr to {8, 16, 12, 10}
int4* third = m + 2; // ptr to {27, 32, 5, 15}
Deriving the indexing operator
Let us now try to reduce inner element access to a sequence of
pointer arithme,c opera,ons
Deriving the indexing operator
To this end, let us consider the following expression:
m[2][1]
Deriving the indexing operator
By replacing [1] with its defini3on we obtain:
m[2][1] β†’ *(m[2] + 1)
Deriving the indexing operator
If we do the same for [2] we obtain:
m[2][1] β†’ *(m[2] + 1) β†’ *(*(m + 2) + 1)
Indexing operator on arrays of arrays
Accessing inner elements of a array of arrays is therefore s2ll
defined in terms of pointer arithme,c
Given an array of arrays m, and a pair of indexes i and j, the
opera3on a[i][j] is implemented as *(*(m + i) + j)
Arrays of arrays vs. func.ons
The sum_int func)on
Assume we would like to write a func3on named sum_int, which
computes the summa3on of all the elements in a 2-dimensional
array
What we know
β€’ We can model 2-dimensional arrays as arrays of arrays
β€’ Arrays of arrays follow the same rules as normal arrays
β€’ We cannot pass arrays by value
β€’ The common workaround is to design func>ons that accept a
pointer to the 1st
element, together with the array size
An interface for sum_int
As a first step, let us write sum_int so that it works properly when
m is fed as an input
using int4 = int[4];
int4 m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
An interface for sum_int
int sum_int(??? first, ??? n);
An interface for sum_int
int sum_int(int4* first, std::size_t n);
An interface for sum_int
Since we want sum_int to work with m, we impose first to be of
type int4
int sum_int(int4* first, std::size_t n);
An interface for sum_int
Hence, sum_int can only work with 2-dimensional arrays of
exactly 4 columns
int sum_int(int4* first, std::size_t n);
An interface for sum_int
int sum_int(int4* first, std::size_t n);
β€’ The number of rows is determined by the parameter n
β€’ The number of columns is fixed by the type of first (int4)
An interface for sum_int
If we need to work with 2-dimensional arrays of, e.g., 5 columns,
we need to write a new func(on4
int sum_int(int4* first, std::size_t n);
int sum_int5(int5* first, std::size_t n);
4
This can be automated by using a func%on template
sum_int β†’ sum_int4
In light of this, let us rename sum_int as sum_int4
int sum_int4(int4* first, std::size_t n);
Implemen'ng sum_int4
int sum_int4(int4* first, std::size_t n) {
int sum = 0;
for (std::size_t i = 0; i < n; ++i)
for (std::size_t j = 0; j < 4; ++j)
sum += ???
return sum;
}
Implemen'ng sum_int4
int sum_int4(int4* first, std::size_t n) {
int sum = 0;
for (std::size_t i = 0; i < n; ++i)
for (std::size_t j = 0; j < 4; ++j)
sum += *(*(first + i) + j);
return sum;
}
Implemen'ng sum_int4
int sum_int4(int4* first, std::size_t n) {
int sum = 0;
for (std::size_t i = 0; i < n; ++i)
for (std::size_t j = 0; j < 4; ++j)
sum += first[i][j];
return sum;
}
Invoking sum_int4
int main() {
using int4 = int[4];
int4 m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
int r = sum_int4(???, ???);
std::cout << "the elements sum up to " << r;
}
Invoking sum_int4
int main() {
using int4 = int[4];
int4 m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
int r = sum_int4(m, 3);
std::cout << "the elements sum up to " << r;
}
The array of arrays type
The int4 type alias
Up to now, we took advantage of the int4 type alias for the
defini7on of m
using int4 = int[4];
int4 m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
The int4 type alias
We did so in order to stress the true nature of mul1dimensional
arrays in C++
using int4 = int[4];
int4 m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
The data type of m
A"er introducing int4, we can say that m is of type int4[3]
(i.e., an array of 3 elements of type int4)
using int4 = int[4];
int4 m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
The data type of m
However, one could wonder how to express the data type of m
without introducing the int4 type alias
??? m[3] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
The data type of m
This amounts to asking what is the result of applying the [3] type
constructor on int[4]
Applying [3] on int
Construct a type "array of 3 elements of type int"
Input type int
Type constructor: [3]
Output type: int[3]
Applying [3] on int[4]
Construct a type "array of 3 elements of type int[4]"
Input type: int[4]
Type constructor: [3]
Output type: int[3][4]
The data type of m
Therefore, we conclude that m is of type int[3][4]
int m[3][4] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
Arrays of arrays
The common C++ syntax for defining arrays of arrays gives the
illusion of working with mul;dimensional arrays
int m[3][4] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
Arrays of arrays
However - though the syntax would suggest otherwise - the
element type of m is s8ll int[4], not int
int m[3][4] = { {1, 7, 14, 2},
{8, 16, 12, 10},
{27, 32, 5, 15} };
The alterna*ve signature
As with normal arrays, C++ admits func6on signatures that may
appear more natural when dealing with mul6dimensional arrays
The alterna*ve signature
Namely, we can change the signature of sum_int4 from this...
int sum_int4(int4* first, std::size_t n);
The alterna*ve signature
...to this:
int sum_int4(int m[][4], std::size_t n);
The alterna*ve signature
int sum_int4(int m[][4], std::size_t n) {
int sum = 0;
for (std::size_t i = 0; i < n; ++i)
for (std::size_t j = 0; j < 4; ++j)
sum += m[i][j];
return sum;
}
Bibliography
Bibliography
β€’ S.B. Lippman et al., C++ Primer (5th
Ed.)
β€’ B. Stroustrup, The C++ Programming Language (4th
Ed.)
β€’ StackOverflow FAQ, "How do I use arrays in C++?"

More Related Content

What's hot

Array and string
Array and stringArray and string
Array and string
prashant chelani
Β 
Array in c
Array in cArray in c
Array in c
Ravi Gelani
Β 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
imtiazalijoono
Β 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
Mazharul Islam
Β 
Arrays
ArraysArrays
Arrays
SARITHA REDDY
Β 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
Neeru Mittal
Β 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
Sharad Dubey
Β 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
Sangani Ankur
Β 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
Harshita Yadav
Β 
Array in c++
Array in c++Array in c++
Array in c++
Mahesha Mano
Β 
2D Array
2D Array 2D Array
2D Array
Ehatsham Riaz
Β 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
Awais Alam
Β 
Data types in C language
Data types in C languageData types in C language
Data types in C language
kashyap399
Β 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
Dr.Subha Krishna
Β 
Array in C
Array in CArray in C
Array in C
adityas29
Β 
C string
C stringC string
Presentation on array
Presentation on array Presentation on array
Presentation on array
topu93
Β 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
DevoAjit Gupta
Β 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
Jasleen Kaur (Chandigarh University)
Β 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
Β 

What's hot (20)

Array and string
Array and stringArray and string
Array and string
Β 
Array in c
Array in cArray in c
Array in c
Β 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
Β 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
Β 
Arrays
ArraysArrays
Arrays
Β 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
Β 
Array Of Pointers
Array Of PointersArray Of Pointers
Array Of Pointers
Β 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
Β 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
Β 
Array in c++
Array in c++Array in c++
Array in c++
Β 
2D Array
2D Array 2D Array
2D Array
Β 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
Β 
Data types in C language
Data types in C languageData types in C language
Data types in C language
Β 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
Β 
Array in C
Array in CArray in C
Array in C
Β 
C string
C stringC string
C string
Β 
Presentation on array
Presentation on array Presentation on array
Presentation on array
Β 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
Β 
Pointers in C Programming
Pointers in C ProgrammingPointers in C Programming
Pointers in C Programming
Β 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Β 

Viewers also liked

CBSE Class XI Programming in C++
CBSE Class XI Programming in C++CBSE Class XI Programming in C++
CBSE Class XI Programming in C++
Pranav Ghildiyal
Β 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overviewTAlha MAlik
Β 
Overview of c++
Overview of c++Overview of c++
Overview of c++geeeeeet
Β 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
Thooyavan Venkatachalam
Β 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)
Muhammad Tahir Bashir
Β 
Overview of c++ language
Overview of c++ language   Overview of c++ language
Overview of c++ language
samt7
Β 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
Steve Johnson
Β 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
Deepak Singh
Β 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
Β 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
AAKASH KUMAR
Β 
CS101- Introduction to Computing- Lecture 26
CS101- Introduction to Computing- Lecture 26CS101- Introduction to Computing- Lecture 26
CS101- Introduction to Computing- Lecture 26
Bilal Ahmed
Β 

Viewers also liked (12)

CBSE Class XI Programming in C++
CBSE Class XI Programming in C++CBSE Class XI Programming in C++
CBSE Class XI Programming in C++
Β 
Cs1123 3 c++ overview
Cs1123 3 c++ overviewCs1123 3 c++ overview
Cs1123 3 c++ overview
Β 
Overview of c++
Overview of c++Overview of c++
Overview of c++
Β 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
Β 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)
Β 
Overview of c++ language
Overview of c++ language   Overview of c++ language
Overview of c++ language
Β 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
Β 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
Β 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
Β 
Structure in c
Structure in cStructure in c
Structure in c
Β 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
Β 
CS101- Introduction to Computing- Lecture 26
CS101- Introduction to Computing- Lecture 26CS101- Introduction to Computing- Lecture 26
CS101- Introduction to Computing- Lecture 26
Β 

Similar to Multidimensional arrays in C++

Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Ilio Catallo
Β 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
HarmanShergill5
Β 
Generative Coding Lecture notes using coding
Generative Coding Lecture notes using codingGenerative Coding Lecture notes using coding
Generative Coding Lecture notes using coding
ssuserff773c
Β 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Rakesh Roshan
Β 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
Sujith Kumar
Β 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptx
Mrhaider4
Β 
Chap1 array
Chap1 arrayChap1 array
Chap1 array
raksharao
Β 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
AnisZahirahAzman
Β 
Arrays
ArraysArrays
Arrays
Neeru Mittal
Β 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
Β 
ARRAYS
ARRAYSARRAYS
ARRAYS
muniryaseen
Β 
Array and Pointers
Array and PointersArray and Pointers
Array and Pointers
Prof Ansari
Β 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
Β 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
Venkateswarlu Vuggam
Β 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arraysAseelhalees
Β 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
Venkateswarlu Vuggam
Β 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
Md. Imran Hossain Showrov
Β 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
TaseerRao
Β 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
AnirudhaGaikwad4
Β 

Similar to Multidimensional arrays in C++ (20)

Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Β 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
Β 
Generative Coding Lecture notes using coding
Generative Coding Lecture notes using codingGenerative Coding Lecture notes using coding
Generative Coding Lecture notes using coding
Β 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Β 
Array
ArrayArray
Array
Β 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
Β 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptx
Β 
Chap1 array
Chap1 arrayChap1 array
Chap1 array
Β 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
Β 
Arrays
ArraysArrays
Arrays
Β 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
Β 
ARRAYS
ARRAYSARRAYS
ARRAYS
Β 
Array and Pointers
Array and PointersArray and Pointers
Array and Pointers
Β 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Β 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
Β 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Β 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
Β 
Lecture 15 - Array
Lecture 15 - ArrayLecture 15 - Array
Lecture 15 - Array
Β 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
Β 
Python Session - 3
Python Session - 3Python Session - 3
Python Session - 3
Β 

More from Ilio Catallo

C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
Ilio Catallo
Β 
Regular types in C++
Regular types in C++Regular types in C++
Regular types in C++
Ilio Catallo
Β 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
Ilio Catallo
Β 
Memory management in C++
Memory management in C++Memory management in C++
Memory management in C++
Ilio Catallo
Β 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
Ilio Catallo
Β 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
Ilio Catallo
Β 
Spring MVC - Wiring the different layers
Spring MVC -  Wiring the different layersSpring MVC -  Wiring the different layers
Spring MVC - Wiring the different layers
Ilio Catallo
Β 
Java and Java platforms
Java and Java platformsJava and Java platforms
Java and Java platforms
Ilio Catallo
Β 
Spring MVC - Web Forms
Spring MVC  - Web FormsSpring MVC  - Web Forms
Spring MVC - Web Forms
Ilio Catallo
Β 
Spring MVC - The Basics
Spring MVC -  The BasicsSpring MVC -  The Basics
Spring MVC - The Basics
Ilio Catallo
Β 
Web application architecture
Web application architectureWeb application architecture
Web application architecture
Ilio Catallo
Β 
Introduction To Spring
Introduction To SpringIntroduction To Spring
Introduction To Spring
Ilio Catallo
Β 
Gestione della memoria in C++
Gestione della memoria in C++Gestione della memoria in C++
Gestione della memoria in C++
Ilio Catallo
Β 
Array in C++
Array in C++Array in C++
Array in C++
Ilio Catallo
Β 
Puntatori e Riferimenti
Puntatori e RiferimentiPuntatori e Riferimenti
Puntatori e Riferimenti
Ilio Catallo
Β 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
Ilio Catallo
Β 
JSP Standard Tag Library
JSP Standard Tag LibraryJSP Standard Tag Library
JSP Standard Tag Library
Ilio Catallo
Β 
Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3
Ilio Catallo
Β 
Validation in Jakarta Struts 1.3
Validation in Jakarta Struts 1.3Validation in Jakarta Struts 1.3
Validation in Jakarta Struts 1.3
Ilio Catallo
Β 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3
Ilio Catallo
Β 

More from Ilio Catallo (20)

C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
Β 
Regular types in C++
Regular types in C++Regular types in C++
Regular types in C++
Β 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
Β 
Memory management in C++
Memory management in C++Memory management in C++
Memory management in C++
Β 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
Β 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
Β 
Spring MVC - Wiring the different layers
Spring MVC -  Wiring the different layersSpring MVC -  Wiring the different layers
Spring MVC - Wiring the different layers
Β 
Java and Java platforms
Java and Java platformsJava and Java platforms
Java and Java platforms
Β 
Spring MVC - Web Forms
Spring MVC  - Web FormsSpring MVC  - Web Forms
Spring MVC - Web Forms
Β 
Spring MVC - The Basics
Spring MVC -  The BasicsSpring MVC -  The Basics
Spring MVC - The Basics
Β 
Web application architecture
Web application architectureWeb application architecture
Web application architecture
Β 
Introduction To Spring
Introduction To SpringIntroduction To Spring
Introduction To Spring
Β 
Gestione della memoria in C++
Gestione della memoria in C++Gestione della memoria in C++
Gestione della memoria in C++
Β 
Array in C++
Array in C++Array in C++
Array in C++
Β 
Puntatori e Riferimenti
Puntatori e RiferimentiPuntatori e Riferimenti
Puntatori e Riferimenti
Β 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
Β 
JSP Standard Tag Library
JSP Standard Tag LibraryJSP Standard Tag Library
JSP Standard Tag Library
Β 
Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3
Β 
Validation in Jakarta Struts 1.3
Validation in Jakarta Struts 1.3Validation in Jakarta Struts 1.3
Validation in Jakarta Struts 1.3
Β 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3
Β 

Recently uploaded

Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
Β 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
Β 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
ViralQR
Β 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilotβ„’
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilotβ„’Le nuove frontiere dell'AI nell'RPA con UiPath Autopilotβ„’
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilotβ„’
UiPathCommunity
Β 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
Β 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
Β 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
Β 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
Β 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
Β 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
Β 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
Β 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
Β 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
Β 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
Β 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
Β 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
Β 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
Β 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
Β 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
Β 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
Β 

Recently uploaded (20)

Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Β 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
Β 
Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.Welocme to ViralQR, your best QR code generator.
Welocme to ViralQR, your best QR code generator.
Β 
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilotβ„’
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilotβ„’Le nuove frontiere dell'AI nell'RPA con UiPath Autopilotβ„’
Le nuove frontiere dell'AI nell'RPA con UiPath Autopilotβ„’
Β 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Β 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Β 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
Β 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Β 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Β 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Β 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
Β 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Β 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
Β 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Β 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
Β 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Β 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
Β 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Β 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Β 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Β 

Multidimensional arrays in C++

  • 1. Mul$dimensional arrays Ilio Catallo - info@iliocatallo.it
  • 2. Outline β€’ Arrays of arrays β€’ Arrays of arrays vs. pointers β€’ Pointer arithme4c β€’ Arrays of arrays vs. func4ons β€’ The array of arrays type β€’ Bibliography
  • 4. How to define an array Arrays are defined by specifying: β€’ The type common to each element β€’ The name of the array β€’ Its size
  • 5. How to define an array int numbers[4]; β€’ Element type: int β€’ Name: numbers β€’ Size: 4
  • 6. Array representa+on 0 ... 3 β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” numbers: β”‚ β”‚ β”‚ β”‚ β”‚ int[4] β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
  • 7. Type aliases C++ supports type aliases, i.e., new names for previously defined types1 using integer = int; integer a = 5; // the same as: int a = 5 1 Star'ng from C++11, type aliases can be introduced by means of the using keyword (in addi'on to the already available typedef keyword)
  • 8. Type aliases for array types We can introduce type aliases for array types as well. For instance, let us introduce a type alias for the type int[4] using int4 = int[4];
  • 9. Type aliases for array types We can now define numbers in terms of the type alias int4 using int4 = int[4]; int4 numbers = {1, 7, 13, 10};
  • 10. Can we now take advantage of our newly-defined alias to define an array of, e.g., 3 elements of type int4?
  • 11. Array of int4's int4 m[3]; β€’ Element type: ? β€’ Name: ? β€’ Size: ?
  • 12. Array of int4's int4 m[3]; β€’ Element type: int4 β€’ Name: m β€’ Size: 3
  • 13. Arrays of arrays Since each element of m is in turn an array, we have effec6vely defined an array of arrays int4 m[3];
  • 14. Arrays of arrays Specifically, m is an array of 3 elements, where each element is in turn an array of 4 elements int4 m[3];
  • 15. Array arrays representa+on 0 1 2 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” β”‚ m: β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ int4[3] β”‚ β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  • 16. Ini$alizing an array We know that arrays can be ini1alized using an ini#alizer list int numbers[4] = {1, 7, 13, 10};
  • 17. Ini$alizing an array In case of numbers, the ini.alizer list is composed of int values // 1 is a int, 7 is a int, ... int numbers[4] = {1, 7, 13, 10};
  • 18. Ini$alizing an array of arrays In case of m, the ini.alizer list is composed of int4 values using int4 = int[4]; int4 m[3] = { {1, 7, 14, 2}, // {1, 7, 14, 2} is an int4 {8, 16, 12, 10}, // {8, 16, 12, 10} is an int4 {27, 32, 5, 15} }; // {27, 32, 5, 15} is an int4
  • 19. Accessing inner elements It would be convenient to be able to access inner elements in m using int4 = int[4]; int4 m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} };
  • 20. Accessing inner elements For example, being able to access the element in (2, 1) using int4 = int[4]; int4 m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} };
  • 21. Accessing inner elements In order to do so, it is sufficient to specify the right amount of indices (as many as the number of dimensions) m[2][1]
  • 22. Accessing inner elements In order to do so, it is sufficient to specify the right amount of indices (as many as the number of dimensions) m[2][1] β†’ {27, 32, 5, 15}[1]
  • 23. Accessing inner elements In order to do so, it is sufficient to specify the right amount of indices (as many as the number of dimensions) m[2][1] β†’ {27, 32, 5, 15}[1] β†’ 32
  • 24. Accessing inner elements using int4 = int[4]; int4 m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} }; std::cout << m[2][1]; // output: 32
  • 25. Mul$dimensional arrays Note that m can therefore act as a mul$dimensional array, and, specifically, it can be treated as a matrix
  • 26. Mul$dimensional arrays Mul$dimensional arrays extend the concept of array to one or more dimensions
  • 27. Mul$dimensional arrays For common arrays, elements are addressed by specifying one index β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” β”‚ β”‚ β”‚ β”‚ β”‚ β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜ β–² β”‚ i
  • 28. Mul$dimensional arrays For mul(dimensional arrays, each element is iden(fied by a tuple of indexes (as many as the number of dimensions) β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” β”‚ β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€ β”‚ β”‚ β”‚ β”‚ β”‚ β”œβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”Όβ”€β”€β”€β”€ β”‚ β”‚ β”‚ β”‚ │◀─── i β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜ β–² β”‚ j
  • 29. Arrays of arrays C++ does not provide proper mul1dimensional arrays. Instead, mul1dimensional arrays are approximated as arrays of arrays
  • 30. Memory layout Since mul*dim. arrays are approximated as arrays of arrays, it follows that elements are organized in memory as a sequence m[0] m[1] m[2] ◀──────────────▢◀────────────▢◀─────────────▢ β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” m: β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ int4[3] β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
  • 31. Arrays of arrays vs. pointers
  • 32. Pointer to first element Assume we want to store the address of the first element of m // {1, 7, 14, 2} is the 1st element int4 m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} };
  • 33. Pointer to first element We start by no,cing that the element type of m is int4 int4 m[3] = { {1, 7, 14, 2}, // {1, 7, 14, 2} is an int4 {8, 16, 12, 10}, // {8, 16, 12, 10} is an int4 {27, 32, 5, 15} }; // {27, 32, 5, 15} is an int4
  • 34. Pointer to first element Since the element type is equal to int4, a pointer2 to any element in the array has to be of type int4* using int4 = int[4]; int4* first = ... 2 The int4 type alias provides a convenient way to introduce pointers to arrays. Without the intermediate type alias, one would define first to be of type int(*)[4], which is arguably less readable
  • 35. What to write on the RHS? using int4 = int[4]; int4 m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} }; int4* first = ???
  • 36. What to write on the RHS? using int4 = int[4]; int4 m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} }; int4* first = &m[0]; // the address of {1, 7, 14, 2}
  • 37. Pointer decay As with any array, pointer decay can be used using int4 = int[4]; int4 m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} }; // array-to-pointer decay (int4[3] β†’ int4*) int4* first = m;
  • 38. Pointer decay β”Œ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ 0 1 2 β”‚β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” β”‚ β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β” β”‚ m: β”‚β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ β”‚ int4[3] β”‚ β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜ β”‚ β”‚β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ β–² β”‚ β”‚ β”‚ β”‚ β”Œβ”€β”€β”€β” first:β”‚ β”‚ int4* β””β”€β”€β”€β”˜
  • 40. Pointer arithme,c Given that arrays of arrays are only a par1cular case of arrays, pointer arithme,c can be applied on them without any special considera1on
  • 41. Pointer arithme,c: addi,on using int4 = int[4]; int4 m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} }; int4* first = m; // ptr to {1, 7, 14, 2} int4* second = m + 1; // ptr to {8, 16, 12, 10} int4* third = m + 2; // ptr to {27, 32, 5, 15}
  • 42. Pointer arithme,c No#ce that pointer arithme#c allows moving between the rows of our 2-dimensional array m int4* first = m; // ptr to {1, 7, 14, 2} int4* second = m + 1; // ptr to {8, 16, 12, 10} int4* third = m + 2; // ptr to {27, 32, 5, 15}
  • 43. Deriving the indexing operator Let us now try to reduce inner element access to a sequence of pointer arithme,c opera,ons
  • 44. Deriving the indexing operator To this end, let us consider the following expression: m[2][1]
  • 45. Deriving the indexing operator By replacing [1] with its defini3on we obtain: m[2][1] β†’ *(m[2] + 1)
  • 46. Deriving the indexing operator If we do the same for [2] we obtain: m[2][1] β†’ *(m[2] + 1) β†’ *(*(m + 2) + 1)
  • 47. Indexing operator on arrays of arrays Accessing inner elements of a array of arrays is therefore s2ll defined in terms of pointer arithme,c Given an array of arrays m, and a pair of indexes i and j, the opera3on a[i][j] is implemented as *(*(m + i) + j)
  • 48. Arrays of arrays vs. func.ons
  • 49. The sum_int func)on Assume we would like to write a func3on named sum_int, which computes the summa3on of all the elements in a 2-dimensional array
  • 50. What we know β€’ We can model 2-dimensional arrays as arrays of arrays β€’ Arrays of arrays follow the same rules as normal arrays β€’ We cannot pass arrays by value β€’ The common workaround is to design func>ons that accept a pointer to the 1st element, together with the array size
  • 51. An interface for sum_int As a first step, let us write sum_int so that it works properly when m is fed as an input using int4 = int[4]; int4 m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} };
  • 52. An interface for sum_int int sum_int(??? first, ??? n);
  • 53. An interface for sum_int int sum_int(int4* first, std::size_t n);
  • 54. An interface for sum_int Since we want sum_int to work with m, we impose first to be of type int4 int sum_int(int4* first, std::size_t n);
  • 55. An interface for sum_int Hence, sum_int can only work with 2-dimensional arrays of exactly 4 columns int sum_int(int4* first, std::size_t n);
  • 56. An interface for sum_int int sum_int(int4* first, std::size_t n); β€’ The number of rows is determined by the parameter n β€’ The number of columns is fixed by the type of first (int4)
  • 57. An interface for sum_int If we need to work with 2-dimensional arrays of, e.g., 5 columns, we need to write a new func(on4 int sum_int(int4* first, std::size_t n); int sum_int5(int5* first, std::size_t n); 4 This can be automated by using a func%on template
  • 58. sum_int β†’ sum_int4 In light of this, let us rename sum_int as sum_int4 int sum_int4(int4* first, std::size_t n);
  • 59. Implemen'ng sum_int4 int sum_int4(int4* first, std::size_t n) { int sum = 0; for (std::size_t i = 0; i < n; ++i) for (std::size_t j = 0; j < 4; ++j) sum += ??? return sum; }
  • 60. Implemen'ng sum_int4 int sum_int4(int4* first, std::size_t n) { int sum = 0; for (std::size_t i = 0; i < n; ++i) for (std::size_t j = 0; j < 4; ++j) sum += *(*(first + i) + j); return sum; }
  • 61. Implemen'ng sum_int4 int sum_int4(int4* first, std::size_t n) { int sum = 0; for (std::size_t i = 0; i < n; ++i) for (std::size_t j = 0; j < 4; ++j) sum += first[i][j]; return sum; }
  • 62. Invoking sum_int4 int main() { using int4 = int[4]; int4 m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} }; int r = sum_int4(???, ???); std::cout << "the elements sum up to " << r; }
  • 63. Invoking sum_int4 int main() { using int4 = int[4]; int4 m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} }; int r = sum_int4(m, 3); std::cout << "the elements sum up to " << r; }
  • 64. The array of arrays type
  • 65. The int4 type alias Up to now, we took advantage of the int4 type alias for the defini7on of m using int4 = int[4]; int4 m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} };
  • 66. The int4 type alias We did so in order to stress the true nature of mul1dimensional arrays in C++ using int4 = int[4]; int4 m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} };
  • 67. The data type of m A"er introducing int4, we can say that m is of type int4[3] (i.e., an array of 3 elements of type int4) using int4 = int[4]; int4 m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} };
  • 68. The data type of m However, one could wonder how to express the data type of m without introducing the int4 type alias ??? m[3] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} };
  • 69. The data type of m This amounts to asking what is the result of applying the [3] type constructor on int[4]
  • 70. Applying [3] on int Construct a type "array of 3 elements of type int" Input type int Type constructor: [3] Output type: int[3]
  • 71. Applying [3] on int[4] Construct a type "array of 3 elements of type int[4]" Input type: int[4] Type constructor: [3] Output type: int[3][4]
  • 72. The data type of m Therefore, we conclude that m is of type int[3][4] int m[3][4] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} };
  • 73. Arrays of arrays The common C++ syntax for defining arrays of arrays gives the illusion of working with mul;dimensional arrays int m[3][4] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} };
  • 74. Arrays of arrays However - though the syntax would suggest otherwise - the element type of m is s8ll int[4], not int int m[3][4] = { {1, 7, 14, 2}, {8, 16, 12, 10}, {27, 32, 5, 15} };
  • 75. The alterna*ve signature As with normal arrays, C++ admits func6on signatures that may appear more natural when dealing with mul6dimensional arrays
  • 76. The alterna*ve signature Namely, we can change the signature of sum_int4 from this... int sum_int4(int4* first, std::size_t n);
  • 77. The alterna*ve signature ...to this: int sum_int4(int m[][4], std::size_t n);
  • 78. The alterna*ve signature int sum_int4(int m[][4], std::size_t n) { int sum = 0; for (std::size_t i = 0; i < n; ++i) for (std::size_t j = 0; j < 4; ++j) sum += m[i][j]; return sum; }
  • 80. Bibliography β€’ S.B. Lippman et al., C++ Primer (5th Ed.) β€’ B. Stroustrup, The C++ Programming Language (4th Ed.) β€’ StackOverflow FAQ, "How do I use arrays in C++?"