June:12(Tue)

CS 361 Exercise-Set #6

Due Tuesday, June 19, by lecture time

You can submit your work at the CIS main office, MAK 2202


    Please do all of the exercises here in teams of (at most) two. Then, naturally have only one submission of this work per team and clearly identify both members of the team.


  1. [12 points]   Augment your Exs. 4E search() program files as follows:
    1. Write a header file search.h containing the declaration of function search() and the definition of the 'not found' value -1.
      • At the beginning and end of this file, have appropriate conditional compilation directives as presented in lectures for .h header files.
    2. In search.c,   #include your new search.h .
    3. Use typedef to give a name such as "index_t" to the type of values that search() returns and its third argument. Use this "index_t" in search.h and search.c .
    4. Obtain your own copy of search_demos.c from ~mcguire/public_html/teaching/361/exercises/4/e/ , and in it, #include your new search.h .
    5. In your Makefile, remove all references to ~mcguire/public_html/teaching/361/exercises/4/e/ , and add search.h to the dependencies as appropriate.
    Submit your new search.h , search.c , search_demos.c , and Makefile , as well as a demonstration in which you execute the following:
    $ gcc -MM search.c
    $ gcc -MM search_demo.c
    $ touch search.h
    $ make
    $ ./search_demos
    
    If make doesn't recompile everything, then you need to fix things.


  2. [8 points]   Change your parentheses-checking program of Exercise B of Exercise-Set #5 to process input from a file that it open()s.

    Handle errors as in lowercase_sys.c presented in lectures.

    Still don't #include <stdio.h> .

    Demo as before plus with  .../0  and  .../_  and no argument. Display exit-status values.


  3. [20 points]   Add more functionality to your rudimentary shell: Demonstrate compiling your new version of rudimentary_sh and using these new functionalities. Execute echo a couple of times with different arguments; and similarly for cd , executing pwd before and after each execution of cd . (Note that "~"-interpretation probably won't work with cd .) Apply size to each of the following arguments: Finally, demonstrate your command interpreter with the commands of Exercise A of Exercise-Set #1. Except you may have to skip over commands #19-20 or kind of restart things if you do try them.


  4. [10 points]   Write a program that uses none of the facilities of stdio.h — and, indeed, doesn't #include it — yet manages to put the characters  hello\n  (where  \n  means a newline character) in the file specified by the program's first argument. Use the system calls of Sections 8.2 and 8.3 . Your program should behave appropriately if the number of arguments is wrong or if accessing the file fails; again, use strerror(). Also, your program needs to close the file explicitly (rather than having the file closed implicitly when the program ends).

    Here (again) are macros for outputting strings:

    #define fdputs(fd,s)    write((fd), (s), strlen(s))
    #define fdputsq(fd,sq)  write((fd), (sq), sizeof (sq) - 1)
    
    Copy these into your program here.

    In your demonstration here, do the following:


  5. [16 points]  
    1. Define a struct to represent a DVD (movie), with data members for the movie's title, number of minutes, and price (a floating-point number).
    2. Write a function for displaying one of these DVD structs.
    3. Create an array of DVDs initialized to contain ten different ones (with different titles, lengths, prices, etc.) which you choose.
      Display the contents of this array in their initial order (however you initialize things — perhaps in order by your preferences?).
    4. Use qsort() and appropriate comparison functions which you write to sort your list of DVDs by titles, lengths, and prices, displaying the contents of the array each time.
      • Write your function comparing DVDs by title so that any one of the following words and whitespace will be skipped at the beginning of a title: "A", "An", and "The".
      • Write your function comparing DVDs by length so that when qsort() uses it, longer movies will precede shorter ones.
    Naturally use here technology of earlier exercises such as the macro for array length and a typedef for const struct pointers.

    If you don't own 10 DVDs, obtain 10 titles by including movies you've seen recently that are available on DVD, and look up their lengths and prices (via Amazon.com or something).


  6. [18 points]  
    1. Redo your work for Exercise C of Exercise-Set #5 using pointer arithmetic and (a) separate module(s) for your function comparing two strings from back to front.
      • You need to have a separate .c file and a .h file, plus a Makefile, with this exercise's files in their own directory.
      • To use pointer arithmetic, rewrite your function of Exercise C of Exercise-Set #6 with pointer variables, pointing at entries of the given strings, instead of any integer index variables. In the function header, do not use brackets, "[ ]"; instead, use "*"s at proper places. Inside the function, use pointer arithmetic; your function should contain no array references (i.e. using brackets, "[ ]"), and you should not change (increment or decrement) any variables other than pointer variables. Instead of such operations on integral variables, decrement and increment and compare and subtract etc. pointer variables.
      Demonstrate that your main() of Exercise C of Exercise-Set #6 works properly with this new version of your function.

    2. Do the following in an additional .c file:
      1. In main(), output the values of sizeof data ,   sizeof *data ,   alen(data) ,   and all the contents of data[]  where data is defined as follows:
        char  * data[] =
            {  
            "negative", "zero", "one", "two", "three", "four", "five", "six",
            "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
            "fourteen", "fifteen",
            "twenty", "twenty-one", "twenty-two", "twenty-three",
            "infinity",
            "click", "cling", "clock", "clung", "flick", "fling", "flock", "flung"
            };
        
      2. Then use the standard library function qsort() and auxiliary code — particularly scmp() — as shown in lecture (and as also available online) to sort data[]  alphabetically (lexicographically), just as (most of) argv[]  was sorted in qsorts.c presented in lecture.
      3. Display data[] again, after that sorting.
      4. Write a function like scmp() except that it invokes your function of Part 1 above instead of strcmp().
        Then, use qsort() and this new function to sort data[]  in rhyming dictionary order.
        You need this new function like scmp() rather than just the one from Part 1 above (or Exercise C of Exercise-Set #6) alone because qsort() expects you to pass it a function that can handle, e.g., &data[0] and &data[1] as arguments.
      5. Display data[] again after it's been sorted this second way.



(Copyright © 2007 by Hugh McGuire   -- for thoughts about this, see:   http://www.csis.gvsu.edu/~mcguire/teaching/copyright_thoughts.html )