Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 22. Numerics > Exercises

22.9. Exercises

1.(*1.5) Write a function that behaves like apply() from §22.4.3, except that it is a nonmember function and accepts function objects.
2.(*1.5) Write a function that behaves like apply() from §22.4.3, except that it is a nonmember function, accepts function objects, and modifies its valarray argument.
3.(*2) Complete Slice_iter (§22.4.5). Take special care when defining the destructor.
4.(*1.5) Rewrite the program from §17.4.1.3 using accumulate().
5.(*2) Implement I/O operators << and >> for valarray. Implement a get_array() function that creates a valarray of a size specified as part of the input itself.
6.(*2.5) Define and implement a three-dimensional matrix with suitable operations.
7.(*2.5) Define and implement an n-dimensional matrix with suitable operations.
8.(*2.5) Implement a valarray-like class and implement + and * for it. Compare its performance to the performance of your C++ implementation’s valarray. Hint: Include x=0.5*(x+y)+z among your test cases and try it with a variety of sizes for the vectors x, y, and z.
9.(*3) Implement a Fortran-style array Fort_array where indices start from 1 rather than 0.
10.(*3) Implement Matrix using a vector< vector<double> > as the representation of the elements (rather than a pointer to a valarray).
11.(*2.5) Use compositors (§22.4.7) to implement efficient multidimensional subscripting using the [] notation. For example, v1[x], v2[x][y], v2[x], v3[x][y][z], v3[x][y], and v3[x] should all yield the appropriate elements and subarrays using a simple calculation of an index.
12.(*2) Generalize the idea from the program in §22.7 into a function that, given a generator as an argument, prints a simple graphical representation of its distribution that can be used as a crude visual check of the generator’s correctness.
13.(*1) If n is an int, what is the distribution of (double(rand())/RAND_MAX)*n?
14.(*2.5) Plot points in a square output area. The coordinate pairs for the points should be generated by Urand(N), where N is the number of pixels on a side of the output area. What does the output tell you about the distribution of numbers generated by Urand?
15.(*2) Implement a Normal distribution generator, Nrand.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial