Arrays



Two-dimensional arrays

Important:

* Review the notes on 2-D arrays from your 1st year programming course, given by Brenda.

* Review this year’s notes on 1-D arrays

Important definitions:

1. Loop: a programming structure to repeat something over and over again, using either while or for in Java.

2. A variable: a storage box in a programming language. Each 2-D array consists of rows and columns of variables.

3. End user: the person who ultimately uses the program; he/she may know little about computers, other than how to type and use the mouse etc.

Now read the following:

1-dimensional arrays represent lists.

2-dimensional arrays represent tables.

See the first line in the main method below for the declaration of a 2-D array. Here it is again:

int [ ][ ] orange = new int[3][4];

In 2-D arrays the first subscript can conveniently be regarded as representing the row number and the second subscript the column number. Thus the array orange would represent an array with three rows numbered 0, 1, and 2 and four columns 0, 1, 2 and 3.

Thus the variable in the 1st row (numbering from 0) and 2nd column (numbering from 0) would be orange[1][2].

2-D arrays, just like 1-D arrays, can be big or small:

Here’s a small array:

int [ ][ ] apple = new int[2][3];

containing 6 storage boxes for integers.

They are:

apple[0][0] apple[0][1] apple[0][2]

apple[1][0] apple[1][1] apple[1][2]

Mote from the above, that the first number in square brackets shows the row and the second number in square brackets shows the column.

Each storage location can hold any integer. For example apple[0][1] might have 4,567 in it and apple[0][2] might have -345 in it.

Here’s a large array:

int [ ][ ] apple = new int[34][26];

containing 34 multiplied by 26 equals 844 storage locations for integers.

They can be of different types, for example:

double[][] pear = new double[3][4];

boolean[][] pomegranate = new boolean[3][4];

Now study the following program which you’re asked to develop paragraph by

paragraph. Each paragraph will deal with just one aspect of processing the array. For example, you might have a paragraph to fill an array, another to print it out, yet another to double each element of the array, another to print the array so that each row looks like a column, another to find the maximum number in the 2nd column etc.

|In the case of a paragraph of code that processes each variable in an entire array, you’ll have two loops (each loop using |

|either while or for). |

Thus the code to fill each variable in a 2-D array will have two loops, the outer one usually to control the row number and the inner one to control the column number. For each value of the row number the inner loop runs through each variable in that row, by varying the column number. The inner core will fill just one variable in the array, using myInput.nextInt().

|In the case of a paragraph of code that processes each variable in either a row or a column, you’ll have one loop (using |

|either while or for). |

Thus the code to add up all the numbers in the 1st column will have one loop, to vary the row number, since the column number will be held steady at 1.

Here’s the program you’ll be asked to develop, first by carrying out Activity 1 and Activity 2 which are mentioned in the text of the program, and then by doing the Additional Exercises below (starting on page 5).

//*********************************************

//** Program to illustrate the processing of 2-D integer

//** arrays, built in distinct titled paragraphs of code.

// ** Do activities 1 and 2 in the text of the program

// ** and then the additional exercises in blue below.

//*********************************************

import java.util.Scanner;

public class Array

{

public static void main(String [] args)

{

int [ ][ ] orange = new int[2][3]; // declares an integer array orange

int row; // many people prefer to use the letter i for this variable

int column; // many people prefer to use the letter j for this variable

int maximum;

Scanner myInput = new Scanner(System.in);

// The code below fills a 2-D integer array with numbers that

// the user types in at the keyboard.

// Notice that that the outer loop controls

// the row number while the inner loop controls the

// column number. This means that we're filling the array

// row by row. For each row, the computer obeying this program

// has to run through all the values of the column number.

// The first three numbers the user types in fills the 0th row

// and the second three numbers the user type in fills the 1st row.

// Make sure that the following comment is put into your program.

// Each paragraph of code in an array processing program should

// begin with a title.

// **************************************************

// ** fill the array with six numbers that the user types in:

// **************************************************

row = 0; //start at the top row

while(row < 2)

{

//now process the row, that’s been chosen by the outer loop:

column = 0; // start at the left end of the row

while(column < 3)

{

orange[row][column] = myInput.nextInt();

++column; // move along the row one space

}

++row; // get ready to process the next row

}

// Note that I could write another identical paragraph of code immediately below the one above to double each variable in the array.

// All I’d have to do is change the brown line of code to:

// orange[row][column] = orange[row][column] * 2;

// Activity 1: Copy and paste this program into BlueJ or Eclipse and compile.

// Don’t forget two closing sets of curly brackets properly indented.

// You’ll find the program is rather uncommunicative to the end user.

// (What use could you make of

// System.out.println(“text of some sort”);

// to make it better?)

// Activity 2: Go on now to write another complete paragraph of code to add on // to your program; this new code will be to find the maximum number in

// the 1st row. Assume that the numbers are in the range 0 to 100.

//

// Here’s a hint about how to do it:

// For that you hold the row number steady at 1 and go

// through each and every value of the column number.

// Given that any row of a 2-D array is a 1-D array,

// you only need one loop!

// It’s more or less the same thing as you were doing before

// with 1-D arrays, isn’t it? Only a small step forward.

// (If you’re stuck, just ask.)

// The answer to Activity 2 now follows.

// **************************************************

// ** find the maximum number in the 1st row, using while:

// **************************************************

maximum = 0; // assume, initially, that all numbers are as low as they can be

column = 0;

while(column < 3)

{

if(orange[1][column] > maximum) // note the figure one in square brackets

maximum = orange[1][column];

++column;

}

System.out.println("Suitable text of your choice " + maximum);

// **************************************************

// ** find the maximum number in the 1st row, using for:

// **************************************************

// see notes for information about how to convert

// between while and for:

maximum = 0;

for(column = 0; column < 3; ++column)

{

if(orange[1][column] > maximum)

maximum = orange[1][column];

}

System.out.println(maximum);

}

}

There are exercises in the code above: make sure you’ve done them, before going on to the exercises below.

In the exercises below (just as you did above) start each new paragraph of code with the header:

// **************************************************

// ** description of what the paragraph does:

// **************************************************

You should follow this practice in all the work you do in Java including work submitted in answer to assignments.

Additional Exercises:

1. Write an additional paragraph with code to find the maximum number in the 2nd column. Hint: You must hold the column number steady at 2 and make the computer

run through all the values of the row number. Thus each time round the loop, you’ll have a comparison between orange[row][2] and maximum, in other words:

orange[row][2] > maximum

Compare this with

orange[1][column] > maximum

in the original program.

2. Write an additional loop with code to find the maximum number in a column of the user’s choice. The user’s choice of column is provided through a myInput.nextInt() statement.

3. Write an additional loop with code to add up the numbers in the 1st column (Assuming, like the computer, we number from 0; so that would be the 2nd column in conventional terms.)

4. Repeat exercise 3 for the 1st row.

5. Write out a paragraph of code to print out the array. (Hint: You’ll need to use spaces or tab to separate the numbers in a given row and you’ll need to use a new line

character after an entire row has been printed. The new line character is represented by \n in a System.out.println statement.)

6. Write code to get in a 4 by 5 array (that is 4 rows of 5 columns) from the user and to print out the transpose of this array, that is: where all rows appear to be columns.

Thus if the 4 by 5 array is:

4 3 456 -3 22

452 -453 3 92 23

99 98 29 2345 -4000

4 5 1 32 -3

then it will be printed out thus:

4 452 99 4

3 -453 98 5

456 3 29 1

-3 92 2345 32

22 23 -4000 -3

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download