Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 107 – Programming For Science. Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional.

Similar presentations


Presentation on theme: "CSC 107 – Programming For Science. Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional."— Presentation transcript:

1 CSC 107 – Programming For Science

2 Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional array entry assignments  Using values stored in the entries in these array

3 Problem with Variables  Normal variable has single value at any time  Can do better & hold list of values in an array  Only 1 value per entry, however, & size is fixed  But this is very limiting in most situations  Bar codes are boring pictures  Must evaluate bridge stresses in multiple dimensions  Cannot do this  Cannot do this with what we have so far

4 Multi-dimensional Arrays  Multiple dimensions refine how data viewed  1-d array is a row of data

5 Multi-dimensional Arrays  Multiple dimensions refine how data viewed  1-d array is a row of data  Create an entire table of entries with 2-d array

6 Multi-dimensional Arrays  Multiple dimensions refine how data viewed  1-d array is a row of data  Create an entire table of entries with 2-d array  3-d array creates box of entries

7 Multi-dimensional Arrays  Each entry still holds one piece of data  Column  Column used in 1-d array to access column row column  Access entries in 2-d array with row & column  17 indices needed for entry in 17-dimension array  Using an entry still depends on array variable  Values are independent of all other entries  Its all about the values: only work with single entry

8 Declaring Arrays Variables

9 Working With Arrays  0  0 is start of columns, rows, (& everything else)  Within table, all rows have same number of columns  Identical rows & columns in 3-d array on z-dimension  Still  Still cannot find size of array on any dimension  Even finding number of rows or columns impossible  Still  Still no warning exceeding bounds of array  But restricts code to use entry or array variable  Entire row or column cannot be used in any way

10 Initializing an Array

11 Using An Array

12 Multi-Dim. Array Example float bridgeStress[100][100]; int times[100]; bridgeStress[0,4] = 3.5; times[45] = 2; bridgeStress(23)(12) = pow(12, 2); times[12] = times[45] * 4; bridgeStress[2] += times[12]; times[99] = bridgeStress[0][34] + 34; bridgeStress[3][12] = times[-2]; bridgeStress[2][0] = 0; bridgeStress[2][0] += 45; bridgeStress[3][2] = pow(2,bridgeStress[2][0]);

13 Multi-Dim. Array Example float bridgeStress[100][100]; int times[100]; bridgeStress[0,4] = 3.5; times[45] = 2; bridgeStress(23)(12) = pow(12, 2); times[12] = times[45] * 4; bridgeStress[2] += times[12]; times[99] = bridgeStress[0][34] + 34; bridgeStress[3][12] = times[-2]; bridgeStress[2][0] = 0; bridgeStress[2][0] += 45; bridgeStress[3][2] = pow(2,bridgeStress[2][0]);

14 Multi-Dim. Array Example float bridgeStress[100][100]; int times[100]; bridgeStress[0,4] = 3.5; times[45] = 2; bridgeStress(23)(12) = pow(12, 2); times[12] = times[45] * 4; bridgeStress[2] += times[12]; times[99] = bridgeStress[0][34] + 34; bridgeStress[3][12] = times[-2]; bridgeStress[2][0] = 0; bridgeStress[2][0] += 45; bridgeStress[3][2] = pow(2,bridgeStress[2][0]);

15 Multi-Dim. Array Example float bridgeStress[100][100]; int times[100]; bridgeStress[0,4] = 3.5; times[45] = 2; bridgeStress(23)(12) = pow(12, 2); times[12] = times[45] * 4; bridgeStress[2] += times[12]; times[99] = bridgeStress[0][34] + 34; bridgeStress[3][12] = times[-2]; bridgeStress[2][0] = 0; bridgeStress[2][0] += 45; bridgeStress[3][2] = pow(2,bridgeStress[2][0]);

16 Multi-Dim. Array Example float bridgeStress[100][100]; int times[100]; bridgeStress[0,4] = 3.5; times[45] = 2; bridgeStress(23)(12) = pow(12, 2); times[12] = times[45] * 4; bridgeStress[2] += times[12]; times[99] = bridgeStress[0][34] + 34; bridgeStress[3][12] = times[-2]; bridgeStress[2][0] = 0; bridgeStress[2][0] += 45; bridgeStress[3][2] = pow(2,bridgeStress[2][0]);

17 Multi-Dim. Array Example float bridgeStress[100][100]; int times[100]; bridgeStress[0,4] = 3.5; times[45] = 2; bridgeStress(23)(12) = pow(12, 2); times[12] = times[45] * 4; bridgeStress[2] += times[12]; times[99] = bridgeStress[0][34] + 34; bridgeStress[3][12] = times[-2]; bridgeStress[2][0] = 0; bridgeStress[2][0] += 45; bridgeStress[3][2] = pow(2,bridgeStress[2][0]);

18 Multi-Dim. Array Example float bridgeStress[100][100]; int times[100]; bridgeStress[0,4] = 3.5; times[45] = 2; bridgeStress(23)(12) = pow(12, 2); times[12] = times[45] * 4; bridgeStress[2] += times[12]; times[99] = bridgeStress[0][34] + 34; bridgeStress[3][12] = times[-2]; bridgeStress[2][0] = 0; bridgeStress[2][0] += 45; bridgeStress[3][2] = pow(2,bridgeStress[2][0]);

19 Multi-Dim. Array Example float bridgeStress[100][100]; int times[100]; bridgeStress[0,4] = 3.5; times[45] = 2; bridgeStress(23)(12) = pow(12, 2); times[12] = times[45] * 4; bridgeStress[2] += times[12]; times[99] = bridgeStress[0][34] + 34; bridgeStress[3][12] = times[-2]; bridgeStress[2][0] = 0; bridgeStress[2][0] += 45; bridgeStress[3][2] = pow(2,bridgeStress[2][0]);

20 Multi-Dim. Array Example float bridgeStress[100][100]; int times[100]; bridgeStress[0,4] = 3.5; times[45] = 2; bridgeStress(23)(12) = pow(12, 2); times[12] = times[45] * 4; bridgeStress[2] += times[12]; times[99] = bridgeStress[0][34] + 34; bridgeStress[3][12] = times[-2]; bridgeStress[2][0] = 0; bridgeStress[2][0] += 45; bridgeStress[3][2] = pow(2,bridgeStress[2][0]);

21 Multi-Dim. Array Example float bridgeStress[100][100]; int times[100]; bridgeStress[0,4] = 3.5; times[45] = 2; bridgeStress(23)(12) = pow(12, 2); times[12] = times[45] * 4; bridgeStress[2] += times[12]; times[99] = bridgeStress[0][34] + 34; bridgeStress[3][12] = times[-2]; bridgeStress[2][0] = 0; bridgeStress[2][0] += 45; bridgeStress[3][2] = pow(2,bridgeStress[2][0]);

22 Multi-Dim. Array Example float bridgeStress[100][100]; int times[100]; bridgeStress[0,4] = 3.5; times[45] = 2; bridgeStress(23)(12) = pow(12, 2); times[12] = times[45] * 4; bridgeStress[2] += times[12]; times[99] = bridgeStress[0][34] + 34; bridgeStress[3][12] = times[-2]; bridgeStress[2][0] = 0; bridgeStress[2][0] += 45; bridgeStress[3][2] = pow(2,bridgeStress[2][0]);

23 Passing an Array  Entry like any variable & can pass to function  Value of the entry cannot change in function  Entry's current value received by the function fabs(values[8][4]); cos(momentum[4][1]);  Pass entire array as argument for a function  As before, parameter must be array of same type  Must specify number of columns in the parameter

24 Working With 2d Parameters int determinant(int a[][2]) { return (a[0][0]*a[1][1])–(a[1][0]*a[0][1]); } \ int main(void) { int bookExample[2][2]; bookExample[0][0] = 1; bookExample[0][1] = 3; bookExample[1][0] = -1; bookExample[1][1] = 5; int det = determinant(bookExample); cout << det; }

25 Your Turn  Get into your groups and try this assignment

26 For Next Lecture  Working with files in Section 11.1 – 11.7  How can we read files and use the data it contains?  Avoid printing to the screen by saving data in files?  What do the files look like? Can anyone read them?  Angel also has Weekly Assignment #10 due Tues.  Programming Assignment #2 due tomorrow


Download ppt "CSC 107 – Programming For Science. Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional."

Similar presentations


Ads by Google