CS/CE218 --- Unix and C Programming. 11 October 91 Name: TA: HOMEWORK 6: Make, RCS, and C pointers Due: 18 November 1991 To start this homework, execute the following commands: cd $HOME mkdir hw6 chgrp cs218s . hw6 chmod 750 . hw6 Then leave the files described below in your directory $HOME/hw6. These will be readable by the course staff, but not by other students. 1. (10 points) You must use RCS for this homework. We expect you to use ``ci -l'' for your Makefile and each C program and header file. You should use these at each logical point in your program development and at the end of each of your work periods. Some logical points in your program development include before you compile the program for the first time, and after you get all the syntax errors out, but before you start testing. Also be sure to check in (ci -l) the final version that you hand in. (To summarize, at a minimum, you must check in any original you got from us, a version before you first compile, a version after all syntax errors are out, and a final version.) (If you use a C compiler at home, the best thing to do is to get a copy of RCS and make by anonymous ftp from wuarchive.wustl.edu in the directory mirrors/msdos/pgmutl (file dosrcsex.zip) and use RCS at home. You will then have to bring in the RCS and other files to school by the due date. If you can't do that, save the text of your program at logical points, and insert the texts into your RCS files on Zippy or Zaphod daily. If you have difficulties with all this the course staff will tell you that it is simpler just to use Zippy and Zaphod...) In *each* of your C program files, put the line static char rcs[] = "@(#)$Header$"; as the first line (if it's not already there). Also your header (.h) files should have the following structure (the example is for a file called my_header.h). #if !defined(MY_HEADER_ID) #define MY_HEADER_ID "@(#)$Header$" /* ... regular stuff goes here ...*/ #endif Note: after running ``ci -l file.c'', if you have an emacs buffer that contains file.c, emacs will ask you if you want to edit the buffer, since the file has changed. Tell it ``no'', and use M-x find-file (C-x C-f) to read the file in again. This will allow you to change a copy of the file with the $Header$ information expanded. 2. (20 points) You must use ``make'' for this homework. These points are reserved for your makefile. It should be suitable for compiling all the other exercises. Hand in a printout of your makefile with all the other exercises. 3. (40 points) Do exercises (a) 4-3 and (b) 4-4 on page 79 of the C book. For part (a), note that getop.c doesn't handle negative numbers. You'll find the code for the calculator (slightly altered from pages 75-82) in $PUB/hw6. Copy this code to $HOME/hw6, execute ``ci -l Makefile *.[ch]'', and then make the necessary changes to do these exercises. For problem 4-4, the commands to add are to be called "top", "dup", "swap", and "clear". You'll have to modify getop.c to be able to pass such commands to the main program. 4. (EXTRA CREDIT ONLY) Modify getop.c from the above problem so that it treats its argument as a pointer instead of as an array. 5. (10 points) Do exercise 5-3 on page 107 of "The C Programming Language (2nd edition)". (To save typing, the original code (from page 48, slightly altered) is in the file $PUB/hw6/strcat.c on Zippy and Zaphod.) For this problem, also write a main program in a file test_strcat.c, and put a target test_strcat in your makefile. 6. (20 points) Do exercise 5-4 on page 107 of the C book. Use pointers! Put your code in strend.c, and write a main program in the file test_strend.c to test strend. 7. (Extra credit only) Do exercise 4-14 on page 91 of the C book. 8. (Extra credit only) Write a macro sort_decl(t) that when given a type t declares a sort function extern void t_sort(t []) and also a macro sort_def(t) that defines the function that sort_decl(t) declares. You should be able to use these as follows, assuming that they are in the file "sort.h" #include "sort.h" sort_decl(double); ... double_sort(myarray);... and in another file, called double_sort.c: #include "sort.h" sort_def(double) 9. (Extra credit only) Do exercise 5-6 on page 107 of the C book. Time your code and the original code (use a loop to call the function in question some number of times in the main program, and the time command of the shell). Which is faster? Is the optimizer of gcc smart enough to produce faster code than you?