Archive for September, 2007

274 Arrays Chapter 7 In an array

Sunday, September 30th, 2007

274 Arrays Chapter 7 In an array of primitive data types, every element of the array contains one value of the declared data type of the array. In an array of a reference type, every element of the array is a reference to an object of the data type of the array. For example, every element of a string array is a reference to a stringand that reference has the value null by default. The elements of single-dimensional and rectangular arrays can be allocated and initialized in the array declaration by following the declaration with an equal sign and a comma-separated initializer list enclosed in braces ({ and }). A const variable must be declared and initialized in the same statement. Constants also are called named constants. They often are used to make a program more readable. Unlike its predecessors C and C++, .NET-compliant languages provide mechanisms to prevent accessing elements outside the bounds of the array. When a reference is made to a nonexistent element of an array, an IndexOutOfRangeException occurs. To pass an array argument to a method, specify the name of the array without any brackets. Although entire arrays are passed by reference, individual array elements of primitive data types are passed by value, as are simple variables. To pass an array element to a method, use the subscripted name of the array element as an argument in the method call. Sorting data (i.e., placing the data into a particular order, such as ascending or descending) is one of the most important computing applications. The chief virtue of the bubble sort is that it is easy to program. However, the bubble sort runs slowly, which becomes apparent when sorting large arrays. The linear search method works well for small or unsorted arrays. However, for large arrays, linear searching is inefficient. After each comparison, the binary search algorithm eliminates from consideration half the elements in the array being searched. The algorithm locates the middle array element and compares it to the search key. If they are equal, the search key has been found, and the subscript of that element is returned. Otherwise, the problem is reduced to searching half the array. If the search key is less than the middle array element, the first half of the array is searched; otherwise, the second half of the array is searched. The search continues until the search key is equal to the middle element of a subarray, or until the subarray consists of one element that is not equal to the search key (i.e., the search key is not found). The maximum number of comparisons needed for the binary search of any sorted array is the exponent of the first power of 2 that is greater than the number of elements in the array. There are two types of multiple-subscripted arrays rectangular and jagged. In general, an array with m rows and n columns is referred to as an m-by-n array. Multiple-subscripted arrays can be initialized in declarations, as can single-subscripted arrays. The compiler determines the number of columns in each row by counting the number of initializer values in the sub-initializer list for that row. Jagged arrays are maintained as arrays of arrays. Unlike rectangular arrays, rows in jagged arrays can be of different lengths. Many common array manipulations use for repetition structures. When used with one-dimensional arrays, foreach behaves like a for structure that iterates through the range of indices from 0 to the array s Length.
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

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

Saturday, September 29th, 2007

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.

272 Arrays Chapter 7 Fig. 7.15 Example using

Friday, September 28th, 2007

272 Arrays Chapter 7 Fig. 7.15 Example using double-subscripted arrays. (Part 3 of 3.) of the third row are compared with variable lowGrade. When execution of the nested structure is complete, lowGrade contains the smallest grade in the double-subscripted array. Method Maximum works similarly to method Minimum. Method Average takes one argument a single-subscripted array of test results for a particular student. When Averageis called (line 59), the argument grades[i] specifies that a particular row of the double-subscripted array grades is to be passed to Average. For example, the argument grades[1]represents the four values (a single- subscripted array of grades) stored in the second row of the double-subscripted array grades. Remember that a jagged two-dimensional array is an array with elements that are single-subscripted arrays. Method Average calculates the sum of the array elements, divides the total by the number of test results and then returns the floating-point result cast as a double value (line 101). 7.10 foreach Repetition Structure C# provides the foreach retition structure for iterating through values in data structures, such as arrays. When used with one-dimensional arrays, foreach behaves like a for structure that iterates through the range of indices from 0 to the array s Length. Instead of a counter, foreach uses a variable to represent the value of each element. The program in Fig. 7.16 uses the foreach structure to determine the minimum value in a two-dimensional array of grades. 1 // Fig. 7.16: ForEach.cs 2 // Demonstrating for/each structure. 3 using System; 4 5 class ForEach 6 { 7 // main entry point for the application 8 static void Main( string[] args ) 9 { Fig. 7.16 Using ForEach/Next with an array. (Part 1 of 2.)
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Cedant web hosting - Chapter 7 Arrays 271 53 54 outputLabel.Text +=

Thursday, September 27th, 2007

Chapter 7 Arrays 271 53 54 outputLabel.Text += “nnLowest grade: ” + Minimum() + 55 “nHighest grade: ” + Maximum() + “n”; 56 57 for ( int i = 0; i < students; i++ ) 58 outputLabel.Text += "nAverage for student " + i + " is " + 59 Average( grades[ i ] ); 60 61 } // end method showOutputButton_Click 62 63 // find minimum grade in grades array 64 public int Minimum() 65 { 66 int lowGrade = 100; 67 68 for ( int i = 0; i < students; i++ ) 69 70 for ( int j = 0; j < exams; j++ ) 71 72 if ( grades[ i ][ j ] < lowGrade ) 73 lowGrade = grades[ i ][ j ]; 74 75 return lowGrade; 76 } 77 78 // find maximum grade in grades array 79 public int Maximum() 80 { 81 int highGrade = 0; 82 83 for ( int i = 0; i < students; i++ ) 84 85 for ( int j = 0; j < exams; j++ ) 86 87 if ( grades[ i ][ j ] > highGrade ) 88 highGrade = grades[ i ][ j ]; 89 90 return highGrade; 91 } 92 93 // determine average grade for a particular student 94 public double Average( int[] setOfGrades ) 95 { 96 int total = 0; 97 98 for ( int i = 0; i < setOfGrades.Length; i++ ) 99 total += setOfGrades[ i ]; 100 101 return ( double ) total / setOfGrades.Length; 102 } 103 104 } // end class DoubleArray Fig. 7.15 Example using double-subscripted arrays. (Part 2 of 3.)
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

270 Arrays Chapter 7 // Fig. 7.15: DoubleArray.cs

Wednesday, September 26th, 2007

270 Arrays Chapter 7 // Fig. 7.15: DoubleArray.cs // Manipulating a double-subscripted array. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class DoubleArray : System.Windows.Forms.Form { private System.Windows.Forms.Button showOutputButton; private System.Windows.Forms.Label outputLabel; int[][] grades; int students, exams; // Visual Studio .NET generated code [STAThread] static void Main() { Application.Run( new DoubleArray() ); } private void showOutputButton_Click( object sender, System.EventArgs e ) { grades = new int[ 3 ][]; grades[ 0 ] = new int[]{ 77, 68, 86, 73 }; grades[ 1 ] = new int[]{ 96, 87, 89, 81 }; grades[ 2 ] = new int[]{ 70, 90, 86, 81 }; students = grades.Length; // number of students exams = grades[ 0 ].Length; // number of exams // line up column headings outputLabel.Text = ” “; // output the column headings for ( int i = 0; i < exams; i++ ) outputLabel.Text += "[" + i + "] "; // output the rows for ( int i = 0; i < students; i++ ) { outputLabel.Text += "ngrades[" + i + "] "; for ( int j = 0; j < exams; j++ ) outputLabel.Text += grades[ i ][ j ] + " "; } Fig. 7.15 Example using double-subscripted arrays. (Part 1 of 3.)
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Chapter 7 Arrays 269 which contains 3 rows, (Business web site)

Tuesday, September 25th, 2007

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.

268 Arrays Chapter 7 42 43 outputLabel.Text += (Ftp web hosting)

Thursday, September 20th, 2007

268 Arrays Chapter 7 42 43 outputLabel.Text += “n”; 44 } 45 46 outputLabel.Text += “nValues in array2 by row aren”; 47 48 // output values in array2 49 for ( int i = 0; i < array2.Length; i++ ) 50 { 51 for ( int j = 0; j < array2[ i ].Length; j++ ) 52 outputLabel.Text += array2[ i ][ j ] + " "; 53 54 outputLabel.Text += "n"; 55 } 56 57 } // end method showOutputButton_Click 58 } Fig. 7.14 Initializing multidimensional arrays. (Part 2 of 2.) The declaration of array1 (line 27) provides six initializers in two sublists. The first sublist initializes the first row of the array to the values 1, 2 and 3. The second sublist initializes the second row of the array to the values 4, 5 and 6. The declaration of array2 (line 30) creates a jagged array of 3 arrays (specified by the 3 in the first set of square brackets). Lines 31 33 initialize each subarray so that the first subarray contains the values 1 and 2, the second contains the value 3 and the last contains the values 4, 5 and 6. The for structure on lines 38 44 appends the elements of array1 to string output. Note the use of a nested for structure to output the rows of each double-subscripted array. In the nested for structures for array1, we use method GetLength to determine the number of elements in each dimension of the array. Line 38 determines the number of rows in the array by invoking array1.GetLength(0), and line 40 determines the number of columns in the array by invoking array1.GetLength( 1 ). Arrays with additional dimensions would require more deeply nested for loops to process. The nested for structures on lines 49 55 output the elements of jagged array array2. Recall that a jagged array is essentially an array that contains additional arrays as its elements. Line 49 uses the Length property of array2 to determine the number of rows in the jagged array. Line 51 determines the Length of each subarray with the expression array2[i].Length. Many common array manipulations use for repetition structures. For the remainder of this section, we will focus on manipulations of jagged arrays. Imagine a jagged array a,
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.

Web design online - Chapter 7 Arrays 267 // allocate and initialize

Wednesday, September 19th, 2007

Chapter 7 Arrays 267 // allocate and initialize elements in row 0 c[ 1 ] = new int[] { 3,4, 5 }; creates integer array c with row 0 (which is an array itself) containing two elements (1 and 2), and row 1containing three elements (3, 4 and 5). The Length property of each sub- array can be used to determine the size of each column. For the jagged array c, the size of the zeroth column is c[0].Length, which is 2. The application in Fig. 7.14 demonstrates the initialization of double-subscripted arrays in declarations and the use of nested for loops to traverse the arrays (i.e., to manipulate each array element). 1 // Fig. 7.14: TwoDimensionalArrays.cs 2 // Initializing two-dimensional arrays. 3 using System; 4 using System.Drawing; 5 using System.Collections; 6 using System.ComponentModel; 7 using System.Windows.Forms; 8 using System.Data; 9 10 public class TwoDimensionalArrays : System.Windows.Forms.Form 11 { 12 private System.Windows.Forms.Button showOutputButton; 13 private System.Windows.Forms.Label outputLabel; 14 15 // Visual Studio .NET generated code 16 17 [STAThread] 18 static void Main() 19 { 20 Application.Run( new TwoDimensionalArrays() ); 21 } 22 23 private void showOutputButton_Click( object sender, 24 System.EventArgs e ) 25 { 26 // declaration and initialization of rectangular array 27 int[,] array1 = newint[,] { { 1, 2, 3 }, { 4, 5, 6 } }; 28 29 // declaration and initialization of jagged array 30 int[][] array2 = new int[ 3 ][]; 31 array2[ 0 ] = newint[] { 1, 2 }; 32 array2[ 1 ] = new int[] { 3 }; 33 array2[ 2 ] = newint[] { 4, 5,6 }; 34 35 outputLabel.Text = “Values in array1 by row aren”; 36 37 // output values in array1 38 for ( int i = 0; i < array1.GetLength( 0 ); i++ ) 39 { 40 for ( int j = 0; j < array1.GetLength( 1 ); j++ ) 41 outputLabel.Text += array1[ i, j ] + " "; Fig. 7.14 Initializing multidimensional arrays. (Part 1 of 2.)
You want to have a cheap webhost for your apache application, then check apache web hosting services.

Free web hosting services - 266 Arrays Chapter 7 Column 0 Column 1

Tuesday, September 18th, 2007

266 Arrays Chapter 7 Column 0 Column 1 Column 2 Column 3 Row 0 Row 1 Row 2 a[0, 0] a[0, 1] a[0, 2] a[0, 3] a[1, 0] a[1, 1] a[1, 2] a[1, 3] a[2, 0] a[2, 1] a[2, 2] a[2, 3] Column index (or subscript) Row index (or subscript) Array name Fig. 7.13 Double-subscripted array with three rows and four columns. Every element in array a is identified in Fig. 7.13 by an element name of the form a[i,j], in which a is the name of the array, and i and j are the subscripts that uniquely identify the row and column of each element in a. Notice that the names of the elements in the first row all have a first subscript of 0; the names of the elements in the fourth column all have a second subscript of 3. Multiple-subscripted arrays can be initialized in declarations like single-subscripted arrays. A double-subscripted array b with two rows and two columns could be declared and initialized with int[,] b = new int[ 2, 2 ]; b[ 0, 0 ] =1; b[ 0, 1 ] =2; b[ 1, 0 ] =3; b[ 1, 1 ] =4; or this can be written on one line using an initializer list as shown below: int[,] b = { { 1, 2 }, { 3, 4 } }; The values are grouped by row in braces. Thus, 1 and 2 initialize b[0,0] and b[0,1], and 3 and 4 initialize b[1,0] and b[1,1]. The compiler determines the number of rows by counting the number of sub-initializer lists (represented by sets of braces) in the main initializer list. The compiler determines the number of columns in each row by counting the number of initializer values in the sub-initializer list for that row. Method GetLength returns the length of a particular array dimension. In the preceding example, b.GetLength(0)returns the length of the zeroth dimension of b, which is 2. Jagged arrays are maintained as arrays of arrays. Unlike in rectangular arrays, the arrays that compose jagged arrays can be of different lengths. The declaration int[][] c = new int[ 2 ][]; // allocate rows // allocate and initialize elements in row 0 c[ 0 ] = new int[] { 1,2 };
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

Web server logs - Chapter 7 Arrays 265 Fig. 7.12 Binary search

Monday, September 17th, 2007

Chapter 7 Arrays 265 Fig. 7.12 Binary search of a sorted array. (Part 4 of 4.) If key matches the middle element of a subarray (line 74), BinarySearch returns middle(the subscript of the current element), indicating that the value was found and the search is complete. If key does not match the middle element of a subarray, Binary- Search adjusts the low subscript or highsubscript (both declared in the method) so that a smaller subarray can be searched. If key is less than the middle element (line 76), the high subscript is set to middle-1, and the search continues on the elements from low to middle-1. If key is greater than the middle element (line 78), the low subscript is set to middle+1, and the search continues on the elements from middle+1to high. These comparisons occur in the nested if/else structure on lines 74 79. The program uses a 15-element array. The first power of 2 greater than the number of array elements is 16 (24) so at most four comparisons are required to find the key. To illustrate this concept, method BinarySearch calls method BuildOutput (lines 87 106) to output each subarray during the binary search process. BuildOutput marks the middle element in each subarray with an asterisk (*) to indicate the element with which the key is compared. Each search in this example results in a maximum of four lines of output one per comparison. Note that the .NET framework includes a built-in array- searching capability that implements the binary-search algorithm. To search for the key 7 in the sorted array a in Fig. 7.12, you can use the statement Array.BinarySearch( a, 7 ); 7.9 Multiple-Subscripted Arrays So far we have studied single-subscripted (or one-dimensional) arrays i.e., those that contain single lists of values. In this section, we introduce multiple-subscripted (often called multidimensional) arrays. Such arrays require two or more subscripts to identify particular elements. Arrays that require two subscripts to identify a particular element commonly are called double-subscripted arrays. We concentrate on double-subscripted arrays (often called two-dimensional arrays). There are two types of multiple-subscripted arrays rectangular and jagged. Rectangular arrays with two subscripts often represent tables of values consisting of information arranged in rows and columns, where each row is the same size, and each column is the same size. To identify a particular table element, we must specify the two subscripts by convention, the first identifies the element s row and the second identifies the element s column. Multiple-subscripted arrays can have more than two subscripts. Figure 7.13 illustrates a double-subscripted array, a, containing three rows and four columns (i.e., a 3-by-4 array). An array with m rows and n columns is called an m-by-n array.
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.