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

Lect2 Multidimarray Part3

Two-dimensional arrays are arrays of one-dimensional arrays, with each element accessed using two subscripts. Elements are stored in row-major order with the elements of each row stored sequentially. Pointers can also be used to access elements of two-dimensional arrays by treating the 2D array as a contiguous 1D block of memory.

Uploaded by

A7a Wtf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Lect2 Multidimarray Part3

Two-dimensional arrays are arrays of one-dimensional arrays, with each element accessed using two subscripts. Elements are stored in row-major order with the elements of each row stored sequentially. Pointers can also be used to access elements of two-dimensional arrays by treating the 2D array as a contiguous 1D block of memory.

Uploaded by

A7a Wtf
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Arrays

2-dimensional arrays

https://bohr.wlu.ca/cp264/notes/cp264_lecture07_array.ppt
3. Two-dimensional Arrays
A two-dimensional array is specified using two subscripts where one
subscript denotes row and the other denotes column.

C looks at a two-dimensional array as an array of one-dimensional


arrays. A two-dimensional array is declared as
data_type array_name[row_size][column_size];
First/row Dimension

Second/column Dimension

https://bohr.wlu.ca/cp264/notes/cp264_lecture07_array.ppt
Two-dimensional Arrays????
Therefore, a two dimensional m×n array is an array that contains
m×n data elements and each element is accessed using two
subscripts, i and j, where 0<= i<m and 0<= j<n
Example:
int marks[3][5];

Col 0 Col 1 Col2 Col 3 Col 4


Rows/Columns
Row 0 marks[0][0] marks[0][1] marks[0][2] marks[0][3] marks[0][4]
Row 1 marks[1][0] marks[1][1] marks[1][2] marks[1][3] marks[1][4]
Row 2 marks[2][0] marks[2][1] marks[2][2] marks[2][3] marks[2][4]

Two Dimensional Array

https://bohr.wlu.ca/cp264/notes/cp264_lecture07_array.ppt
FIGURE 8-35 Array Of Arrays

Computer Science: A Structured Programming Approach Using C 4


https://www.unf.edu/~wkloster/2220/ppts/COP2220/Chap8ppt/Chap-08-
Memory Representation of a 2D Array
• The 2-D array is stored by row-major order in memory.

• In the row-major order the elements of the first row are stored
before the elements of the second and third rows. That is, the
elements of the array are stored row by row where n elements of
the first row will occupy the first nth locations.

(0, 0) (0, 1) (0, 2) (0, 3) (1, 0) (1, 1) (1, 2) (1, 3) (2, 0) (2, 1) (2, 2) (2, 3)

https://bohr.wlu.ca/cp264/notes/cp264_lecture07_array.ppt
FIGURE 8-36 Memory Layout

Computer Science: A Structured Programming Approach Using C 6


https://www.unf.edu/~wkloster/2220/ppts/COP2220/Chap8ppt/Chap-08-
Row-major vs. column-major storage

Figure 11.6 Memory layout of arrays


11.7
https://elearning.kau.edu.sa/GetFile.aspx?id=64681&Lng=AR&fn=ch-11.ppt
Pointers and 2D Arrays
Individual elements of the array mat can be accessed using either:
mat[i][j] or *(*(mat + i) + j) or*(mat[i]+j);
Pointer to a one-dimensional array can be declared as:
int arr[]={1,2,3,4,5};
int *parr;
parr=arr;
Similarly, pointer to a two-dimensional array can be declared as:
int arr[2][2]={{1,2},{3,4}};
int (*parr)[2];
parr=arr;
Pointers for 2-D arrays
int a[2][3];
int *p; p  &a[0][0] p+1  &a[0][1] p+2  &a[0][2]
p = &a[0][0];
*p  a[0][0] *(p+1) a[0][1] *(p+2)  a[0][2]

p+3  &a[1][0] p+4  &a[1][1] p+5  &a[1][2]

*(p+3)  a[1][0] *(p+4)  a[1][1] *(p+5)  a[1][2]


Pointers for 2-D arrays
int a[row][col];

int *p = &a[0][0];

(p+i*col+j)  &a[i][j];

*(p+i*col+j)  a[i][j];
Example: transpose of a matrix ?????
Pass by matrix name
void transpose(int n, int m[][n]) { // need declare n first
int *p = m, i = 0, j = 0, temp;

for (i = 0; i < n; i++) {


for (j = i + 1; j < n; j++) {
temp = m[i][j];
m[i][j] = m[j][i];
m[j][i] = temp;
}
}
}
// call
int mat[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}:
transpose(3, mat);

© Oxford University Press 2014. All rights reserved.


Example: transpose of a matrix
Pass by single pointer
void transpose(int *m, int n) {
int *p = m, i = 0, j = 0, temp;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
temp = *(p + i * n + j);
*(p + i * n + j) = *(p + j * n + i);
*(p + j * n + i) = temp;
//swap(p + i * n + j, p + j * n + i);
}
}
}
// call
int mat[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int *p = &mat[0][0];
transpose(p, 3);
Multi-dimensional Arrays ????
• A multi-dimensional array is an array of arrays.
• Like we have one index in a single dimensional array, two indices in a
two-dimensional array, in the same way we have n indices in a n-
dimensional array or multi-dimensional array.
• Conversely, an n dimensional array is specified using n indices.
• An n dimensional m1 x m2 x m3 x ….. mn array is a collection of
m1×m2×m3× ….. ×mn elements.
• In a multi-dimensional array, a particular element is specified by using
n subscripts as A[I1][I2][I3]…[In], where
I1<M1 I2<M2 I3 <M3 ……… In <Mn
FIGURE 8-40 A Three-dimensional Array (3 x 5 x 4)

Computer Science: A Structured Programming Approach Using C 14


https://www.unf.edu/~wkloster/2220/ppts/COP2220/Chap8ppt/Chap-08-
FIGURE 8-41 C View of Three-dimensional Array

Computer Science: A Structured Programming Approach Using C 15


https://www.unf.edu/~wkloster/2220/ppts/COP2220/Chap8ppt/Chap-08-
Pointers and Three-dimensional Arrays????

A pointer to a three-dimensional array can be declared as:


int arr[2][2][2]={1,2,3,4,5,6,7,8};
int (*parr)[2][2];
parr=arr;

We can access an element of a three-dimensional array by writing:

arr[i][j][k]= *(*(*(arr+i)+j)+k)

You might also like