Lecture 02.1 Array
Lecture 02.1 Array
Array [2-Dimensional]
III. Access
2. String
I. Definition & Structure
{
0 1 2 3 4
0 minu[0][0] minu[0][1] minu[0][2] minu[0][3] minu[0][4]
1 minu[1][0] minu[1][1] minu[1][2] minu[1][3] minu[1][4]
minu 2 minu[2][0] minu[2][1] minu[2][2] minu[2][3] minu[2][4]
The way to reference the 2nd element vertically and 4th horizontally or the (2 × 4)
8th element in an expression would be: minu [1][3];
Generally, for two-dimensional array, 1st dimension is considered as row and the
2nd dimension is considered as column. Here, we have 3 rows and 5 columns.
Initialization
Assigning values at the time of declaring a two-dimensional array can be any one of
the following ways:
int minu[3][5] = {1,2,3,4,5,2,4,6,8,10,3,6,9,12,15};
int minu[3][5] = {{1,2,3,4,5},{2,4,6,8,10},{3,6,9,12,15}};
int minu[3][5] = {
{1,2,3,4,5},
{2,4,6,8,10},
{3,6,9,12,15}
};
The internal braces are unnecessary but helps to distinguish the rows from the
columns.
Take care to include the semicolon at the end of the curly brace which closes the
assignment.
If there are not enough elements in the curly braces to account for every single
element in an array, the remaining elements will be filled out with garbage/zeros.
Static and global variables are always guaranteed to be initialized to zero anyway,
whereas auto or local variables are guaranteed to be garbage.
Access
Nested loop is used to take input and Consider the following example (the dark area at the
give output. end consists the input and the output of this program;
the yellow colored text represents input given by the
The input is taken in row (1st user and black colored text represents output):
dimension) major. i.e. all the values of
row 0 is scanned first, then the values
1 // input & output of a 2D array
of row 1, and values of row 2. For each 2 void main (void)
row, value at column 0 is scanned first, 3 {
then the value at column 1, and value 4 int i, j, a[3][3], b[3]
at column 2. 5 [3]={1,3,5,7,9,2,4,6,8};
6 for(i=0;i<3;i++)
7 for(j=0;j<3;j++)
In output, array a is used in row major. 8 cin>>a[i][j];
But array b is used in column (2nd 9 for(i=0;i<3;i++)
dimension) major. i.e. all the values of 1 for(j=0;j<3;j++)
0 cout<<a[i][j] + b[j][i];
column 0 is added first, then the values 1 }
of column 1, and values of column 2. 1
For each column, value at row 0 is 2 4 6 8 1 3 5 7 9
added first, then the value at row 1, ? ? ? ? ? ? ? ? ?
and value at row 2.
Find the summation of the elements of a 2D array.
Process:
1. Start from the row with row-value r=0.
2. Start from the column with column-value c= 0.
3. Add the value of A[r][c] with B[r][c] and store the summation in
S[r][c].
4. Increase the value of c by 1.
5. Repeat (3) and (4) for all the columns.
6. Increase the value of r by 1.
7. Repeat (2), (3), (4), (5) and (6) for all the rows.
Output: Print the array S.
Find the summation of two 2D arrays and store the
result in another 2D array.
0 1 2 0 1 2 0 1 2
0 12 5 8 0 2 8 18 0 14 13 26
1 6 7 4 + 1 16 14 5 = 1 22 21 9
2 18 9 2 2 8 19 12 2 26 28 14
Memory Access
Two-dimensional arrays are just an abstraction for programmers, since we can obtain the
same results with a simple array just by putting a factor between its indices: int minu
[3][5]; is equivalent to (3 * 5 = 15); int minu [15];
2D Array 1D array
int minu [3][5], H=3, W=5, n, m, i=0; int minu [3 * 5], H=3, W=5, n, m, i=0;
void main (void){ 0 1 2 3 4 void main (void){
0 1 2 3 4 5
for (n=0; n<H; n++) for (n=0; n<H; n++)
1 6 7 8 9 10
for (m=0; m<W; m++) 2 11 12 13 14 15 for (m=0; m<W; m++)
minu [n][m]= ++i;
minu [ W * n + m ] = ++i;
}
}
&mimo[2] gives the memory location of the 3rd element of the array mimo.
If the element is more than a byte, it gives the starting byte of the element.
0 1 2 3 4
mimo
567 569 571 573 575 577 579 581 583 585 587
568 570 572 574 576 578 580 582 584 586
mimo[2] will give us 4 bytes (int) of information starting from 575 to 579.
The name of an array always refer to the starting location of the array. i.e. the first
element of the array. So, mimo = &mimo[0].
Memory Access (1D Array)
3
mimo[1][1] will give us 4 bytes (int) of information
starting from 583 to 587.
0 1 2 3 4
mimo
567 569 571 573 575 577 579 581 583 585 587
568 570 572 574 576 578 580 582 584 586
A 2D array is also referred as an array of arrays. i.e. an array of which each element
is another array.
Books
“Schaum's Outline of Data Structures with C++”. By John R. Hubbard
“Data Structures and Program Design”, Robert L. Kruse, 3rd Edition, 1996.
“Data Structures and Algorithm Analysis”, Edition 3.2 (C++ Version), Clifford A.
Shaffer, Virginia Tech, Blacksburg, VA 24061 January 2, 2012
“C++ Data Structures”, Nell Dale and David Teague, Jones and Bartlett Publishers,
2001.
1. https://en.wikipedia.org/wiki/Array_data_structure
2. https://www.programiz.com/cpp-programming/strings
3. https://cal-linux.com/tutorials/strings.html
4. http://www.cplusplus.com/reference/cstring/
5. http://www.cplusplus.com/reference/string/string/
6. https://cal-linux.com/tutorials/strings.html