Programming in C, autumn 2007
Exercise 4
Sep. 28 2007
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)
.