ࡱ> UWT bjbj88 4TRRi<$$8aL-r4,,,,,,,,.@1,,#4,###4,#,##r&T' I5'{,,0-C'1#1'#':)`,,#-1$+ O:   CSE 1321L: Programming and Problem Solving I Lab Lab 13 Multi-Dimensional Arrays Exercise #1: Develop a program that prints out the sum of each column of a two-dimensional array. The program defines method sumColumn() takes a two-dimensional array of integers and returns a single-dimensional array that stores the sum of columns of the passed array. The program main method prompts the user to enter a 3-by-4 array, prints out the array, and then calls method sumColumns(). Finally, it prints out the array retuned by method sumColumns(). Document your code, and organize and space the outputs properly as shown below. C++ students: instead of asking the user for input, you must read it from a file. See appendix for more information. Sample run 1: Enter a value: 9 Enter a value: 1 Enter a value: 2 Enter a value: 4 . . . Enter a value: 3 The entered matrix: 9 1 2 4 2 2 8 0 3 3 3 3 Sum of column 0 is 14 Sum of column 1 is 6 Sum of column 2 is 13 Sum of column 3 is 7 Sample run 2: [SIMILAR FORMAT OF INPUT ABOVE, BUT DIFFERENT NUMBERS] The entered matrix: 10 10 10 10 1 1 1 1 50 50 50 50 Sum of column 0 is 61 Sum of column 1 is 61 Sum of column 2 is 61 Sum of column 3 is 61 Exercise #2: Develop a program that prints out the location of the largest value in a two-dimensional array. The largest values may appear more than once in the array, so you must only print out the first instance. The program defines method locateLargest() that takes a two-dimensional array of integers and returns the location (row index and column index) of the first largest value as a single-dimensional array. The program main method prompts the user to enter a 3-by-4 matrix, prints out the matrix, and then calls method locateLargest(). Finally, it prints out the array returned by method locateLargest(). Document your code, and organize and space the outputs properly as shown below. Sample run 1: [SIMILAR FORMAT OF INPUT ABOVE, BUT DIFFERENT NUMBERS] The entered matrix: 9 1 2 4 2 11 18 20 3 20 3 12 First largest value is located at row 1 and column 3 Sample run 2: [SIMILAR FORMAT OF INPUT ABOVE, BUT DIFFERENT NUMBERS] The entered matrix: 19 11 22 44 29 51 81 20 23 90 45 90 First largest value is located at row 2 and column 1 Sample run 3: [SIMILAR FORMAT OF INPUT ABOVE, BUT DIFFERENT NUMBERS] The entered matrix: 89 11 22 44 29 51 80 20 33 10 45 10 First largest value is located at row 0 and column 0 Exercise #3: Develop a program (name it AddMatrices) that adds two matrices. The matrices must of the same size. The program defines method Addition() that takes two two-dimensional arrays of integers and returns their addition as a two-dimensional array. The program main method defines two 3-by-3 arrays of type integer. The method prompts the user to initialize the arrays. Then it calls method Addition(). Finally, it prints out the array retuned by method Addition(). Document your code, and organize and space the outputs properly as shown below. Sample run 1: Matrix A: 2 1 2 7 1 8 3 20 3 Matrix B: 1 1 1 1 1 1 1 1 1 A + B: 3 2 3 8 2 9 4 21 4 Sample run 2: Matrix A: 2 2 2 2 2 2 2 2 2 Matrix B: 2 2 2 2 2 2 2 2 2 A + B: 4 4 4 4 4 4 4 4 4 Instructions: 1. Programs must be working correctly. 2. Programs must be completed and checked before working the assignment. 3. Programs must be checked by the end of the designated lab session. Appendix Referencing row or column length C# int[][] grid = new int[4][5]; To get the row length use arrayname.GetLength(0) E.g.: grid.GetLength(0) To get the column length use arrayname.GetLength(1) E.g.: grid.GetLength(1) Java: int[][] grid = new int[4][5]; To get the row length use arrayname.length E.g.: grid.length To get the column length use arrayname[0].length E.g.: grid[0].length Appendix for C++ Students One of the most common (and simplest) file formats for storing data is comma separated values or .csv files. You can make these in Microsoft Excel or something even simpler like Notepad or TextEdit. They look like this: 1, 2, 3 4, 5, 6 7, 8, 9 Each data element is separated from the others by comma; note that theres no comma at the end of the line. Youve probably used getline a lot now, but theres a variation of it that you probably havent used. If we pass it three parameters instead of two, the third represents the delimiter between the data elements which, in this case, is the comma. However, getline gives us a string, not a number so we have to convert it to a number using stoi (or string to int). The code below reads from a file called matrices.csv which is located in the Debug folder of the project (so it can be found). Note: make this file before coding. #include #include #include using namespace std; void main() { // Create a filestream object just like last lab. fstream fs; // Open the file for reading using in. fs.open("matrices.csv", fstream::in); // When we read in from a file, it's a string (s). // We have to convert that string to a number (i). string s; int i; int sum = 0; // While we can read a row/string from the file... continue processing each row while (getline(fs, s, ',')) { // Get the first string, reading up until a comma (1st) i = stoi(s); sum += i; // Convert it to an int and add to sum cout << i <<"|"; // Print it out getline(fs, s, ','); // Get the next value, reading until a comma (2nd) i = stoi(s); sum += i; // Convert it to an int... cout << i << "|"; // Print it out getline(fs, s); // Get the remainder of the string (3rd) i = stoi(s); sum += i; // Convert it to an int... cout << i << endl; } cout << sum << endl; // Close the file fs.close(); cin >> s; }     PAGE  Page  PAGE 1 of  NUMPAGES 4  3689:RSTUaַydO7/hsfh!n%5>*B*CJOJQJ^JaJph)h'Nh!n%B*CJOJQJ^JaJph)h'Nh+B*CJOJQJ^JaJphh=CJOJQJ^JaJ huhlz2CJOJQJ^JaJ hhN]CJOJQJ^JaJhT{CJOJQJ^JaJ hh!CJOJQJ^JaJh;CJOJQJ^JaJh+CJOJQJ^JaJhYCJOJQJ^JaJh?CJOJQJ^JaJ129:STU   * ; = ? A R S g y h^hgd'Ngd!n%gdz$a$gd$a$gd;alyc   ! o q``qOq hsfhsfCJOJQJ^JaJ hsfh:CJOJQJ^JaJ#hsfh'N6CJOJQJ^JaJ hsfh'NCJOJQJ^JaJ,hsfh'NB*CJOJQJ\^JaJph)hsfh:B*CJOJQJ^JaJph)hsfh'NB*CJOJQJ^JaJph)hsfh!n%B*CJOJQJ^JaJph)hsfh0 B*CJOJQJ^JaJpho S g h i j k l n o q r s t u y z { | ~  tbPbPPbtbPbPbP#hNJB*CJOJQJ^JaJph#h2B*CJOJQJ^JaJph#h0 B*CJOJQJ^JaJph#h'NB*CJOJQJ^JaJph#hsfB*CJOJQJ^JaJph,h'Nh!n%>*B*CJOJQJ^JaJph&h'NB*CJOJQJ\^JaJph,hsfh'NB*CJOJQJ\^JaJph&hsfB*CJOJQJ\^JaJph   > R S U V \ ] _ ` e f i j n o s t w x | }  ۷ɷ۷ۥyg#hsfB*CJOJQJ^JaJph,h'Nh!n%>*B*CJOJQJ^JaJph)h'Nh!n%B*CJOJQJ^JaJph#ho4bB*CJOJQJ^JaJph#hNJB*CJOJQJ^JaJph#h2B*CJ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dTyL`MvL$r3IȨc4Euиid(|PZX6` 4Օem5}(-z0Y@pE4g6ʲȸ"WABJ+_JCz%YA$BO6"#Zaol ijK.Joegzmo1QT5#QQt1JK:ꬌvVhglzPnZvޮ{&iv3fXXޮk!M;|.ے]k>2~{QLfPweXjv1jv&~kv+neN`]04_+U՗;{ 1K*J%|zl&jO"DqkH}]^J*^ӫU:~Y~>uË(*/]۽%UTq! :[q*f\Na+hto\B~3ZNUתIn5}b+壈W:PK! ѐ'theme/theme/_rels/themeManager.xml.relsM 0wooӺ&݈Э5 6?$Q ,.aic21h:qm@RN;d`o7gK(M&$R(.1r'JЊT8V"AȻHu}|$b{P8g/]QAsم(#L[PK-!pO[Content_Types].xmlPK-!֧6 -_rels/.relsPK-!kytheme/theme/themeManager.xmlPK-!o(ʾtheme/theme/theme1.xmlPK-! ѐ' theme/theme/_rels/themeManager.xml.relsPK] T ;;;>ao jDoq "#%'( ;!$&) $&+68>!!@ @H 0(  0( lߧlߧ B S  ?>K) 4 jm} '*:=`pwhoU\{ =DU\GNGHKO[\ru{ !'+6:PX]`iklnoqrtu}>L r  jn '+`pwQSoq;=bd<@DGx{ "$&';=NP[]fghiklnoqrtu3333333333333333333CF2y3c.Ύou lqx:^`o( ^`hH. pL^p`LhH. @ ^@ `hH. ^`hH. L^`LhH. ^`hH. ^`hH. PL^P`LhH.^`o( ^`hH. pL^p`LhH. @ ^@ `hH. ^`hH. L^`LhH. ^`hH. ^`hH. PL^P`LhH.k^`ko( ^`hH. pL^p`LhH. @ ^@ `hH. ^`hH. L^`LhH. ^`hH. ^`hH. PL^P`LhH.^`o( ^`hH. pL^p`LhH. @ ^@ `hH. ^`hH. L^`LhH. ^`hH. ^`hH. PL^P`LhH.oCy3c lq         x                x        {@ C* Q+f f MP!P~=B2"#jo=fMP!2"8M"{@ !<}D&b8M&fxOqUkC* QbDQ&bNvo=f{$g{$gqUkbDQpNvG)H1&=qG7O3u{} Up 0 gU D*;,QCj=\07Sc1N];AR=$ !FB!1$!n%~&Q''(x([)B*K-.--.%0+12lz2I45.};f<=k1=vAM6A6B!B"F6kLNbN'NwNYXi%Fee&R3Tq*sf>s= u"NJEPxUj#E!Hr [A"R $no9,j@N 0F>Ds*TiUo$[zme7cm!-V18"V:@ VWc0%+BX,o1?9 kfqOoreTxl J}eqG+UOA]]ik@s}@UnknownG*Ax Times New Roman5Symbol3 *Cx Arial? *Cx Courier New9  @ ConsolasA$BCambria Math"1hT{T{f~ *~ *x24^^ 3qHP ?m2!xx CS 2301/03  Spring 2010hhaddadMicrosoft Office User     Oh+'0  ,8 \ h t 'CS 2301/03 Spring 2010hhaddad Normal.dotmMicrosoft Office User2Microsoft Macintosh Word@@r@i @i ~ ՜.+,0 hp  ' Microsoft* ^ CS 2301/03 Spring 2010 Title  !"#$%&'()*,-./0123456789:;<=>?@ABCEFGHIJKMNOPQRSVRoot Entry F` X1Table+1WordDocument4TSummaryInformation(DDocumentSummaryInformation8LCompObjr  F Microsoft Word 97-2004 Document MSWordDocWord.Document.89q