Multi-dimensional Arrays: Understanding 2-D Arrays and Their Applications - Prof. Cynthia , Study notes of Computer Science

An introduction to multi-dimensional arrays, specifically 2-d arrays, also known as matrices. It covers the organization of information, memory allocation, initialization, and processing of 2-d arrays. The document also includes examples and a method to print a 2-d array in table format. Additionally, it mentions arrays with more dimensions and a programming problem related to locating stars in a digitized night sky representation.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-yw4
koofers-user-yw4 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 110 Arrays II – Multi-dimensional Arrays 1
Copyright C. Tanner 2009
Organize information in a way that solves more complex problems than can be
done with a linear or one dimensional array
2-dimensioanl array – matrix
o simplest
o Table
o All elements must be of the same data type
o Fixed size
Examples
o Classroom seating chart 6 desk in a row with 5 rows
X X X X X X
X X X X X X
X X X X X X
X X X X X X
X X X X X X
String [] [] desk = new String [5][6]
o Employee Time time
Emp Mon Tues Wed Thurs Fri Sat Sun
double [] [] timeCard= new double [30][7]
o Girl Scout Cookie Orders
Customer ThinMints DoSiDoes Treffoils
int [] [] cookieOrders = new int [#customers][3]
Reference a element
Need both row and column index
matrix[r][c]
desk [0][5] – the student’s name in the first row door seat
cookieOrder[3][0] – the number of ThinMints customer 3 ordered
custTotal =0
for (int i = 0; i <3; i++)
custTotal = cookieOrder[3][i];
pf3
pf4

Partial preview of the text

Download Multi-dimensional Arrays: Understanding 2-D Arrays and Their Applications - Prof. Cynthia and more Study notes Computer Science in PDF only on Docsity!

  • Organize information in a way that solves more complex problems than can be done with a linear or one dimensional array
  • 2-dimensioanl array – matrix o simplest o Table o All elements must be of the same data type o Fixed size
  • Examples o Classroom seating chart 6 desk in a row with 5 rows

X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X

ƒ String [] [] desk = new String [5][6] o Employee Time time

Emp Mon Tues Wed Thurs Fri Sat Sun

ƒ double [] [] timeCard= new double [30][7]

o Girl Scout Cookie Orders

Customer ThinMints DoSiDoes Treffoils

  • int [] [] cookieOrders = new int [#customers][3]
  • Reference a element
  • Need both row and column index
  • matrix[r][c]
  • desk [0][5] – the student’s name in the first row door seat
  • cookieOrder[3][0] – the number of ThinMints customer 3 ordered

custTotal = for (int i = 0; i <3; i++) custTotal = cookieOrder[3][i];

  • Memory Allocation of 2 dimensional Arrays
  • Initialization when declared o double matrix [] [] = {{3.2,4.2,1.0}, {5.7,6.1,0.0}}

Creates [2][3]

  • Processing o Nested loops o For each row ƒ For each col - Process element [r][c] o [0][0],[0][1],[0][2][1][0][1][2] etc o Row major order o If we switch the loops then we process in column major order
  • Sum the elements

int sumMatrix (int [] [] matrix) { int sum =0;

for (int r=0; r<matrix.length;r++ for (int c=0; c < matrix[r].length;c++) sum += matrix[r][c];

return sum; }

  • Determining the length of the matrix o Allows for general purpose algorithms o # row Î arrayName.length o # col Î arrayName[row].length ƒ Must do this because each row is an array itself

ƒ Sample Program Stars.java

Adjacent Elements

[i-1][j]

[i][j-1] [i][j] [i][j+1]

[i+1][j]