 Download Download Presentation Lecture 25: Multi-Dimensional Arrays

# Lecture 25: Multi-Dimensional Arrays

Download Presentation ## Lecture 25: Multi-Dimensional Arrays

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. CSC 107 – Programming For Science Lecture 25: Multi-Dimensional Arrays

2. Today’s Goal • Get familiar withmulti-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 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 used in 1-d array to access 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 • Like all variables, must declare before use • Type, name, & size of each dimension needed • Each of the array's entries hold value of that type • Size must be integer since ½ a value hard to use intprettyPicture;float armada[MAX_SHIPS][MAX_DECKS];double taxesOwed[MAX_EARN];char names[MAX_NAME_SIZE];long number;

9. Working With Arrays • 0is 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 cannot find size of array on any dimension • Even finding number of rows or columns impossible • 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 • Declare array variable and set initial values • All entries must have initial value specified • Starts with open brace… • … open brace & values for row’s entries… • …finally add a closing brace to end row… • …closing brace to end the initializationdouble switch ={{0,1},{2.2,3.2}};char epoch = {{‘A’},{‘B’},{‘C’}};intdozer = {{1}};boolraygun = {{true,false,true}};

11. Using An Array • Each entry used like variable as normal • But only can entry via array variable • Use brackets for each dimension to identify the entry • Multidimensional arrays ♥forloops, too intpicture;for (inti = 0; i < 20; i++) { for (intj = 0; j < 10; j++) {picture[i][j] = (i * 10) + j; }}

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

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

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

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

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

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

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

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

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

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

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

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);cos(momentum); • 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 intdeterminant(int a[]) { return (a*a)–(a*a); }\intmain(void) {int bookExample; bookExample = 1; bookExample = 3; bookExample = -1; bookExample = 5;intdet = determinant(bookExample);cout << det;}

25. Your Turn • Get into your groups and try this assignment

26. For Next Lecture • Arrays & arrows discussed in Sections 12.1 – 12.5 • We used arrows in a trace; but how do they work? • * not used enough; where else can I use it in program? • Can C++ make it even easier to shoot ourselves in foot? • Angel also has Weekly Assignment #9 due Tues. • Programming Assignment #2 due in one week