Programming in C, autumn 2007


Exercise 4

Sep. 28 2007

  1. Write a procedure alter(x,y) which changes the value of x to x-y, and the value of y to 2 (x and y are of type double).
  2. Use pointers to implement a solution to the following problem (write a complete program):
    1. Create a memory block to store 30 doubles
    2. Read in up to 30 doubles from the standard input; stop reading when any of the following conditions are satisfied:
      1. 30 doubles have been read
      2. EOF has been encounted
      3. a negative value has been encounted
    3. Find the maximum double value stored in the block.

    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)
    
  3. Write a fuction, which implements command i.

  4. Write a fuction, which implements command r. Write a fuction, which implements command w.

  5. Write a fuction, which implements command s.

  6. Write a main program, which reads and checks input and performs required command. Commands can be given using both lowercase and uppercase letters. In main program is the code required by commands h and q. If the commands d is given the main program can print that the command d has not been implemented. If the input is not valid, the main program will write help about commands.

.