IB Computer Science Unit 1
IB Computer Science Unit 2
IB Computer Science Unit 3
IB Computer Science Unit 4
IB Computer Science Unit 5
IB Computer Science Unit 6

IB Computer Science Java Two Dimensional Arrays

IB Computer Science Learning Goals

In this IB Computer Science lesson you will be learning about:

  • Creating tables of data using 2D arrays
  • Use a 2D array to make a matrix object

2D Tabular Data

Sometimes data comes naturally in two dimensional form:

  • Maps are two dimensional
  • The Layout of a printed page is two dimensional
  • A computer generated image is two dimensional

Imagine a class of 7 students that have a quiz every week for 5 weeks

IB Computer Science 2D Array Table Example

In Java, a table may be implemented as a 2D array

  • Each cell of the array is a variable that can hold a value and works like any variable
  • As with one dimensional arrays, every cell in a 2D array is of the same type
  • Each cell of the array is specified with a row and column number, in that order
  • Say that gradeTable is a 2D array of integers and that it looks like the table above
    • gradeTable[0][1] -> holds the value 42
    • gradeTable[3][4] -> holds the value 93

The subscripted variables are used just the same way any other variable or 1D array value would be used.

 

Important Info

  • Suppose you have a 2D array with N rows and M columns
  • Rows are numbered 0 to N-1
  • Columns are numbered from 0 to M-1
  • A 2D array with N rows and M columns has N x M number of cells

Declaring and assigning values to a 2D Array

Just like 1D arrays there are many ways to initialize a 2D array

IB Computer Science 2D Array Java Declaration

Store Hours Example

An employer stores the hours worked by each of its employees for each day of the week in a file called ehours.txt. There are 3 employees and data is kept for all 7 days during the week. Your job is to add up the total hours worked by each employee and add up the total hours worked by all the employees each day of the week. Display your results in a nice way.

IB Computer Science Store Hours 2D Array

Solution:

  • This problem essentially asks you to add up the rows and columns of a two dimensional array
  • So we read in the data from the file directly into a two dimensional array
  • Add up the rows using a nested loop
  • Add up the columns using another nested loop
  • Display results using some creativity

Variables Needed

  • The variable hours will be a 2 Dimensional array to hold the 7 x 3 grid of employee hours
IB Computer Science Store Hours Variables

Reading the Data

  • There are 7 lines of data in the file and 7 rows in the 2 dimensional array so the outside loop will count from 0 to 6.
  • Each time through that loop a line must be read from the file
  • Since there are 3 pieces of data in each line you must use the StringTokenizer Class to create tokens for those 3 values them
  • To cycle through and store in the array each column value or token you can use another loop that counts from 0 to 2

IB Computer Science Java 2D Read File

Calculating the Sum for each Row

  • The sum of the rows is equal to
    o hours[row][0] + hours[row][1] + hours[row][2]
  • Thus you can use the outside loop for each row and the inside loop to add up all the individual columns
  • In the code above rowSum is overwritten for each different row, so unless you store those values in an array somehow you will have to print its value on the screen immediately after (see full solution)
IB Computer Science Find the sum of Columns 2D array

Calculating the Sum of each Column

  • The sum of the columns is equal to
    o hours[0][col] + hours[1][col] +…+ hours[6][col]
  • Thus you can use the outside loop for each col and the inside loop to add up all the individual rows
  • In the code above colSum is overwritten for each different column, so unless you store those values in an array somehow you will have to print its value on the screen immediately after (see full solution)
IB Computer Science Sum of Rows 2D Array Java

Complete Solution

IB Computer Science 2D Array Example Complete

Complete the following as practice

1. Pattern Generator
Generate Tables with the following patterns. Your program must work for any size square grid.

2. Pascal Triangle
Below is what as known as pascals triangle
• Here the entire first row is 0 except for the first column which is 1
• The entire first column is always 1
• All the other numbers are obtained by adding the number directly above it to the number diagonally above and to the left of it
o At position [8][4] the value is 70 which is 35 (The number directly above its position) + 21 (The number diagonally above and to the left of its position

Complete the following tasks:
• Use a two Dimensional array to create the Pascal triangle shown above (Assign the first row explicitly and use a nested loop to create all subsequent rows)
• Write the Pascal Triangle to a file without all the 0’s
• Modify the program so that it asks the user how many levels of the triangle to display

Looking to Learn More about Computer Science and Coding?

Check out our programing in python courses that focus on high school level coding.  

  • Grade 11 Computer Science ICS3U
  • Grade 12 Computer Science ICS4U

These courses are complete with interactive coding lessons, teacher led videos, and more practice questions with complete solutions

Return To International Baccalaureate Computer Science Main Page