I’m trying to solve exercises 8 and 9 from B. Stoustrup’s book’s, “Programming — Principles and Practice Using C++”, eighth chapter.
Exercise 8 involves writing a function named randint() that generates a random integer in the range [0;MAXINT]. What is MAXINT supposed to be? Is it the greater number possible to store in an int? Is it an argument to the function? The author doesn’t state it. The only hint given is: “The Art of Programming, Knuth, vol. 2″. That book describes an algorithm called the linear congruential method, which involves 4 parameters (see linked Wikipedia article).
Exercise 9 involves using the randint(int a, int b) function from exercise 8 to generate a random integer in the range [a;b].
Now I could write a single function implementing the linear congruential method, with 4 arguments, but that’s not what the author wants. I’m trying to understand WHAT he wants, and wondering if someone solved these exercises. Unfortunately there’s no solution to these exercises on the author’s website.
Thanks for your suggestions.
There is nothing in standard C++ called
MAXINT.There is a macro
INT_MAX, defined in<climits>(<limits.h>in C); it expands to a constant expression whose value is the maximum value of typeint. Ifintis 32 bits, for example,INT_MAXis probably2147483647.Either
MAXINTis described elsewhere in the book (check the index and/or a few pages preceding the exercise), or it’s an error and Stroustrup meant to refer toINT_MAX.If you can’t find out where this
MAXINTis defined, you can probably just substituteINT_MAX.