These are the first of several problems which involve the use of dynamically allocated memory. For full marks, you must not only use malloc() to obtain RAM for data storage, you must also ensure that all allocated memory is freed before the program terminates. You must provide hard copy and ask the teacher to run your program.
This problem allows you to become acquainted with dynamic memory allocation in the familiar context of arrays. The array will contain a set of pointers to memory allocated for data structures, each block of RAM being represented by a pointer in the array. This is still not the best programming technique, because the array is statically declared. The next Problem Set will deal with a better method.
Using the following compiler directive and declarations
#define MAXROLLS 10
struct roll_s {
int die1,
die2,
total; };
struct roll_s *roll_ptrs[MAXROLLS];
write a program named dice1.c which simulates ten rolls of a pair of dice.
Your program must follow the pseudocode provided below.
N.B. For this quickie program, you may assume that the memory requested is in fact allocated.
Using the compiler directive and declarations given above along with the one below, cannibalize your source code for Program I and add the following features, saving your file as dice2.c.
struct roll_s *roll_copy_ptrs[MAXROLLS];
Your program will read in pairs of numbers from a file, each of which represents a roll of a pair of dice. After printing a labeled table of the original data, you will use a copy of the array of pointers to sort the data based on the total of each roll. Finally, you will print out a labeled table of the sorted data.
You can adapt any of the following sorting algorithms for your program:
Your program must follow the pseudocode provided below.
N.B. A graceful exit from the program is required if the user-specified file does not exist. For this program, you must ensure that the memory requested is in fact allocated -- if it is not, you must call the function described in Step 9 before exiting.