When possible, SHOW YOUR WORK. Also, YOU MAY NOT USE A COMPILER OR ANY OTHER MATERIALS (except a pen and your brain).
0. Write your name on the top-right corner. No name, no grade, no exceptions.
1. Write a C++ function largestAndSmallestInMatrix, declared below, which prints out the largest and the smallest elements in the 2D matrix g_matrix.
const int ROWS = 3;
const int COLS = 4;int g_matrix[ROWS][COLS];
void largestAndSmallestInMatrix()
{
;; your code here
} // end of largestAndSmallestInMatrix()
2. Again considering g_matrix from the previous question,
as declared above it is not initialized. Your task here is to provide
a similar declaration of g_matrix, but one which also initializes it to
values of your choosing.
3. Recall that a 2D array is really a contiguous (packed) array of pointers to 1D arrays, and that each 1D array, while itself packed, need not be contiguous in memory to the other 1D arrays.
a) Draw a diagram showing all the blocks of memory which are needed to store the 3x4 g_matrix, being sure not to show items as contiguous unless they must be contiguous.
b) Recalling that an int requires 4 bytes, as does a pointer, indicate the total number of bytes necessary to store the 3x4 g_matrix.
4. Recall that a 32-bit floating point number has 1 sign
bit, 8 exponent bits (representing the exponent plus 127), and 23 mantissa
bits (plus the implicit "1." ahead of the mantissa).
a. How many positive integers (ie, numbers with no fractional portion) can be represented as 32-bit floating point numbers?
b. How many 32-bit floating point numbers are there in the range (0,1) (do not include 0.0 nor 1.0)?
5. The game of Nim is played by two players alternating turns,
each time picking up 1, 2, or 3 sticks from a pile. The player to
pick up the last stick wins. There is a winning strategy: always
try to leave a multiple of 4 sticks in the pile after your turn (thus,
if there are 7 sticks, pick up 3; if there are 49 sticks, pick up 1).
Write a C++ function,
int sticksToPickUp(int sticksInPile), which
returns the ideal number of sticks to pick up given the number of sticks
in the pile. (Hint: Remember to use the "return" command
to return a value from a function.)
See Course Home Page.