Download Presentation
## CSC 107 -

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**1. **CSC 107 -Programming for Science Lecture 28:
Multi-dimensional Arrays

**2. **Problem of the Day What do you get when you cross a mountain climber and a grape?
Nothing, you cannot cross a scalar.

**3. **Todays Goal After lecture should be familiar with multi-dimensional arrays
Creating multi-dimensional array variables
Assigning data inside these array
Using values stored in these array

**4. **Problem with Arrays Each array stores single list of values
Often work in multiple dimensions
Pictures are rarely 1 dimensional
Bridge design means evaluating stresses in multiple dimensions
Work with tables of data
Not easy to do with a vector

**5. **Multidimensional Arrays Variable holding arrays of arrays
1-d array is a vector
2-d array is a matrix
Each location holds one piece of data
Elements now accessed using row & column
All locations still depend on array variable
Each locations value independent of others
But cannot separate out rows or columns

**6. **Declaring Multidimensional Array Must declare array variable before use
Declaration includes type, name, and size in each dimension
Sizes should be literal value
Name follows same rules as any other variable
Variable is 2-dimensional array of requested type
Each location, however, holds data of that type
int neo[10][30]; // 10 rows x 30 columnsfloat trinity[200][3]; // 200 rows x 3 columnsFILE* morpheus[3][1]; // 3 rows x 1 column

**7. **Working With Arrays Starts numbering rows & columns at 0
Every row must have same number of columns
Still cannot find arrays size
No way to compute number rows or columns
Still no warning if you go beyond bounds
But only lets you use array variable or location

**8. **Initializing an Array Can set locations initial values
Must specify value for all locations
Starts with open brace
lists values for rows location in braces
ends with closing braces.double switch[2][2]={{0.0, 1.0},{2.2, 3.2}};char epoch[3][1] = {{A},{B},{C}};int dozer[1][1] = {{1}};FILE *tank[][3] = {{NULL,NULL,NULL},{NULL,NULL,NULL}};

**9. **Using An Array Each array location used like a variable
But can access only through array variable
Locations named via row & column
Smoothing out graphical data:
int i, j, picture[20][10];// ... Read in data to form picturefor (i = 1; i < 19; i++) { for (j = 1; j < 9; j++) { picture[i][j] = (picture[i-1][j] + picture[i+1][j] + picture[i][j-1] + picture[i][j+1])/4; }}

**10. **Passing an Array Can pass a location like any other value
Elements value cannot change
Function only gets single value
fabs(values[8][4]);cos(momentum[4][1]);
Can also pass entire array
Parameter must also be for an array
Parameter must include number of columns

**11. **Working With 2d Parameters int determinant(int a[][2]) { int i,j; return (a[0][0] * a[1][1])(a[1][0] * a[0][1]);}

**12. **Your Turn Get back into groups and complete the daily activity

**13. **For Next Lecture Continue on Programming Assignment #2
Study for Midterm #2 on Monday