270 likes | 436 Views
CSC 107 – Programming For Science. Lecture 25: Multi-Dimensional Arrays. 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.
E N D
CSC 107 – Programming For Science Lecture 25: Multi-Dimensional Arrays
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
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
Multi-dimensional Arrays • Multiple dimensions refine how data viewed • 1-d array is a row of data
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
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
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
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[256][32];float armada[MAX_SHIPS][MAX_DECKS];double taxesOwed[MAX_EARN][10];char names[17][MAX_NAME_SIZE];long number[1][1];
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
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[2][2] ={{0,1},{2.2,3.2}};char epoch[3][1] = {{‘A’},{‘B’},{‘C’}};intdozer[1][1] = {{1}};boolraygun[3][3] = {{true,false,true}};
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[20][10];for (inti = 0; i < 20; i++) { for (intj = 0; j < 10; j++) {picture[i][j] = (i * 10) + j; }}
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]);
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]);
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]);
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]);
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]);
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]);
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]);
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]);
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]);
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]);
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]);
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
Working With 2d Parameters intdeterminant(int a[][2]) { return (a[0][0]*a[1][1])–(a[1][0]*a[0][1]); }\intmain(void) {int bookExample[2][2]; bookExample[0][0] = 1; bookExample[0][1] = 3; bookExample[1][0] = -1; bookExample[1][1] = 5;intdet = determinant(bookExample);cout << det;}
Your Turn • Get into your groups and try this assignment
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