Programming in C, autumn 2008
Exercise 4
Sep. 29 2008
double* myAlloc(int n); /*Allocate a block of memory large enough to store n double values. * Return a pointer to the newly allocated block; and a null pointer * if the block cannot be allocated or if n <=0. */ int get(double * block, int size); /* Assume that block points to a block of memory large enough to store * size double values. Read double values from the standard input and * store incoming values in the block of memory passed as the first * parameter. Stop reading when either size values have been read, a * value that rounds to 1 (one) has been encounted, or an invalid * double value has been encounted. * Return the number of values read. */ void show(double *block, int size); /* Assume that block points to a block of memory storing size double * values. Print these values to the standard output, each value on a * separate line. */ double max(double *block, int size); /* Assume that block points to a block of memory storing size double * values. Return the maximum value. */
Exercises 3-6. Write a menu-driven program, which supports the following commands:
i n where n is an integer value r fname where fname is a filename w s r where r is a double d h q The above commands have the following meaning: i allocates a bolck of memory to store n double values; if a block of memory has previously been allocated, that block is deallocated r reads double values from the file whose name is fname; if fails if memory has not been allocated. It stops reading if either the number of values read is equal to the current size of the memory block, or no more double values can be read from the file (because end-of-file has been encounted or a reading error has been occured) w shows all values that are currently stored in the memory block s searches the block of memory looking for a given value and informs the user whether or not this value is in the block; it fails if the block has not been allocated. d is only available when the program is compiled using debugging mode. It turns additional debugging message on/off; i.e. when it is executed for the first time, it turns debugging on, and then shows additional information about commands being executed. When the d command is executed for a second time, these messages are not shown anymore. Subsequent commands using the d option toggle debugging on and off h shows alls available commands q quits the program (but first, it closes any files that have been opened, and deallocates any memory that have been allocated)
.