Docsity

Programming with Two-Dimensional Arrays: Notations, Declaration, and Applications, Slides of Computer Science

Toggle sidebar
bg1
Programming with two-dimensional arrays May 9, 2006
1
Programming with Two
Programming with Two-
-
dimensional Arrays
dimensional Arrays
Larry Caretto
Computer Science 106
Computing in Engineering
and Science
May 9, 2006
2
Outline
• 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 use
3
Two-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 operators
4
Two-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 classification
5
Two-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
• Column
6
Two-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 machine
const int maxOp = 6, maxMach = 4;
int output[maxOp][maxMach];
cout << output[3][2];
pf3
pf4
pf5

Partial preview of the text

Download Programming with Two-Dimensional Arrays: Notations, Declaration, and Applications and more Slides Computer Science in PDF only on Docsity!

Programming with Two-Programming with Two-

dimensional Arraysdimensional Arrays

Larry Caretto Computer Science 106

Computing in Engineering

and Science

May 9, 2006

2

Outline

  • 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 use

3

Two-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 operators

4

Two-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 classification

5

Two-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
  • Column 6

Two-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 machine const int maxOp = 6, maxMach = 4; int output[maxOp][maxMach]; cout << output[3][2];

7

Two-Dimensional Array Data

M tot 208 316 265 202 991

Op tot

M 3

M 2

M 1

M 0

Op 5

Op 4

Op 3

Op 2

Op 1

Op 0

Individual

data plus

totals for

operators

and

machines

output[3][2]

Array data 8

Two-dimensional array Code

const int maxOp = 6, maxMach = 4

int 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;

9

Other 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?

10

Units 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]; }

11

Units 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]; } 12

Comments 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]

19

Passing 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 parameter

20

Passing 2D Arrays to Functions III

  • Global constant: const int maxSecond = 20
  • Function header double getSum ( double x[][maxSecond],…
  • Function prototype (semicolon at end) double getSum ( double x[][maxSecond],… double getSum ( double [][maxSecond],…
  • Calling program const int maxFirst = 20; double x[maxFirst][maxSecond]; // other code assigns values to x array double result = getSum( x, ….

21

Passing 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 uses two-dimensional arrays 22

Passing 2D Arrays to Functions V

  • Example: write a function that accepts a

two-dimensional array, output, used in

the previous example and computes and

returns the row sums and columns sums

as well as the total

  • How to pass information?
    • Pass 2D output array into function
    • Return 1D arrays with row and column sums
    • Return total in function name
    • Pass number of machines and operators, which can be less than the maximum array sizes, into function

23

Example of 2D Array Function

int getSums( int output[][maxMach], int opSum[], int machSum[], int Nop, int Nmach) { int total = 0; for ( int op = 0; op < Nop; op++ ) { opSum[op] = 0; for ( int m = 0; m < Nmach; m++ ) opSum[op] += output[op][m];

total += opSum[op]; } // continues on next chart 24

2D Array Function Concluded

for ( int m = 0; m < Nmach; m++ ) { machSum[m] = 0; for ( int op = 0; op < Nop; op++ ) machSum[op] += output[op][m]; } return total; } // closes function opening brace

  • How do we use this function?
  • What is its prototype?

25

Using the 2D Array Function

  • Start with global constants for common

array dimensions in various locations

const int maxMach = 10, maxOp = 10;

  • Prototype is just header with a semicolon int getSums( int output[][maxMach], int opSum[], int machSum[], int Nop, int Nmach);
  • Use global constants as array dimensions in

calling program

int output[maxOp][maxMach], opSum[maxOp], machSum[maxMach]; 26

Using the 2D Array Function

  • Get data in calling program (usually from

file)

ifstream inFile( “production.dat” ); inFile >> Nop >> Nmach; for (op = 0; op < Nop; op++ ) for ( m = 0; m < Nmach; m++ ) infile >> output[op][m];

  • Call function int total = getSums( output, opSum, machSum, Nop, Nmach);
  • Output results

Array call has only

array names

27

Input Data Files for Arrays

  • Must match input statements in code for (i = 0; i < N; i++) inFil >> x[i]; for (i = 0; i < N; i++) inFil >> y[i];
  • Compare above statements with code below for (i = 0; i < N; i++) { inFil >> x[i] >> y[i]; }
  • First example read all x data then all y data.

Second reads x and y data in pairs

  • Usually write code to determine number of

array elements by testing for end of file

28

Input Data File for 1D Arrays

  • How does the code

below read x and y from

each file on this page?

for (i = 0; i < 3; i++) cin >> x[i] >> y[i];

  • What about this code? for (i = 0; i < 3; i++) cin >> x[i]; for (i = 0; i < 3; i++) cin >> y[i];

29

Input Data Files for 2D Arrays

  • Recall input code from example of passing

2D arrays to functions

ifstream inFile(“production.dat”); inFile >> Nop >> Nmach; for (op = 0; op < Nop; op++ ) { for ( m = 0; m < Nmach; m++ ) { inFile >> output[op][m]; } }

  • How would you prepare the data file?

Braces not needed 30

Input Data File for 2D Arrays

  • Usually prepare data file for 2D arrays

to look like row and column data