Chapter 7 Arrays 273 10 int[,] gradeArray = (Web server hosting)

Chapter 7 Arrays 273 10 int[,] gradeArray = { { 77, 68, 86, 73 }, 11 { 98, 87, 89, 81 }, { 70, 90, 86, 81 } }; 12 13 int lowGrade = 100; 14 15 foreach ( int grade in gradeArray ) 16 { 17 if ( grade < lowGrade ) 18 lowGrade = grade; 19 } 20 21 Console.WriteLine( "The minimum grade is: " + lowGrade ); 22 } 23 } The minimum grade is: 68 Fig. 7.16 Using ForEach/Next with an array. (Part 2 of 2.) The header of the foreach structure (line 15) specifies a variable, grade, and an array, gradeArray. The foreach structure iterates through all elements in grade- Array, sequentially assigning each value to variable grade. Line 15 compares each value to variable lowGrade, which stores the lowest grade in the array. For rectangular arrays, the repetition of the foreach structure begins with the element whose indices are all zero, then iterates through all possible combinations of indices, incrementing the rightmost index first. When the rightmost index reaches its upper bound, it is reset to zero, and the index to the left of it is incremented by 1. In this case, grade takes the values as they are ordered in the initializer list in lines 10 11. When all the grades have been processed, lowGrade is displayed (line 21). Although many array calculations are handled best with a counter, foreach is useful when the indices of the elements are not important. The foreach structure is particularly useful for looping through arrays of objects, as we discuss in Chapter 10, Object-Oriented Programming: Polymorphism. SUMMARY An array is a group of contiguous memory locations that all have the same name and type. To refer to a particular location or element in an array, specify the name of the array and the position number of the element within the array. The first element in every array is the zeroth element (i.e., element 0). The position number in square brackets is more formally called a subscript (or an index). This number must be an integer or an integer expression. To reference the ith element of a single-dimensional array, use i-1 as the index. The brackets that enclose the subscript of an array are operators that have the same level of precedence as parentheses. When arrays are allocated, the elements are initialized to zero for the numeric primitive-data-type variables, to false for bool variables or to null for reference types. Arrays may be declared to contain most data types.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

Leave a Reply