1 EE 355 Unit 5 Multidimensional Arrays Mark Redekopp
2 MULTIDIMENSIONAL ARRAYS
3 Multidimensional Arrays • Thus far arrays can be thought of 1-dimensional (linear) sets 0 1 2 3 4 5 – only indexed with 1 value (coordinate) … 01 02 03 04 05 06 – char x[6] = {1,2,3,4,5,6}; Memory • We often want to view our data as 2-D, 3-D or higher dimensional data – Matrix data – Images (2-D) Column Index – Index w/ 2 coordinates 0 0 0 0 64 64 64 0 (row,col) 128 192 192 0 192 192 128 64 Individual Row Index Pixels Image taken from the photo "Robin Jeffers at Ton House" (1927) by Edward Weston
4 Multidimension Array Declaration • 2D: Provide size along both dimensions (normally rows first then columns) Col. 0 Col. 1 Col. 2 – Access w/ 2 indices 5 3 1 Row 0 – Declaration: int my_matrix[2][3]; 6 4 2 Row 1 – Access elements with appropriate indices • my_matrix[0][1] evals to 3, my_matrix [1][2] evals to 2 • 3D: Access data w/ 3 indices – Declaration: char image[2][4][3]; Plane 0 – Up to human to interpret 35 3 1 Plane 0 Plane 1 meaning of dimensions 35 3 12 7 32 44 23 Plane 1 6 14 72 • Planes x Rows x Cols 35 3 44 51 6 14 49 10 59 18 88 10 81 63 • Rows x Cols x Planes or 35 3 44 16 72 61 53 84 10 81 65 40 75 18 6 14 72 91 39 21 7 Plane 2
5 Passing Multi-Dimensional Arrays void doit( int my_array[][4][3] ) { • Formal Parameter: Must give my_array[1][3][2] = 5; } dimensions of all but first int main(int argc, char *argv[]) dimension { int data[2][4][3]; • Actual Parameter: Still just doit( data ); ... the array name (i.e. starting return 0; } address) • Why do we have to provide all 35 3 1 0 35 42 8 12 but the first dimension? 6 14 72 1 03 2 01 67 25 49 • So that the computer can 10 81 63 3 06 4 14 14 48 65 determine where element: 40 75 18 … … data[i][j][k] is actually located 74 21 7 11 18 12 42 in memory 13 08 14 12 Memory
6 Linearization of Multidimensional Arrays • Analogy: Hotel room layout => 3D 100 110 200 220 – Access location w/ 3 indices: 101 111 201 211 • Floors, Aisles, Rooms 102 112 202 212 • But they don’t give you 3 indices, they give you one 103 113 203 213 room number 2 nd Floor – Room #’s are a linearization of the 3 dimensions 104 114 204 214 1 st Floor 105 115 205 215 • Room 218 => Floor=2, Aisle 1, Room 8 106 116 206 216 • When “linear” -izing we keep proximity for only 107 117 207 217 lowest dimension 108 118 208 218 – Room 218 is next to 217 and 219 109 119 209 219 • But we lose some proximity info for higher Analogy: Hotel Rooms dimensions – Presumably room 218 is right below room 318 1 st Digit = Floor – But in the linearization 218 seems very far from 318 2 nd Digit = Aisle 3 rd Digit = Room
7 Linearization of Multidimensional Arrays • In a computer, multidimensional arrays must still be stored in memory which is addressed linearly (1-Dimensional) • C/C++ use a policy that lower dimensions are placed next to each other followed by each higher level dimension int x[2][3]; 100 00 00 00 05 x[0][0] 104 00 00 00 03 x[0][1] Col. 0 Col. 1 Col. 2 108 00 00 00 01 x[0][2] 112 00 00 00 06 x[1][0] Row 0 5 3 1 116 00 00 00 04 x[1][1] 6 4 2 Row 1 120 00 00 00 02 x[1][2] 124 d2 19 2d 81 … Memory
8 Linearization of Multidimensional Arrays • In a computer, multidimensional arrays must still be stored in memory which is addressed linearly (1-Dimensional) • C/C++ use a policy that lower dimensions are placed next to each other followed by each higher level dimension char y[2][4][3]; 0 35 1 03 35 3 1 2 01 6 14 72 3 06 4 14 42 8 12 10 81 63 … … 67 25 49 11 18 40 75 18 12 42 14 48 65 13 08 14 12 74 21 7 Memory
9 Linearization of Multidimensional Arrays • We could re-organize the memory layout (i.e. linearization) while still keeping the same view of the data by changing the order of the dimensions char y[4][3][2]; 0 35 1 42 35 3 1 2 03 6 14 72 3 08 4 01 42 8 12 10 81 63 5 12 67 25 49 6 06 40 75 18 7 67 14 48 65 8 14 … … 74 21 7 Memory
10 Linearization of Multidimensional Arrays • Formula for location of item at row i, column j in an array with NUMR rows and NUMC columns: 100 00 00 00 05 x[0][0] 104 00 00 00 03 x[0][1] int x[2][3]; // NUMR=2, NUMC = 3; Declaration: 108 00 00 00 01 x[0][2] 112 00 00 00 06 x[1][0] Col. 0 Col. 1 Col. 2 116 00 00 00 04 x[1][1] 5 3 1 Row 0 120 00 00 00 02 x[1][2] 124 00 00 00 08 x[2][0] 6 4 2 Row 1 128 00 00 00 09 x[2][1] 132 00 00 00 07 x[2][2] 8 9 7 Row 2 00 00 00 0f x[3][0] 136 15 3 6 Row 3 00 00 00 03 x[3][1] 140 00 00 00 06 x[3][2] 144 … x[i][j]: Access: Memory
11 Linearization of Multidimensional Arrays • Formula for location of item at plane p, row i, column j in array with NUMP planes, NUMR rows, and NUMC columns int x[2][4][3]; // NUMP=2, NUMR=4, NUMC=3 Declaration: 100 35 35 3 1 104 03 x[p][i][j]: Access: 6 14 72 108 01 116 06 10 81 63 120 14 … … 40 75 18 Memory 42 8 12 67 25 49 14 48 65 74 21 7
12 Revisited: Passing Multi-Dimensional Arrays void doit( int my_array[][4][3] ) { • Must give dimensions of all my_array[1][3][2] = 5; } but first dimension int main(int argc, char *argv[]) { • This is so that when you use int data[2][4][3]; ‘ myarray[p][i][j ]’ the computer doit(data); ... and determine where in the return 0; } linear addresses that individual index is located in 35 3 1 100 35 the array 42 8 12 6 14 72 104 03 108 01 – [p][i][j] = start address + 67 25 49 10 81 63 112 06 (p*NUMR*NUMC + 116 14 14 48 65 40 75 18 i*NUMC + j)*sizeof(int) … … 74 21 7 – [1][3][2] in an array of nx4x3 144 18 148 42 becomes: 1*(4*3) + 3(3) + 2 = 23 152 08 ints = 23*4 = 92 bytes into the 156 12 Memory array
13 Using 2- and 3-D arrays to create and process images IMAGE PROCESSING
14 Practice: Drawing • Download the BMP library code: – In your examples directory on your VM create a new subdirectory: gradient • $ rm – rf gradient • $ mkdir gradient • $ cd gradient • $ wget http://ee.usc.edu/~redekopp/ee355/code/gradient.tar • $ tar xvf gradient.tar – Code to read (open) and write (save) .BMP files is provided in bmplib.h and bmplib.cpp – Look at bmplib.h for the prototype of the functions you can use in your main() program in demo.cpp • demo.cpp contains a main function and two global arrays: image[255][255] and rgbimage[255][255][3] – bwimage is a 256x256 image with grayscale pixels (0=black, 255=white)
15 Multi-File Programs • We need a way to split our code into many separate files so that we can partition our code – We often are given code libraries from other developers or companies – It can also help to put groups of related functions into a file • bmplib.h has prototypes for functions to read, write, and show .BMP files as well as constant declarations • bmplib.cpp has the implementation of each function • gradient.cpp has the main application code – It #include's the .h file so as to have prototypes and constants available Key Idea : The .h file tells you what library functions are available; The .cpp file tells you how it does it
16 Multi-file Compilation • Three techniques to compile multiple files into a single application – Use 'make' with a 'Makefile' script • We will provide you a 'Makefile' whenever possible and it contains directions for how to compile all the files into a single program • To use it just type 'make' at the command prompt – Compile all the .cpp files together like: $ g++ -g -o gradient gradient.cpp bmplib.cpp • Note: NEVER compile .h files
17 Multi-file Compilation • Three techniques to compile multiple files into a single application – Compile each .cpp files separately into an "object file" (w/ the – c option) and then link them altogether into one program: $ g++ -c bmplib.cpp – o bmplib.o $ g++ -c gradient.cpp – o gradient.o $ g++ -g – o gradient gradient.o bmplib.o – The first two commands produce .o (object) files which are non-executable files of 1's and 0's representing the code – The last command produces an executable program by putting all the .o files together – Don't do this approach in 103, but it is approach 'Makefiles' use and the way most real programs are compiled
18 Practice: Drawing • Draw an X on the image – Try to do it with only a single loop, not two in sequence • Draw a single period of a sine wave – Hint: enumerate each column, x, with a loop and figure out the appropriate row (y-coordinate)
19 Practice: Drawing • Modify gradient.cpp to draw a gradient down the rows (top row = black through last row = white with shades of gray in between • Modify gradient.cpp to draw a diagonal gradient with black in the upper left through white down the diagonal and then back to black in the lower right
Recommend
More recommend