Archive for September, 2007

Ipower web hosting - 254 Arrays Chapter 7 // Fig. 7.9: ArrayReferenceTest.cs

Friday, September 7th, 2007

254 Arrays Chapter 7 // Fig. 7.9: ArrayReferenceTest.cs // Testing the effects of passing array references // by value and by reference. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class ArrayReferenceTest : System.Windows.Forms.Form { private System.Windows.Forms.Label outputLabel; private System.Windows.Forms.Button showOutputButton; [STAThread] static void Main() { Application.Run( new ArrayReferenceTest() ); } private void showOutputButton_Click( object sender, System.EventArgs e ) { // create and initialize firstArray int[] firstArray = { 1, 2, 3 }; // copy firstArray reference int[] firstArrayCopy = firstArray; outputLabel.Text = “Test passing firstArray reference by value”; outputLabel.Text += “nnContents of firstArray ” + “before calling FirstDouble:nt”; // print contents of firstArray for ( int i = 0; i < firstArray.Length; i++ ) outputLabel.Text += firstArray[ i ] + " "; // pass reference firstArray by value to FirstDouble FirstDouble( firstArray ); outputLabel.Text += "nnContents of firstArray after " + "calling FirstDoublent"; // print contents of firstArray for ( int i = 0; i < firstArray.Length; i++ ) outputLabel.Text += firstArray[ i ] + " "; Fig. 7.9 Passing an array reference by value and by reference (Part 1 of 3.).
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Chapter 7 Arrays 253 7.6 Passing Arrays by (Web hosting servers)

Thursday, September 6th, 2007

Chapter 7 Arrays 253 7.6 Passing Arrays by Value and by Reference In C#, a variable that stores an object, such as an array, does not actually store the object itself. Instead, such a variable stores a reference to the object (i.e., the location in the computer s memory where the object itself is stored). The distinction between reference variables and primitive data type variables raises some subtle issues that programmers must understand to create secure, stable programs. When a program passes an argument to a method, the called method receives a copy of that argument s value. Changes to the local copy do not affect the original variable that the program passed to the method. If the argument is of a reference type, the method makes a local copy of the reference itself, not a copy of the actual object to which the reference refers. The local copy of the reference also refers to the original object in memory. Thus, reference types are always passed by reference, which means that changes to those objects in called methods affect the original objects in memory. Performance Tip 7.1 Passing arrays and other objects by reference makes sense for performance reasons. If arrays were passed by value, a copy of each element would be passed. For large, frequently passed arrays, this would waste time and would consume considerable storage for the copies of the arrays both of these problems cause poor performance. C# also allows methods to pass references with keyword ref. This is a subtle capability, which, if misused, can lead to problems. For instance, when a reference-type object like an array is passed with ref, the called method actually gains control over the passed reference itself, allowing the called method to replace the original reference in the caller with a different object or even with null. Such behavior can lead to unpredictable effects, which can be disastrous in mission-critical applications. The program in Fig. 7.9 demonstrates the subtle difference between passing a reference by value and passing a reference with keyword ref. Lines 26 and 29 declare two integer array variables, firstArray and firstArray- Copy (we make the copy so we can determine whether reference firstArray gets overwritten). Line 26 initializes firstArray with the values 1, 2 and 3. The assignment statement on line 29 copies reference firstArray to variable firstArrayCopy, causing these variables to reference the same array object in memory. The for structure on lines 38 39 prints the contents of firstArray before it is passed to method First- Double (line 42) so we can verify that this array is passed by reference (i.e., the called method indeed changes the array s contents). The for structure in method FirstDouble (lines 99 100) multiplies the values of all the elements in the array by 2. Line 103 allocates a new array containing the values 11, 12 and 13; the reference for this array then is assigned to parameter array (in an attempt to overwrite reference firstArray this, of course, will not happen, because the reference was passed by value). After method FirstDouble executes, the for structure on lines 48 49 prints the contents of firstArray, demonstrating that the values of the elements have been changed by the method (and confirming that in C# arrays are always passed by reference). The if/else structure on lines 52 57 uses the == operator to compare references firstArray (which we just attempted to overwrite) and firstArrayCopy. The expression on line 40 evaluates to true if the operands to binary operator == indeed reference the same object. In this case, the object represented is the array allocated in line 26 not the array allocated in method FirstDouble (line 103).
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

252 Arrays Chapter 7 54 55 // method (Starting a web site)

Thursday, September 6th, 2007

252 Arrays Chapter 7 54 55 // method modifies the array it receives, 56 // original will be modified 57 public void ModifyArray( int[] b ) 58 { 59 for ( int j = 0; j < b.Length; j++ ) 60 b[ j ] *= 2; 61 } 62 63 // method modifies the integer passed to it 64 // original will not be modified 65 public void ModifyElement( int e ) 66 { 67 outputLabel.Text += 68 "nvalue received in ModifyElement: " + e; 69 70 e *= 2; 71 72 outputLabel.Text += 73 "nvalue calculated in ModifyElement: " + e; 74 } 75 } Fig. 7.8 Passing arrays and individual array elements to methods. (Part 2 of 2.) To show the value of a[3] before the call to ModifyElement, lines 44 46 append the value of a[3] (and other information) to outputLabel.Text. Line 44 invokes method ModifyElement and passes a[3]. Remember that a[3] is a single int value in the array a. Also, remember that values of primitive types always are passed to methods by value. Therefore, a copy of a[3] is passed. Method ModifyElementmultiplies its argument by 2 and stores the result in its parameter e. The parameter of ModifyElement is a local variable, so when the method terminates, the local variable is destroyed. Thus, when control is returned to PassArray, the unmodified value of a[3] is appended to the outputLabel.Text (line 51 52).
Check Tomcat Web Hosting services for best quality webspace to host your web application.

Chapter 7 Arrays 251 // (Web site development) Fig. 7.8: PassArray.cs

Wednesday, September 5th, 2007

Chapter 7 Arrays 251 // Fig. 7.8: PassArray.cs // Passing arrays and individual elements to methods. using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class PassArray : System.Windows.Forms.Form { private System.Windows.Forms.Button showOutputButton; private System.Windows.Forms.Label outputLabel; // Visual Studio .NET generated code [STAThread] static void Main() { Application.Run( new PassArray() ); } private void showOutputButton_Click( object sender, System.EventArgs e ) { int[] a = { 1, 2, 3, 4, 5 }; outputLabel.Text = “Effects of passing entire array ” + “call-by-reference:nnThe values of the original ” + “array are:nt”; for ( int i = 0; i < a.Length; i++ ) outputLabel.Text += " " + a[ i ]; ModifyArray( a ); // array is passed by reference outputLabel.Text += "nnThe values of the modified array are:nt"; // display elements of array a for ( int i = 0; i < a.Length; i++ ) outputLabel.Text += " " + a[ i ]; outputLabel.Text += "nnEffects of passing array " + "element call-by-value:nna[ 3 ] before " + "ModifyElement: " + a[ 3 ]; // array element passed call-by-value ModifyElement( a[ 3 ] ); outputLabel.Text += "na[ 3 ] after ModifyElement: " + a[ 3 ]; } Fig. 7.8 Passing arrays and individual array elements to methods. (Part 1 of 2.)
Check Tomcat Web Hosting services for best quality webspace to host your web application.

250 Arrays Chapter 7 Common Programming Error 7.4 (Web design programs)

Tuesday, September 4th, 2007

250 Arrays Chapter 7 Common Programming Error 7.4 Referring to an element outside the array bounds is a logic error. Testing and Debugging Tip 7.3 When looping through an array, the array subscript never should go below 0 and should always be less than the total number of elements in the array (one less than the length of the array). The loop-terminating condition should prevent accessing elements outside this range. Testing and Debugging Tip 7.4 Programs should validate the correctness of all input values to prevent erroneous information from affecting a program s calculations. 7.5 Passing Arrays to Methods To pass an array argument to a method, specify the name of the array without using brackets. For example, if array hourlyTemperatures declared as int[] hourlyTemperatures = new int[ 24 ]; the method call ModifyArray( hourlyTemperatures ); passes array hourlyTemperatures to method ModifyArray. Every array object knows its own size (via the Length instance variable), so when we pass an array object into a method, we do not pass the size of the array as an argument separately. Although entire arrays are passed by reference, individual array elements of primitive data types are passed by value, the same way as simple variables are. (The objects referred to by individual elements of a nonprimitive-type array are still passed by reference.) Such simple single pieces of data are sometimes called scalars or scalar quantities. To pass an array element to a method, use the subscripted name of the array element as an argument in the method call. For a method to receive an array through a method call, the method s parameter list must specify that an array will be received. For example, the method header for method ModifyArray might be written as public void ModifyArray( int[] b ) indicating that ModifyArray expects to receive an integer array in parameter b. Arrays are passed by reference; when the called method uses the array name b, it refers to the actual array in the caller (array hourlyTemperatures). The application in Fig. 7.8 demonstrates the difference between passing an entire array and passing an array element. The forloop on lines 32 33 appends the five elements of integer array ato the Text property of outputLabel. Line 33 invokes method ModifyArray and passes to it array a. Method ModifyArray multiplies each element by 2. To illustrate that array a s elements were modified, the for loop on lines 41 42 appends the five elements of integer array a to the Text property of outputLabel. As the screen capture indicates, the elements of a are modified by ModifyArray.
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.

Best web hosting - Chapter 7 Arrays 249 The for loop (lines

Monday, September 3rd, 2007

Chapter 7 Arrays 249 The for loop (lines 20 21) takes the responses from the array response one at a time and increments one of the 10 counters in the frequency array (frequency[1] to frequency[10]). The key statement in the loop is on line 21, which increments the appropriate counter in the frequency array, depending on the value of element responses[ answer]. Let us consider several iterations of the for loop. When counter answer is 0, responses[answer] is the value of the first element of array responses (i.e., 1). In this case, the program interprets ++frequency[responses[answer]]; as ++frequency[1];, which increments array element one. In evaluating the expression, start with the value in the innermost set of square brackets (answer). Once you know the value of answer, plug that value into the expression and evaluate the next outer set of square brackets (responses[answer]). Use that value as the subscript for the frequency array to determine which counter to increment. When answeris 1, responses[answer]is the value of the second element of array responses (i.e., 2), so the program interprets ++frequency[ responses[ answer ] ]; as ++frequency[2];, which increments array element two (the third element of the array). When answeris 2, responses[answer] is the value of the third element of array responses (i.e., 6), so the program interprets ++frequency[ responses[ answer ] ]; as ++frequency[6];, which increments array element six (the seventh element of the array) and so on. Note that, regardless of the number of responses processed in the survey, only an 11-element array is required (ignoring element zero) to summarize the results, because all the response values are between 1 and 10, and the subscript values for an 11-element array are 0 10. The results are correct, because the elements of the frequency array were initialized to zero when the array was allocated with new. If the data contained invalid values, such as 13, the program would attempt to add 1to frequency[13]. This is outside the bounds of the array. In the C and C++ programming languages, no checks are performed to prevent programs from reading data outside the bounds of arrays. At execution time, the program would walk past the end of the array to where element number 13 would be located and add 1 to whatever data are stored at that location in memory. This could potentially modify another variable in the program or even result in premature program termination. The .NET framework provides mechanisms to prevent accessing elements outside the bounds of arrays. Testing and Debugging Tip 7.1 When a C# program executes, array element subscripts are checked for validity (i.e., all subscripts must be greater than or equal to 0 and less than the length of the array). Testing and Debugging Tip 7.2 Exceptions indicate when errors occur in programs. Programmers can write code to recover from exceptions and continue program execution instead of terminating the program abnormally. When an invalid array reference occurs, C# generates an IndexOutOfRange- Exception exception. We discuss exceptions in more detail in Chapter 11, Exception Handling.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.

248 Arrays Chapter 7 1 // Fig. 7.5: (Free php web host)

Sunday, September 2nd, 2007

248 Arrays Chapter 7 1 // Fig. 7.5: StudentPoll.cs 2 // A student poll program. 3 4 using System; 5 using System.Windows.Forms; 6 7 class StudentPoll 8 { 9 // main entry point for application 10 static void Main( string[] args ) 11 { 12 int[] responses = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 13 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 14 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 }; 15 16 int[] frequency = new int[ 11 ]; 17 string output = “”; 18 19 // increment the frequency for each response 20 for ( int answer = 0; answer < responses.Length; answer++ ) 21 ++frequency[ responses[ answer ] ]; 22 23 output += "RatingtFrequencyn"; 24 25 // output results 26 for ( int rating = 1; rating < frequency.Length; rating++ ) 27 output += rating + "t" + frequency[ rating ] + "n"; 28 29 MessageBox.Show( output, "Student poll program", 30 MessageBoxButtons.OK, MessageBoxIcon.Information ); 31 32 } // end method Main 33 34 } // end class StudentPoll Fig. 7.7 Simple student-poll analysis program. Good Programming Practice 7.1 Strive for program clarity. It is sometimes worthwhile to trade off the most efficient use of memory or processor time for writing clearer programs.
You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.

Web design seattle - Chapter 7 Arrays 247 96 97 } //

Saturday, September 1st, 2007

Chapter 7 Arrays 247 96 97 } // end class RollDie Results after one roll Results after fifty rolls Fig. 7.6 Using arrays to eliminate a switch structure. (Part 3 of 3.) 7.4.5 Using Arrays to Analyze Survey Results Our next example uses arrays to summarize the results of data collected in a survey. Consider the following problem statement: Forty students were asked to rate the quality of the food in the student cafeteria on a scale of 1 to 10, with 1 being awful and 10 being excellent. Place the 40 responses in an integer array and summarize the frequency for each rating. This is a typical array processing application (Fig. 7.7). We wish to summarize the number of responses of each type (i.e., 1 10). The array responses is a 40-element integer array of the students responses to the survey. We use an 11-element array frequency to count the number of occurrences of each response. We ignore the first element, frequency[0], because it is more logical to have a response of 1 increment frequency[1] than frequency[0]. We can use each response directly as a subscript on the frequencyarray. Each element of the array is used as a counter for one of the survey responses.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.