2D ARRAYS
TWO-DIMENSIONAL ARRAYS
IBO Assessment Statement
Multi-Dimensional Arrays
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.
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
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
Explain the purpose of this algorithm.
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.
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
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
Hint: You could try creating two functions
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();
}
Use the sample data below to compare arrays and record data structures.
Discussing the differences between an array and a record
Each kind of structured data has its own advantages and disadvantages for specific purposes.
Array
Discussing the differences between an array and a record
Record
In many cases, it is actually possible to use either kind of structure.
Task
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
Task
Write pseudocode solutions for each of the stubs in the sample code.