Chapter 7 Arrays 269 which contains 3 rows, (Business web site)
Chapter 7 Arrays 269 which contains 3 rows, or arrays. The following for structure sets all the elements in the third row of array a to zero: for ( int col = 0; col < a[ 2 ].Length; col++ ) a[ 2 ][ col ] = 0; We specified the third row; therefore, we know that the first subscript is always 2(0 is the first row and 1 is the second row). The for loop varies only the second subscript (i.e., the column subscript). Notice the use of a[2].Length in the for structure s conditional expression. This statement demonstrates that each row of a is an array in itself, and therefore the program can access a typical array s properties, such as Length. Assuming the length of array a[2]is 4, the preceding for structure is equivalent to the assignment statements a[ 2 ][0 ] = 0; a[ 2 ][1 ] = 0; a[ 2 ][2 ] = 0; a[ 2 ][3 ] = 0; The following nested for structure determines the total of all the elements in array a. We use a.Length in the conditional expression of the outer for structure to determine the number of rows in a, in this case, 3. int total = 0; for ( int row = 0; row < a.Length; row++ ) for ( int col = 0; col < a[ row ].Length; col++ ) total += a[ row ][ col ]; The for structure totals the elements of the array one row at a time. The outer for structure begins by setting the row subscript to 0, so the elements of the first row may be totaled by the inner for structure. Then the outer for structure increments rowto 1, so the second row can be totaled. Finally, the outer for structure increments row to 2, so the third row can be totaled. The result can be displayed when the nested for structure terminates. The program in Fig. 7.15 performs several other array manipulations on 3-by-4 array grades. Each row of the array represents a student, and each column represents a grade on one of the four exams that the student took during the semester. The array manipulations are performed by four methods. Method Minimum (lines 64 76) determines the lowest grade of any student for the semester. Method Maximum (lines 79 91) determines the highest grade of any student for the semester. Method Average (lines 94 102) determines a particular student s semester average. Methods Minimum and Maximum use array grades and the variables students (number of rows in the array) and exams (number of columns in the array). Each method loops through array grades by using nested for structures. Consider the nested for structure from method Minimum (lines 68 73). The outer for structure sets i (i.e., the row subscript) to 0 so the elements of the first row can be compared with variable low- Grade in the body of the inner for structure. The inner for structure loops through the four grades of a particular row and compares each grade with lowGrade. If a grade is less than lowGrade, then lowGradeis set to that grade. The outer for structure then increments the row subscript by 1. The elements of the second row are compared with variable lowGrade. The outer for structure then increments the row subscript to 2. The elements
Check Tomcat Web Hosting services for best quality webspace to host your web application.