Download presentation
Presentation is loading. Please wait.
Published byAbraham Craig Modified over 9 years ago
1
ECE 103 Engineering Programming Chapter 23 Multi-Dimensional Arrays Herbert G. Mayer, PSU CS Status 6/24/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE
2
1 Arrays (Multi-dimensional) A multi-dimensional array can be thought of as an array whose elements are other arrays 2-D arrays are useful for storing a table of values (e.g., a matrix) 1 st dimension is “rows”, 2 nd dimension is “columns” 3-D arrays are often used to hold 3-D coordinates, such as in graphics applications Higher dimensions are useful for tracking multiple variables in an N-dimensional space
3
2 Declaration: datatype array_name[dim1][dim2][dim3]…[dimN]; The total number of elements allocated is equal to dim1 dim2 dim3 … dimN. Example: /* 12 elements, indices 0:11 */ float x[12]; /* 12*4=48 elements, indices 0:11, 0:3 */ float y[12][4]; /* 12*4*5=240 elements, 0:11, 0:3, 0:4 */ float z[12][4][5]; // # elements? How many bytes?
4
3 Example: /* NUM_STUDENTS*NUM_HW elements */ /* indices 0:(NUM_STUDENTS-1), 0:(NUM_HW-1)*/ int HW_grades[NUM_STUDENTS][NUM_HW]; A multi-dimensional array can be declared and initialized at the same time: Example: int nums[2][3] = { {2, 4, 6},{-9,-7,-5} }; int A[3][3] = {{1, 0, 0}, {1, 1, 0}, {0, 0, 1} }; float data[5][8] = {{0.0}}; /* All zero */
5
4 Memory for an array is allocated as a consecutive, linear (1-D) block of cells In C, array elements are stored in “row major order” Internally, a mapping equation translates array indices to a linear offset 1-D: A[size] → A[i], offset = i 2-D: M[size1][size2] → M[i][j], offset = i*size2 + j
6
5 Example: 1-D integer array (32-bit) A[0] A[1] A[2] int A[3]; 0 1 2 A[2] : 1-D offset is 2 If the array’s base address is Base, then A[2] is stored at address Base + 2*(4 bytes) = Base + 8
7
6 Example: 2-D integer array (32-bit) M[0][0]M[0][1] M[1][0]M[1][1] M[2][0]M[2][1] int M[3][2]; 0 1 2 offset = row# * size2 + col# A[2][1] : 1-D offset is 2*2+1=5 If the array’s base address is Base, then A[2][1] is stored at address Base + 5*(4 bytes) = Base + 20 01 M[0][0] M[0][1] M[1][0] M[1][1] M[2][0] M[2][1] 0 1 2 3 4 5
8
7 Example: Calculate the matrix sum C = A+B
9
8 FOR each row x in C FOR each column y in C C xy = A xy + B xy END FOR 2×32×3 2×32×3 2×32×3
10
9 /* This code fragment adds two matrices */ #define NR 2 #define NC 3 int A[NR][NC] = { { 1, 3, 2 }, { 4, -2, 4} }; int B[NR][NC] = { { -3, 5, 2 }, { 0, 7, 6} }; int C[NR][NC]; // leave uninitialized int x, y; for( x = 0; x < NR; x++ ) { /* Add matrices */ for( y = 0; y < NC; y++ ) { C[x][y] = A[x][y] + B[x][y]; } //end for for( x = 0; x < NR; x++ ) { /* Display result matrix */ for( y = 0; y < NC; y++ ) { printf( "%2d ", C[x][y] ); } //end for printf( "\n” ); } //end for
11
10 Example: Calculate the matrix product C = AB.
12
11 Given: A is size m 1 ×n 1, B is size m 2 ×n 2, and n 1 =m 2. Let C = AB be size m 1 ×n 2 Element C xy stored at row x and column y in C is the dot product of row A x and column B y. Procedure: FOR each row A x FOR each column B y C xy = A x ·B y END FOR
13
12 2×32×3 3×23×2 2×22×2 1 st row of A, 1 st column of B 1 st row of A, 2 nd column of B 2 nd row of A, 1 st column of B 2 nd row of A, 2 nd column of B
14
13 /* This code fragment multiplies two matrices */ #define NR1 2 #define NC1 3 #define NR2 3 #define NC2 2 int A[NR1][NC1] = { {1,3,2},{4,-2,4}}; int B[NR2][NC2] = { {4,-1},{3,2},{5,3}}; int C[NR1][NC2]; int m1=NR1, n1=NC1, m2=NR2, n2=NC2; int x, y, k; for (x = 0; x < m1; x++) /* row */ for (y = 0; y < n2; y++) /* column */ { C[x][y] = 0; /* Initialize summation */ for (k = 0; k < n1; k++) /* Dot product */ C[x][y] += (A[x][k] * B[k][y]); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.