Lecture-6 Data Structures CSE242
Lecture-6 Data Structures CSE242
Arrays
Data Structures
c t ure
Le
6 By
Dr. Sudeshna Chakraborty
1
Department of CSE, School of Engineering and Technology
Lecture -6
Contents
Arrays
• Array – Definition
• Multidimensional Array
• Multidimensional Array elements
• Multidimensional Array storing and accessing
2
Department of CSE, School of Engineering and Technology
Lecture -6
Arra
Array y
Arrays
• Collection of similar types of data items/elements
Multi
• Contiguous memory locations One Dimensiona
Dimensional l
• Linear or one dimensional
• Multi dimensional
3
Department of CSE, School of Engineering and Technology
Lecture -6
Multidimensional Array
Arrays
• Collection of Linear arrays – 2D Arrays
• Collection of 2D - Arrays – 3D Arrays
4
Department of CSE, School of Engineering and Technology
Lecture -6
5
Department of CSE, School of Engineering and Technology
0 1 2
Lecture -6
3 4 5
Multidimensional Array Elements 6
Array
7
s
8
Method 1: 9 10 11
int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 12 13 14
22, 23};
15 16 17
Better Method: 18 19 20
int x[2][3][4] =
{ 21 22 23
{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, 0 1 2
{20,21,22,23} } 3 412 513 14
}; 6 715 816 17
9 1018 1119 20
21 22 23
6
Department of CSE, School of Engineering and Technology
0 1 2
Lecture -6
3 4 5
Multidimensional Array Elements
int x[2][3][4] =
6 7
12 Array
8
13 s 14
9 10 11
{ 15 16 17
{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} } 18 19 20
};
21 22 23
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for (int k = 0; k < 4; ++k)
{
printf("Element at x[%d][%d[%d] = %d”, i, j, k, x[i][j][k]
;
}
}
}
7
Department of CSE, School of Engineering and Technology
Lecture -6
9
Department of CSE, School of Engineering and Technology
Lecture -6
10
Department of CSE, School of Engineering and Technology
Lecture -6
Assignment
Arrays
Write a C program to find sum of specific elements of given 2D Array of nxn where n is odd:
1. Sum of both diagonals such that the central element is considered only once.
2. Sum of middle row and middle column elements such that the central element is considered only once.
3. Sum of corner elements.
11
Department of CSE, School of Engineering and Technology