Multi-Dimensional Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Multi-Dimensional Arrays

Description:

(Rounded to Whole Dollar Amounts) Row and Column Indices for an Array. 0 ... (Rounded to Whole Dollar Amounts) ... array contents are displayed with dollar signs. ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 18
Provided by: robertw8
Learn more at: https://www.ecs.csun.edu
Category:

less

Transcript and Presenter's Notes

Title: Multi-Dimensional Arrays


1
Multi-Dimensional Arrays
2
A Table of Values

Balances for Various Interest Rates Compounded Annually (Rounded to Whole Dollar Amounts) Balances for Various Interest Rates Compounded Annually (Rounded to Whole Dollar Amounts) Balances for Various Interest Rates Compounded Annually (Rounded to Whole Dollar Amounts) Balances for Various Interest Rates Compounded Annually (Rounded to Whole Dollar Amounts)
Year 5.00 5.50 6.00
1 1050 1055 1060
2 1103 1113 1124
3 1158 1174 1191
3
Row and Column Indices for an Array

Indices 0 1 2
0 1050 1055 1060
1 1103 1113 1124
2 1158 1174 1191
4
Multi-Dimensional Array Basics
  • We declare a variable, table, to be a
    two-dimensional array as follows
  • int table new int 106
  • This is the same as for one-dimensional arrays
    except we use a second pair of square brackets in
    two places.
  • You can have arrays of any number of dimensions.

5
Multi-Dimensional Array Basics (contd)
  • In a two-dimensional array we consider the first
    index to represent the row number and the second
    to represent the column.
  • As was true of the indexed variables for
    one-dimensional arrays, indexed variables of
    multi-dimensional arrays are variables of the
    base type and can be used anyplace that a
    variable of the base type is allowed.

6
Example of Using a Two-Dimensional Array
  • / Displays a two-dimensional table showing
    how interest rates affect bank
    balances./public class InterestTable
    public static void main(String args)
    int table new int106 int row,
    column for (row 0 row lt 10 row)
    for (column 0 column lt 6 column)
    tablerowcolumn
    balance(1000.00, row 1, (5 0.5column))
    System.out.println("Balances for Various
    Interest Rates") System.out.println("Compou
    nded Annually") System.out.println("(Rounde
    d to Whole Dollar Amounts)")
    System.out.println("Years 5.00 5.50 6.00 6.50
    7.00 7.50") System.out.println( )
    for (row 0 row lt 10 row)
    System.out.print((row 1) " ")
    for (column 0 column lt 6 column)
    System.out.print("" tablerowcolumn "
    ") System.out.println( )

7
Example of Using a Two-Dimensional Array (contd)
  • / Returns the balance in an account that
    starts with startBalance and is left for the
    indicated number of years with rate as the
    interest rate. Interest is compounded annually.
    The balance is rounded to a whole number.
    / public static int balance(double
    startBalance, int years, double rate)
    double runningBalance startBalance int
    count for (count 1 count lt years
    count) runningBalance
    runningBalance(1 rate/100) return (int)
    (Math.round(runningBalance))

8
Multi-Dimensional Array Parameters and Returned
Values
  • Methods may have multi-dimensional array
    parameters and may return a multi-dimensional
    array.
  • The situation is similar to that of the
    one-dimensional case, except that you use more
    square brackets.

9
Example Using a Multi-Dimensional Array Parameter
  • / Displays a two-dimensional table showing
    how interest rates affect bank
    balances./public class InterestTable2
    public static void main(String args)
    int table new int106 int row,
    column for (row 0 row lt 10 row)
    for (column 0 column lt 6 column)
    tablerowcolumn
    balance(1000.00, row 1, (5 0.5column))
    System.out.println("Balances for Various
    Interest Rates") System.out.println("Compou
    nded Annually") System.out.println("(Rounde
    d to Whole Dollar Amounts)")
    System.out.println("Years 5.00 5.50 6.00 6.50
    7.00 7.50") System.out.println( )
    showTable(table)

10
Example Using a Multi-Dimensional Array Parameter
(contd)
  • / Precondition The array displayArray
    has 10 rows and 6 columns. Postcondition
    The array contents are displayed with dollar
    signs. / public static void
    showTable(int displayArray) int
    row, column for (row 0 row lt 10
    row) System.out.print((row
    1) " ") for (column 0 column
    lt 6 column) System.out.print(""
    displayArrayrowcolumn " ")
    System.out.println( ) public
    static int balance(double startBalance, int
    years, double rate) double
    runningBalance startBalance int count
    for (count 1 count lt years count)
    runningBalance runningBalance(1
    rate/100) return (int) (Math.round(runningB
    alance))

11
Implementation of Multi-Dimensional Arrays
  • In Java, multi-dimensional arrays are implemented
    using one-dimensional arrays.
  • The array table declared as follows
  • int table new int106
  • is a one-dimensional array of length 10 whose
    elements are one-dimensional arrays of length 6.
  • In other words, multi-dimensional arrays are
    arrays of arrays.

12
The Method showTable Redefined
  • / The array displayArray can have any
    dimensions. Postcondition The array contents
    are displayed with dollar signs./public static
    void showTable(int displayArray) int
    row, column for (row 0 row lt
    displayArray.length row)
    System.out.print((row 1) " ") for
    (column 0 column lt displayArrayrow.length
    column) System.out.print(""
    displayArrayrowcolumn " ")
    System.out.println( )

13
Programming Example Using Multi-Dimensional Arrays
  • / Class for a one-week record of the time
    worked by each employee. Uses a five-day week
    (Mon.-Fri.). main has a sample
    application./public class TimeBook
    private int numberOfEmployees private
    int hours //hoursij has the
    hours for employee j on day i. private int
    weekHours //weekHoursi has the weeks
    hours worked for //employee i1.
    private int dayHours //dayHoursi
    has the total hours worked by all
    //employees on day i. Monday is 0, Tuesday 1,
    etc. / Reads hours worked for
    each employee on each day of the week into
    the two-dimensional array hours. (The method
    for input is just a stub in this preliminary
    version.) Computes the total weekly hours
    for each employee and the total daily hours
    for all employees combined. / public
    static void main(String args)
    TimeBook book new TimeBook(3)
    book.setHours( ) book.update( )
    book.showTable( )

14
Programming Example Using Multi-Dimensional
Arrays (contd)
  • public TimeBook(int theNumberOfEmployees)
    numberOfEmployees theNumberOfEmployees
    hours new int5numberOfEmployees
    //the 5 is for the 5 days Monday through
    Friday. weekHours new
    intnumberOfEmployees dayHours new
    int5 public void setHours( ) //This
    is just a stub. hours00 8
    hours01 0 hours02 9
    hours10 8 hours11 0 hours12
    9 hours20 8 hours21 8
    hours22 8 hours30 8
    hours31 8 hours32 4
    hours40 8 hours41 8 hours42
    8 public void update( )
    computeWeekHours( ) computeDayHours(
    )

15
Programming Example Using Multi-Dimensional
Arrays (contd)
  • private void computeWeekHours( )
    int dayNumber, employeeNumber, sum for
    (employeeNumber 1 employeeNumber
    lt numberOfEmployees employeeNumber)
    //Process one employee sum 0
    for(dayNumber 0 dayNumber lt 5
    dayNumber) sum sum
    hoursdayNumberemployeeNumber - 1
    //sum contains the sum of all the hours
    worked //in one week by employee
    with number employeeNumber.
    weekHoursemployeeNumber - 1 sum
    private void computeDayHours( )
    int dayNumber, employeeNumber, sum
    for (dayNumber 0 dayNumber lt 5
    dayNumber) //Process one day (for all
    employees) sum 0 for
    (employeeNumber 1
    employeeNumber lt numberOfEmployees
    employeeNumber)
    sum sum hoursdayNumberemployee
    Number - 1 //sum contains the
    sum of all hours worked by all
    //employees on day dayNumber.
    dayHoursdayNumber sum

16
Programming Example Using Multi-Dimensional
Arrays (contd)
  • public void showTable( ) int row,
    column System.out.print("Employee ")
    for (column 0 column lt
    numberOfEmployees column)
    System.out.print((column 1) " ")
    System.out.println("Totals")
    System.out.println( ) for (row 0 row
    lt 5 row)
    System.out.print(day(row) " ")
    for (column 0 column lt hoursrow.length
    column) System.out.print(hoursr
    owcolumn " ")
    System.out.println(dayHoursrow)
    System.out.println( )
    System.out.print("Total ") for
    (column 0 column lt numberOfEmployees
    column) System.out.print(weekHoursc
    olumn " ") System.out.println( )

17
Programming Example Using Multi-Dimensional
Arrays (contd)
  • //Converts 0 to "Monday", 1 to "Tuesday" etc.
    //Blanks used to make all strings the same
    length. private String day(int dayNumber)
    String dayName null switch
    (dayNumber) case 0
    dayName "Monday "
    break case 1
    dayName "Tuesday " break
    case 2 dayName
    "Wednesday" break
    case 3 dayName "Thursday "
    break case 4
    dayName "Friday "
    break default
    System.out.println("Fatal Error.")
    System.exit(0) break
    return dayName
Write a Comment
User Comments (0)
About PowerShow.com