0% found this document useful (0 votes)
3 views

Lecture 02.1 Array

The document is a lecture outline for a Data Structures course focusing on two-dimensional arrays. It covers definitions, structure, initialization, access methods, and memory access for 2D arrays, along with examples and references. The lecture is conducted by Dr. Afroza Nahar at the American International University-Bangladesh for the Spring 2023-2024 semester.

Uploaded by

arbin.rahin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lecture 02.1 Array

The document is a lecture outline for a Data Structures course focusing on two-dimensional arrays. It covers definitions, structure, initialization, access methods, and memory access for 2D arrays, along with examples and references. The lecture is conducted by Dr. Afroza Nahar at the American International University-Bangladesh for the Spring 2023-2024 semester.

Uploaded by

arbin.rahin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

AMERICAN INTERNATIONAL UNIVERSITY-BANGLADESH

CSC 2106: : DATA STRUCTURE (THEORY)


Lecture: # 2 Week: # 1 Semester: Spring 2023-2024

Array [2-Dimensional]

Instructor: Dr. Afroza Nahar, Professor


Department of Computer Science, Faculty of Science & Technology.
[email protected]
Lecture Outline
1. Array [2-Dimensional]
I. Definition, Structure & Declaration
II. Initialization

III. Access

IV. Memory Access

2. String
I. Definition & Structure

II. Declaration & Initialization

III. Access, Input, Output

IV. String Handling Functions


Array [2-
Dimensional]
Definition, Structure & Declaration

 Two-dimensional arrays can be described as "arrays of arrays". For example, a


2D array can be imagined as a Two-dimensional table made of elements of same
uniform data type.

{
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]

 minu represents a Two-dimensional array of 3 per 5 elements of type int. The


way to declare this array in C++ would be: int minu [3][5];

 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.

Input (Declarations and Initializations):


int arr[3][3], int sum = 0;
0 1 2 Process:
1. Start from the row with row-value r=0.
0 12 5 8 2. Start from the column with column-
value c= 0.
3. Add the value of sum with the element
1 6 7 4 in arr[r][c] index.
4. Store the summation of the add operation
in (3) in sum.
2 18 9 2 5. Increase the value of c by 1.
6. Repeat (3), (4) and (5) for all the
columns.
7. Increase the value of r by 1.
8. Repeat (2), (3), (4), (5), (6), (7) for all the
rows.
Output: Print the value of sum
Find the summation of two 2D arrays and store the
result in another 2D array.

Input (Declarations and Initializations): int A[3][3], int B[3][3],


int S[3][3];

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;
}
}

As memory is flat, in both codes the values are actually 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14


stored sequentially in the memory (just like the 1D array). 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
The access for the two-dimensional array in that case is just
as the indexing of the array,
Height Width n m Width * n + m
[(Total_column) * (row_index) + (column_index)] 3 5 0
21 2 0
341 2
5
3896
14
13
11
10
12 0
7
1
4
Memory Access (1D)
 Memory of each element of an array can be accessed using the & operator.

 &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.

 Let us consider the starting address of int mimo[5] is 567.

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 the memory location 575.

 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)

&array[index]=start_location_array + index * size_of_data


Þ &mimo[ 2 ] = mimo (or &mimo[0])+ 2 * sizeof(int)

Þ &mimo[ 2 ] = 567 + 2 * 4 = 575


Memory Access (2D)
 Consider a 2D array mimo[R][C] each element addressed by 567 571 575 579
&mimo[i][j], where R=total element in 1st dimension, 0 1 2
C=total element in 2nd dimension, 0 i < R, 0  j < C. 0
1 583 587
 Let int mimo[4][3]; Here, mimo or &mimo[0][0] 579
gives us the starting memory location 567. 2

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

&array[i][j]=start_location + (i * (C * size_of_data)) + (j * size_of_data)

Þ &mimo[1][1] = mimo + (1 * (3 * sizeof(int))) + (1 * sizeof(int))

Þ &mimo[1][1] = 567 + (1 * 3 * 4) + (1 * 4) = 583


Memory Access
 There is a general way to access the memory location of a 2-dimensional array.

 For an array int mimo[R][C]; and 0i<R; 0j<C.


 mimo[i] = &mimo[i][0] represents the starting address of ith row.
 mimo[i] skips i number of rows each with C number of elements from the
start_location of the array.
 So, mimo[i] = start_location + (i * C elements), where C
elements are counted in bytes based on the size_of_data, here int.
 So, mimo[i] = start_location + (i * (C * size_of_data)).
 So, mimo[i] = mimo(or &mimo[0][0]) + (i * (C
*sizeof(int))).

 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, algorithms and performance”, D. Wood, Addison-Wesley, 1993

 “Advanced Data Structures”, Peter Brass, Cambridge University Press, 2008

 “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.

 “Data Structures and Algorithms with Object-Oriented Design Patterns in C++”,


Bruno R. Preiss,
References

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

You might also like