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

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.

Leave a Reply