Multi-dimensional arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Multi-dimensional arrays

Description:

First dimension is number of rows. Second dimension is number of columns. Can have more than two dimensions. Allocated row-pointer in memory ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 10
Provided by: RMK
Category:

less

Transcript and Presenter's Notes

Title: Multi-dimensional arrays


1
Multi-dimensional arrays
  • Allows you to work with matrices
  • int array new int2030
  • First dimension is number of rows
  • Second dimension is number of columns
  • Can have more than two dimensions
  • Allocated row-pointer in memory
  • A row can be stored anywhere in memory
  • Elements in a row are stored contiguously
  • Can visualize as array of arrays

2
Transpose
  • class transpose
  • public static void main(String args)
  • int i, j
  • int table new int34
  • int ttable new int43
  • for(i0 i lt 3 i)
  • for(j0 j lt 4 j)
  • tableij (i4)j1
  • System.out.print(tableij " ")
  • System.out.println()
  • System.out.println()
  • for(j0 j lt 4 j)
  • for(i0 i lt 3 i)
  • ttableji tableij
  • System.out.print(ttableji " ")

3
Matrix multiplication
  • class MatrixMultiply
  • public static void main (String arg)
  • int m 20, n 30, p 40
  • double A new doublemn
  • double B new doublenp
  • double C
  • InitializeArray (A, m, n)
  • InitializeArray (B, n, p)
  • C Multiply (A, B, m, n, p)
  • // continued in next slide

4
Matrix multiplication
  • public static void InitializeArray (double
    x, int m, int n)
  • int i, j
  • for (i0iltmi)
  • for (j0jltnj)
  • xij ij

5
Matrix multiplication
  • public static double Multiply (double
    x, double y, int p, int q, int r)
  • double z new doublepr
  • int i, j, k
  • for (i0iltpi)
  • for (j0jltrj)
  • zij 0
  • for (k0kltqk)
  • zij (xikykj)
  • return z
  • // end class

6
Allocating a triangular matrix
  • May save memory for symmetric matrices
  • Allocate space to store the starting addresses of
    m rows
  • Then allocate the m rows themselves
  • int m 20
  • double triangle new doublem
  • int i
  • for (i0iltmi)
  • // allocate lower triangle
  • trianglei new doublei1

7
arraylength
  • class arraylength
  • public static void main(String args)
  • int list new int10
  • int nums 1, 2, 3
  • int table // a variable-length table
  • 1, 2, 3,
  • 4, 5,
  • 6, 7, 8, 9

8
  • System.out.println("length of list is "
    list.length)
  • System.out.println("length of nums is "
    nums.length)
  • System.out.println("length of table is "
    table.length)
  • System.out.println("length of table0 is "
    table0.length)
  • System.out.println("length of table1 is "
    table1.length)
  • System.out.println("length of table2 is "
    table2.length)
  • System.out.println()

9
  • // use length to initialize list
  • for(int i0 i lt list.length i)
  • listi i i
  • System.out.print("Here is list ")
  • // now use length to display list
  • for(int i0 i lt list.length i)
  • System.out.print(listi " ")
  • System.out.println()
Write a Comment
User Comments (0)
About PowerShow.com