The number of lines added is a linear function of the numer of sides on the dice being rolled. For example, for N-sided dice, the possible values are 2, 3, ..., 2N, which is (roughly) 2N different values --- the number of values is a linear function of N. The number of statements added is also a linear function of N since statements are added for initialization, update, and print, for a rough total of 3 * 2N statements which is still linear.
When using a vector as in Program 8.2 only one constant, DICE_SIDES needs to be changed to simulate 12 sided dice.
int sum = d.Roll() + d.Roll();
to
int sum = d.Roll() + d.Roll() + d.Roll();
0 no title 0 no title 0 no title 0 no title 0 no title 0 no title 0 no title 0 no title 0 no title 0 no titleboth before and after shuffling, so you won't be able to tell if shuffling works.
2 + 4 + 8 + 16 + 32 + 64 + 128 + 256elements before allocating the final 512. This results in a total of 510 + 512 elements or 1,022 elements.
When the capacity is 16,384 the vector will have allocated
2 + 4 + 8 + ... + 4,096 + 8,192If all the values from 1 + 2 + ... + 8192 are added together, the sum is one less than the next power of two, or 16,383. Since the 1 isn't included (the first vector size is two), the total is 16,382 + 16,384 = 32,766 elements.
1 + 2 + 3 + 4 + ... + 32 = 32(33)/2 = 528When the capacitiy is 128 the total number of elements allocated is 128(129)/2 = 8,256 elements. When the capacity is 16,384 the total number of elements allocated is 16384(16385)/2 = 134,225,920 --- compare this with the 32,766 elements allocated in the previous problem.
The value returned for the string "apple" is 0, this is the correct value since "apple" belongs as the first element.
The value of k becomes 3 and list[3] == "orange" which is less than "watermelon" so the loop body executes. k becomes 4, the first part of the loop test is false, and the loop doesn't execute and 4 is returned.
The test k < len is needed to avoid a bad index when a string s is larger than all the elements in the vector. The largest valid index is size() - 1, so the check is needed.
The most number of elements shifted is n, when the new element belongs first (index zero) all the elements must be shifted.
If list represents ("avocado", "banana", "lemon", "orange") then to remove "banana" the call is:
The number of strings that do NOT begin with a vowel is given by