1 of 15

2D ARRAYS

2 of 15

TWO-DIMENSIONAL ARRAYS

IBO Assessment Statement

3 of 15

Multi-Dimensional Arrays

4 of 15

int rows = 3;

int cols = 2;

int table[] [] = new int [rows] [cols];

for (int rloop = 0; rloop < rows; rloop++)

{

for(int cloop = 0; cloop < cols; cloop++)

{

System.out.print("Value for [" + rloop + ", " + cloop + "] = ");

table[rloop] [cloop] = Input.nextInt();

}

}

rloop

cloop

table[rloop][cloop]

output

0

0

Value for [0,0] =

Table [0] [0 ] = 1

0

1

Value for [0,1] =

0

1

0

1

2

1

?

?

2

?

?

The following example, allows the user to enter data into a table with 3 rows and 2 columns.

Trace the algorithm and complete the table for the user input 1, 2, 3, 4, 5, 6.

5 of 15

rloop

cloop

table[rloop][cloop]

output

0

0

Value for [0,0] =

Table [0] [0 ] = 1

0

1

Value for [0,1] =

1, 2, 3, 4, 5, 6

6 of 15

rloop

cloop

table[rloop][cloop]

output

0

0

Value for [0,0] =

Table [0] [0 ] = 1

0

1

Value for [0,1] =

Table [0] [1 ] = 2

1

0

Value for [1,0] =

Table [1] [0 ] = 3

1

1

Value for [1,1] =

Table [1] [1] = 4

2

0

Value for [2,0] =

Table [2] [0 ] = 5

2

1

Value for [2,1] =

Table [2] [1 ] = 6

SOLUTION - DO NOT COPY UNTIL YOU HAVE FULLY SOLVED

7 of 15

Explain the purpose of this algorithm.

8 of 15

int rows = 3;

int cols = 2;

int table[] [] = new int [rows] [cols];

int[] rtotal = new int[rows];

int[] ctoal = new int[cols];

for (int rloop = 0; rloop < rows; rloop++)

{

for(int cloop = 0; cloop < cols; cloop++)

{

System.out.print("Value for [" + rloop + ", " + cloop + "] = ");

table[rloop] [cloop] = Input.nextInt();

rtotal[rloop] = rtotal[rloop] + table[rloop][cloop];

ctotal[cloop] = ctotal[cloop] + table[rloop][cloop];

}

}

rloop

cloop

rtotal

ctotal

0

0

rtotal[rloop] = rtotal[0] = 0+1 = 1

ctotal[cloop] = ctotal[0] = 0+1 = 1

0

1

0

1

2

1

2

The following example, allows the user to enter data into a table with 3 rows and 2 columns. The algorithm calculates the totals for each row and column.

Trace the algorithm and complete the table for the user input 1, 2, 3, 4, 5, 6.

9 of 15

rloop

cloop

rtotal

ctotal

0

0

rtotal[rloop] = rtotal[rloop] + table[rloop][cloop]

rtotal[0] = rtotal[0] + table[0][0]

rtotal[0] = 0 + 1

rtotal[0] = 1

ctotal[cloop] = ctotal[cloop] + table[rloop][cloop]

ctotal[0] = ctotal[0] + table[0][0]

ctotal[0] = 0 + 1

ctotal[0] = 1

0

1

rtotal[rloop] = rtotal[rloop] + table[rloop][cloop]

rtotal[0] = rtotal[0] + table[0][1]

rtotal[0] = 1 + 2

rtotal[0] = 3

ctotal[cloop] = ctotal[cloop] + table[rloop][cloop]

ctotal[1] = ctotal[1] + table[0][1]

ctotal[1] = 0 + 2

ctotal[1] = 2

1

0

rtotal[rloop] = rtotal[rloop] + table[rloop][cloop]

rtotal[1] = rtotal[1] + table[1][0]

rtotal[1] = 0 + 3

rtotal[1] = 3

ctotal[cloop] = ctotal[cloop] + table[rloop][cloop]

ctotal[0] = ctotal[0] + table[1][0]

ctotal[0] = 1 + 3

ctotal[0] = 4

1

1

rtotal[rloop] = rtotal[rloop] + table[rloop][cloop]

rtotal[1] = rtotal[1] + table[1][1]

rtotal[1] = 3 + 4

rtotal[1] = 7

ctotal[cloop] = ctotal[cloop] + table[rloop][cloop]

ctotal[1] = ctotal[1] + table[1][1]

ctotal[1] = 2 + 4

ctotal[1] = 6

2

0

rtotal[rloop] = rtotal[rloop] + table[rloop][cloop]

rtotal[2] = rtotal[2] + table[2][0]

rtotal[2] = 0 + 5

rtotal[2] = 5

ctotal[cloop] = ctotal[cloop] + table[rloop][cloop]

ctotal[0] = ctotal[0] + table[2][0]

ctotal[0] = 4 + 5

ctotal[0] = 9

2

1

rtotal[rloop] = rtotal[rloop] + table[rloop][cloop]

rtotal[2] = rtotal[2] + table[2][1]

rtotal[2] = 5 + 6

rtotal[2] = 11

ctotal[cloop] = ctotal[cloop] + table[rloop][cloop]

ctotal[1] = ctotal[1] + table[2][1]

ctotal[1] = 6 + 6

ctotal[1] = 12

SOLUTION - DO NOT COPY UNTIL YOU HAVE FULLY SOLVED

int rows = 3;

int cols = 2;

int table[] [] = new int [rows] [cols];

int[] rtotal = new int[rows];

int[] ctoal = new int[cols];

for (int rloop = 0; rloop < rows; rloop++)

{

for(int cloop = 0; cloop < cols; cloop++)

{

System.out.print("Value for [" + rloop + ", " + cloop + "] = ");

table[rloop] [cloop] = Input.nextInt();

rtotal[rloop] = rtotal[rloop] + table[rloop][cloop];

ctotal[cloop] = ctotal[cloop] + table[rloop][cloop];

}

}

1, 2, 3, 4, 5, 6

10 of 15

rloop

cloop

rtotal

ctotal

0

0

rtotal[0]=0+1=1

ctotal[0]=0+1=1

1

rtotal[0]=1+2=3

ctotal[1]=0+2=2

1

0

rtotal[1]=0+3=3

ctotal[0]=1+3=4

1

rtotal[1]=3+4=7

ctotal[1]=2+4=6

2

0

rtotal[2]=0+5=5

ctotal[0]=4+5=9

1

rtotal[2]=5+6=11

ctotal[1]=6+6=12

Assignment 1

  1. Improve your solution to output the table with row and columns totals.
  2. Improve the solution to include functions and modular programming. You are encouraged to use method drivers and method stubs in your development approach.

Hint: You could try creating two functions

    • getData()
    • displayArray(myArray)
  1. Add to your solution and display the array, in table format, with row and column averages.

11 of 15

import java.util.Scanner;

import java.io.*;

public class TwoDArrays

{

public static void main(String[] args) throws IOException

{

int cols;

int rows;

System.out.println("How many colums do you have? ");

Scanner Input = new Scanner(System.in);

cols = Input.nextInt();

System.out.println("How many rows do you have? ");

rows = Input.nextInt();

int[][] table = new int[rows][cols];

int[] rtotal = new int[rows];

int[] ctotal = new int[cols];

System.out.println();

System.out.println("Please enter your data");

System.out.println("======================\n");

for (int rloop = 0; rloop < rows; rloop++)

{

for(int cloop = 0; cloop < cols; cloop++)

{

System.out.print("Value for [" + rloop + ", " + cloop + "] = ");

table[rloop] [cloop] = Input.nextInt();

rtotal[rloop] = rtotal[rloop] + table[rloop][cloop];

ctotal[cloop] = ctotal[cloop] + table[rloop][cloop];

}

System.out.println();

}

System.out.println("Here is your table");

System.out.println("==================\n");

for (int rloop = 0; rloop < rows; rloop++)

{

//int rtotal = 0;

for (int cloop =0; cloop <cols; cloop++)

{

System.out.print("\t" + table [rloop] [cloop] );

}

System.out.println("\t| " +rtotal[rloop]);

System.out.println();

}

for (int cloop =0; cloop <cols; cloop++)

{

System.out.print("\t__");

}

System.out.println("\n");

for (int cloop =0; cloop <cols; cloop++)

{

System.out.print("\t" + ctotal[cloop] );

}

}

}

for (int rloop = 0; rloop < rows; rloop++)

{

for(int cloop = 0; cloop < cols; cloop++)

{

System.out.print("Value for [" + rloop + ", " + cloop + "] = ");

table[rloop] [cloop] = Input.nextInt();

rtotal[rloop] = rtotal[rloop] + table[rloop][cloop];

ctotal[cloop] = ctotal[cloop] + table[rloop][cloop];

}

System.out.println();

}

12 of 15

Use the sample data below to compare arrays and record data structures.

13 of 15

Discussing the differences between an array and a record

Each kind of structured data has its own advantages and disadvantages for specific purposes.

Array

  • a data structure that stores a list of values of the same data type. Every location in an array is of the same data type.
  • we can process an entire array using a For Next Loop. Referencing individual items in an array is fairly simple, and a FOR loop is compact, easy to write, and easy to understand.
  • each element has an index to specify its position. Arrays have locations and these are numbered.
  • the data entered in a single array must all be of the same type, and that makes grouping of different data types into related categories rather difficult.

14 of 15

Discussing the differences between an array and a record

Record

  • it is possible to input or output data into the fields of an entity to create a record using dor notation.
  • a record can be used to store a collection of data of different data types. Each data type is referred to as a field.
  • each field has a name. Every field in a record can be of different data types.
  • data cannot be referred to by a numerical position; rather a field name must be employed. For example, in Java we use dot notation.
  • records are (potentially) more elaborate structures than arrays, and the mechanism for using them requires more work to set up.

In many cases, it is actually possible to use either kind of structure.

Task

  1. Define a suitable data structure for the records in the above image.
  2. Suppose one wanted to design a data structure to hold these class records: student name, id and age. In many cases, it is actually possible to use either an array or a record structure. Explain how you could design this using arrays?

15 of 15

One could arrange several arrays in a "side-by-side" fashion as follows:

String[] name = new String[30];� int[] id = new int[30];

Int[] age = new int[30];

enterStudents(); //uses a for loop to enter values for each student

listStudents(); //uses a for loop to output all student data in tabular format

  • Each field is stored in its own 1 dimensional array.
  • Each array is of a fixed data type: String for name, int for id and int for age.
  • With three arrays we can store records side-by-side
  • We can use a for loop to process all three arrays at the same time.
  • As id and age are the same data type, we could use a 2D array instead of two lists for each.

Task

Write pseudocode solutions for each of the stubs in the sample code.