ࡱ> pro @ -bjbjVV jr<r<$6666666J***8J**dJg<F+F+\+\+\+\+\+\+;;;;;;;$]=R?N <6+\+\+++ <66\+\+!<777+6\+6\+;7+;779669\+:+ 0`*a-T9;<7<0g<9,?1?9JJ6666?69\+p+7~+ +S\+\+\+ < <JJN Y4JJN Two-dimensional arrays Important: * Review the notes on 2-D arrays from your 1st year programming course, given by Brenda. * Review this years notes on 1-D arrays Important definitions: Loop: a programming structure to repeat something over and over again, using either while or for in Java. A variable: a storage box in a programming language. Each 2-D array consists of rows and columns of variables. 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: Heres 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. Heres 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 youre 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, youll 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, youll 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. Heres the program youll 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, thats 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 Id 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. // Dont forget two closing sets of curly brackets properly indented. // Youll 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. // // Heres 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! // Its more or less the same thing as you were doing before // with 1-D arrays, isnt it? Only a small step forward. // (If youre 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 youve 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, youll 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 users choice. The users 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: Youll need to use spaces or tab to separate the numbers in a given row and youll 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      PAGE 6 #PR  |eP>P',hE0qh,6B*CJOJQJ^JaJph#h -B*CJOJQJ^JaJph)hE0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J! t0644 laefh:;=CFGL  Ǽڸ˸˸|tmimb h8h-bh-b h8h8h8B* ph h-bh-bh-bB* phh-*B* phhNhhMB*ph3fhhMh-*B* phhhMhhMB* phhhMh-*hE hE B* phhE hAh4Hh8&6CJaJh8&6h8h CJaJ h h h h<h<h &<= Oyz\ ^`gd4H ^`gdgMgdhMgd-* ^`gd8&6gd8&6OTx}9byz"%fiڼڼڲƍzszsz hgMh hgMh4Hh4HB* phhGsGhzw1hgM5B* phhE hE 5B* H*phhE 5B* phh-*5B* phh4H5B* phhgM5B* phh 5B* phhzw1h-*5B* phh h 5h h 5B* phh-** d:pq-2_P^gdJ7 ^`gdp}H^gd8&6gd-*9:pqruz{}!'6͠zrzrghM)hp}HB* phhM)B* phhM)hB* phhhgMB* phhgMB* phhJ7B* phhh-*B* phhh-*B* phhhB* phhB* phhh-*hAhp}Hhh4HB* ph hgMh4H hgMh hgMhgM hgMh nC$6<=@BHLX_ekln!'MQTʿʿݻ~shdh4HhhJ7B* phhhB* phh-bB* phh-bh-bB*phhhhB*phhB*phhhJ7B*phh-bB*phhJ7hh-*B* phhhB* phhhAh-*h8B* phhh-*B* phhhAB* ph%PQVW  F ~ ^`gd ^`gd4H^gdgMgdJ7 ^`gdgM^gd; ^`gdgd-*^gdJ7 T  %+jҾȱҧҧҧҚ{qggq]h215B* phh5B* phh4H5B* phhJ7B*phhzw1h-*5B* phhJ75B* phh4HhJ75B* phhgM5B* phh4Hh-*5B* phh;5B* phh85B* phh4Hh5B* phh4Hh5B*phhhB*phhB* ph$  !!r!x!|!}!!!!!!!!!""""I"J"L"ŸwpiaiZipRHh_I5B* phh4HB* ph hJ7hJ7hJ7h nCH* hJ7h nC hJ7h4HhgM5B* phh4Hhbm5>*B* phh4H5>*B* phh4H5B* phhbm5B* phh;5B*phh4Hh5B*phh4Hh-*5B*phhzw1h-*5B* phhh5B*phh5B* phh-*5B* ph~ !P!r!s!!!!"J"M""""""#4#A#E##]gdNgd_Igd j ^`gd j^gd4Hgdgd-*L"M"Y"[""""""""""""""""""""##7#A#C#D#G#N#źvc%hNhNB*mH nH phsH tH hs h_IB* phh nCB* phhs h jB* phh8hg\h nCh jB*phh nCh_IB*phh_Ih jhh_IB* phh_I5B* phh21h_IB* \phh21h21B* \ph h21\h jh_I\h j5B* phN#Q#Z#y########-$1$4$}$~$$$$$$$$$$$$$$$$$$% %!%$%ּynyjc_h-* h nCh_IhgMh nChgMB*phh nChgMB*phh.hg\hp}Hhp}HB* ph h :h nC h :h jh jhJ7hJ7H* hJ7hJ7hJ7 hNhJ7%hNhNB*mH nH ph*sH tH %hNhNB*mH nH phsH tH +hNhN6B* ]mH nH phsH tH $###.$/$c$$$$$$$$$%!%$%&%'%%%%%gd-*gdgd. ^`gdp}Hgdg\gd j ^`gdJ7^gdJ7$%'%r%%%%%%%%%.&0&5&6&&''''' '¶΢}kZI h [Th nC5B*CJaJph h [Th6B*CJaJph#h<h_56B*CJ0aJ0phh [Th_B*phh [ThtB*CJaJphhhtB* phhtB* phh :B*CJaJphh~BB*CJaJphh"JB*CJaJphhtB*CJaJph hp}Hh"JhB*CJaJphh [Th [TB*CJaJphh-b%.&^&&&'''''[(\(x(y(((((((w)x).*/*U*V*+++gd-*^gdt '!'6'?'m'o'w'x'''\(](k(l(m(n((((((((((((%)F)Z)Ѩљљљљэ~maUhB*CJaJphhrSB*CJaJph h [Th nC5B*CJaJphh [Th [TB*CJaJphh nCB*CJaJphh [ThB*CJaJphhB*CJaJphh [TB*CJaJph h [Th_B*CJH*aJphh [Th nCB*CJaJphh [Th_B*CJaJph h [Th_5B*CJaJphZ)f)k)u)w)x)y)z))))))) **-*/*0*1*A*B*C*ôxl[J;h [ThhMB*CJaJph h [ThhM5B*CJaJph h [Th [TB*CJH*aJphhtB*CJaJphh [TB*CJH*aJphh [TB*CJaJph h [ThX5B*CJaJph h [Th nC5B*CJaJphh [ThXB*CJaJphh [Th_B*CJaJphhB*CJaJph hrSh6B*CJaJph hrSh}a6B*CJaJphC*H*M*O*S*T*U*V*X*+++,,,,,,,,,---- - - -----ijĤxtxtxtxtpf`fUfpthrS0JmHnHu h&z0Jjh&z0JUh&zh"jh"Uh [ThB*phh_B*ph h [Th [T5B*CJaJphh [Th_B*CJaJph h [Th5B*CJaJphh [ThB*CJaJph h [Th [TB*CJH*aJphh [TB*CJaJphh [ThXB*CJaJph+2,3,Q,R,`,q,,,,,,,,,,,,,,,,,,,----gd-*----gd-*. A!"#$%B$$If!vh5J!#vJ!:Vl t65J!G$$If!vh5J!#vJ!:Vl t65J!8@8 Normal_HmH sH tH DA@D Default Paragraph FontVi@V  Table Normal :V 44 la (k(No List 4@4 zw1Header  9r 4 @4 zw1Footer  9r .)@. zw1 Page Numberj@#j ~B Table Grid7:V0%j$+)*BCi*+56Z  HIo? @ I K x y R S /f<= Oyz\ d:pq-2_PQVW  F~PrsJM4AE./c!$&'.^[ \ x y w!x!."/"U"V"###2$3$Q$R$`$q$$$$$$$$$$$$$$$$%%%%%00x0x0x0x0x 0x 0  0x0x0x00x0x00x0x0x0x0x0x0x0x0x0x0x0x0x0x00x0x00x00 0 0x0 0x0 0x000x0 0x0 00000X000 0 0p0`00 0 00x0x0x0x0x00x0x0x00x0x0x0x0x000x000x0x0x0x00 0 0000x000x000x000x000x000x00x0x0x00x0x0000x0x0x0 0x0x0x00x0x00x0x0x0x0x0x0x0x0x00x0x000p0p00 000p0p00 000x0x00 000x0x00000000000000000000000000000000000000000000000000000000000000000000@0@0@0@0@0@0@000t HIo K pq-2_PQVW  F~PrsJM4AE%;00a|1|||;00;00;00;00;00;00;00;00;00;00;0 0h;00;00;00;00;00|1|||;00n;00HǗl;000l;00l;00l;00n;00l;00l;00l;00l;00l;00l;00l;00l;00l;00l;00l;00l;00l;00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{00l{000Rs  C 6L"N#$% 'Z)C*-!#%&()+,.012 oP~ #%+-- "$'*-/34-!N#%%9*urn:schemas-microsoft-com:office:smarttagsplace qx4 C  ?Btu L[GYZ!i!c#u#$$$$$$$$$$%%%%+`~ 6<Z`EKKRqy 4 D  ?Bim68!6=koX[\bSa*/5OY_x{MTGZY_lo. 5 ] d Z!j!""# #I#W###$$$$$$$$$$$$%%%%3333333333333333333333333333333333333333333333333333333333333333333333333333333333333)**? ? 4 ; < @ H J S S  yyqqzLXl!'MLLYDDEEG88#&\ ] %!F!Z!a!b!f!$$$$$$$$$$$%%%%4 E   L]QSTWLTGcdhiklpqwxyz{|8=$%%!F!Z!k!$$$$$$$$$$%%%%%t>vN)^`o(. ^`hH. pLp^p`LhH. @ @ ^@ `hH. ^`hH. L^`LhH. ^`hH. ^`hH. PLP^P`LhH.%t>v          @ @ihX l_.5xm[A811e'L "T$s$ 5&-*o+ -}B/P/zw18&6_B6.8~B{BC nCGsG4Hp}H_I|I"JgMPPrS S [TfTLO[W[g\_}a b-b_=cjzc j}jykbmnXomo pE0q+tIu8MytyB NNyBB/s J7 :8PM)&zwk0;W{<(hM9t{4,"\E k )R;21@ K %@l!l!l!l!%0@UnknownG:Ax Times New Roman5Symbol3& :Cx Arial?& Arial Black"1h uǂ{rCrC$4d$$2QHP?-*ArraysDowneyDowney Oh+'0X    ,8@HPArraysDowney Normal.dotDowney14Microsoft Word 10.0@40@S~@<@r՜.+,0 hp   Norwood C$ Arrays Title  !"#$%&'()*+,-./012345789:;<=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^`abcdefhijklmnqRoot Entry F԰`sData 61Table> @WordDocumentjSummaryInformation(_DocumentSummaryInformation8gCompObjj  FMicrosoft Word Document MSWordDocWord.Document.89q