DOC PREVIEW
CSUN COMP 106 - Programming with Two- dimensional Arrays

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Programming with two-dimensional arrays May 9, 20061Programming with TwoProgramming with Two--dimensional Arraysdimensional ArraysLarry CarettoComputer Science 106Computing in Engineering and ScienceMay 9, 20062Outline• Review basics of 2D arrays– Contrast with 1D arrays– Notation and declaring array sizes• Code applications with 2D arrays• Passing 2D arrays to functions• Higher-dimensional arrays• Summary of array use3Two-dimensional Arrays• One-dimensional arrays refer to a variable that has multiple entries with a single classification• Two-dimensional arrays are used to represent data with two classifications– Example: an experiment on manufacturing productivity measures daily output of four machines with six operators4Two-dimensional Arrays• One-dimensional variable– mathematical notation xi– C++ array notation x[i]• Two-dimensional– mathematical notation xik– C++ array notation x[i][k]• One-way versus two-way classification5Two-Dimensional Array[6][4][5][4][4][4][3][4][2][4][1][4][0][4][6][3][5][3][4][3][3][3][2][3][1][3][0][3][6][2][5][2][4][2][3][2][2][2][1][2][0][2][6][1][5][1][4][1][3][1][2][1][1][1][0][1][6][0][5][0][4][0][3][0][2][0][1][0][0][0]• View two-dimensional arrays as a table with rows and columns of cells•Row index• Column6Two-dimensional Example• In the example of a manufacturing process measuring the output of four machines with six operators– Array named output depending on integer subscripts machine and operator– First subscript is for operator and second is for machineconst int maxOp = 6, maxMach = 4;int output[maxOp][maxMach];cout << output[3][2];Programming with two-dimensional arrays May 9, 200627Two-Dimensional Array Data991202265316208M tot158187143170172161Op tot284225403631M 3484839454243M 2495948525553M 1333831333934M 0Op 5Op 4Op 3Op 2Op 1Op 0Individual data plus totals for operators and machinesoutput[3][2]Array data8Two-dimensional array Codeconst int maxOp = 6, maxMach = 4int output[maxOp][maxMach]; for (int op = 0; op < maxOp; op++){for ( int mach = 0; mach <maxMach; mach++ )cout << output[op] [mach] <<“ units produced at machine “<< mach << “ with operator “<< op;} 9Other Code • How would you compute the total units produced by each machine?• How would you compute the total units produced by each operator?• How would you compute the average and standard deviation for all the units produced by the operators?10Units for Each Machine• This sum is the total output of each machine from all operators (column sum)int outMach[maxMach]; for (int mac = 0; mac < maxMach; mac++){outMach[mac] = 0;for (int op = 0; op < maxOp; op++){outMach[mac] += output[op][mac];}cout << “Total machine “ << mac <<<< “ output is “<<outMach[mac]; } 11Units for Each Operator• This sum is the total output of each operator from all machines (row sum)int outOp[maxOp]; for ( int op = 0; op < maxOp; op++ ){outOp[op] = 0;for ( int m = 0; m < maxMach; m++ ){ outOp[op] += output[op][m]; }cout << “Total operator “ << op << “ output is “ << outOp[op]; }12Comments on this Code• Note that we use one-dimensional arrays to store row (operator) and column (machine) sums• Note that order of subscripts is always [operator][machine]• Conventional, but not required, to write tables as arrays with subscript ordered as [row][column]Programming with two-dimensional arrays May 9, 2006313Simultaneous Linear Equations• Example of 3 equations (3 unknowns)3x + 7y – 3z = 82x – 4y + z = -38x + 6y – 2z = 14• How can we develop a general notation for N equations in N unknowns?– Call variables x0, x1, x2, etc.– Call right hand side b0, b1, b2, etc.– Call top row coefficients a00, a01, a02, etc.14Standard Forma00x0+ a01x1+ a02x2+...+ a0N-1xN-1+ a0NxN= b0a10x0+ a11x1+ a12x2+...+ a1N-1xN-1+ a1NxN= b1a20x0+ a21x1+ a22x2+...+ a2N-1xN-1+ a2NxN= b2... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...aN-1,0x0+ aN-1,1x1+ .. ... ... ... ...+ aN-1,NxN= bN-1aN0x0+ aN1x1+ aN2x2+... ... ... ... ..+ aNNxN= bN• Note that subscripts on a are arow,columnwhererow is equation and column is unknown15Compact Standard Form• Set of equations defined by N and data on aijand bi• Functions to solve this problem use a two-dimensional a[i][j] array and one-dimensional arrays for b[i] and x[j]1,,0,,1101−====∑∑−==NibxaNibxaNjijijNjijijKK16Example in Standard Form• Previous example 3 equations (N = 3)3x + 7y – 3z = 82x – 4y + z = -38x + 6y – 2z = 14• In standard form:– x is x0, y is x1, and z is x2–a00= 3, a01 = 7, a02 = -3, b0, = 8–a10= 2, a11 = -4, a12 = 1, b1, = -3–a20= 8, a21 = 6, a22 = -2, b2, = 1417Standard Form in C++• Equations represent unknowns as xi, the right hand sides as bi, and the left hand side coefficients as aij• In C++ we use arrays x[col] for the unknowns, b[row] for the right hand sides, and a[row][col] for the coefficients on the left hand side• Project three will use library program to solve this system of equations18Passing 2D Arrays to Functions• Execution of array code based on computing memory location from address of first array member plus subscript for particular element• For one-dimensional array we only need the address of the first element to find the location of x[i]• What about two-dimensional arrays?Programming with two-dimensional arrays May 9, 2006419Passing 2D Arrays to Functions II• Consider an array x with declared as x[totalFirst][totalSecond]• The location of x[i][j] is computed as i + j*totalSecond locations from the start of the array• We must know the second dimension to compute the location• We must pass this second dimension to the function that has a two-dimensional array as a parameter20Passing 2D Arrays to Functions III• Global constant: const int maxSecond = 20• Function headerdouble getSum ( double x[][maxSecond],…• Function prototype (semicolon at end)double getSum ( double x[][maxSecond],…double getSum ( double [][maxSecond],…• Calling programconst int maxFirst = 20;double x[maxFirst][maxSecond];// other code assigns values to x arraydouble result = getSum( x, ….21Passing 2D Arrays to Functions IV• Global constant not required, but helpful to accommodate changes to size of second dimension• The second dimension must be the same in the following three statements:– The function prototype– The function header– The declaration of the array passed to the function• Final project


View Full Document

CSUN COMP 106 - Programming with Two- dimensional Arrays

Download Programming with Two- dimensional Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming with Two- dimensional Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming with Two- dimensional Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?