Exercises

  1. 23.6 Investigate other compute-intensive calculations, then modify the example of Fig. 23.1 to perform a different compute-intensive calculation asynchronously.

  2. 23.7 Modify the example of Fig. 23.3 to process the results by using the array returned by the Task produced by Task method WhenAll.

  3. 23.8 Investigate other web services at a site like http://www.programmableweb.com. Locate a REST web service that returns XML, then modify the example of Fig. 23.4 to invoke the web service asynchronously using the methods of class HttpClient. Parse the results using LINQ to XML, then display the results as appropriate for the type of data returned.

  4. 23.9 You may have noticed that the prime-number example in Fig. 23.6 produces prime numbers rapidly. The asynchronous task in the example is probably not compute bound, but rather I/O bound—that is, the speed of displaying the results on the screen slows the program down, not the ability of the processor to determine the prime numbers. Reimplement the example of Fig. 23.6 with a compute bound algorithm such as the recursive Fibonacci calculation of Fig. 23.1. Try calculating all the Fibonacci values from 0 to 47. When you run the app, observe that as the Fibonacci numbers get higher, the calculation speed slows dramatically, slowing the updates of the Progress-Bar and the TextBox that shows the calculation results. Because this calculation is compute bound, the overall speed of the app will depend on your computer’s processor—you may want to decrease or increase the largest Fibonacci number the app calculates accordingly. For example, on our computer the Fibonacci of 47 takes a couple of minutes to calculate.

  5. 23.10 Visit https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes to learn more about the Sieve of Eratosthenes. The algorithm as implemented in Fig. 23.6 is inefficient, because it keeps eliminating multiples of primes even after the array already represents all primes up to the maximum entered by the user. The algorithm is complete once it eliminates the multiples of all primes that are less than or equal to the square root of the maximum value. Make the following modifications to Fig. 23.6:

    1. Update the algorithm to eliminate multiples of primes that are less than or equal to the square root of the maximum value.

    2. Modify the code that updates the ProgressBar and percentage Label so that they indicate the progress of the Sieve of Eratosthenes algorithm, rather than the progress of checking whether each number in the range is prime.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset