CS/CE 218 Lab 7 31 October 1991 Name: Name: TA(s): The report for this lab is due Monday, 4 November 1991. You will be working in pairs today, so both of you should submit a joint report for this lab; you will each receive the same grade. Today's lab examines some ``haunted'' programs. (Some of these were taken from Kelley and Pohl's ``A Book on C'', but don't run off to the library, that book doesn't have the answers.) For each of the programs, you are to find the bug; that is you should explain why it doesn't do what it should and how to fix it. The programs are all in the directory $PUB/lab-data/lab7. It may be helpful to run some of them, or to experiment with some of them by editing them and running them again. Here's an example of the kind of problem you'll see below. The file look_carefully.c (that is $PUB/lab-data/lab7/look_carefully.c), contains the following text. ------------------------ #include int main() { printf("Why is 21 + 31 equal to %d?\n", 2l + 3l); return 0; } -------------------------- If you compile this with gcc -Wall look_carefully.c -o look_carefully, and then run the program with ./look_carefully You'll see that the the program look_carefully.c prints Why is 21 + 31 equal to 5? The question is as follows: Look at the text of the program to see why it doesn't print Why is 21 + 31 equal to 52? The answer is that in the text above ``2l + 3l'' is written with two lower case letter els, those aren't ones. The compiler doesn't complain, because, of course, 2l is a perfectly legitmate constant of type long. The other problems below involve bugs that are stated in the same way. But they won't be the same kind of bug. The programs really illustrate issues in C that we've discussed recently in lecture; you'll see that once you understand the answer.... 1. (10 points) The program ghost.c prints sin(x) * sin(x) + cos(x) * cos(x) is 1.108858900000000e+09 This is a number in the thousand millions (billions for us americans)! Why doesn't it print sin(x) * sin(x) + cos(x) * cos(x) is 1.000000000000000e+00 (as it should, if you remember your trigonometry)? 2. (10 points) The program shadow.c declares two macros. The first macro, PRINTIJK prints the messages of the program. It's correct in the sense that it always prints the values of the variables named i, j, and k. Thus the first PRINTIJK of the program prints i is 3, j is 44, k is 5555 which is utterly correct. If you don't understand how this macro works, look in section 4.11.2 of the C text. (a) explain why the second line printed by shadow.c is *not* the same as the first line. (b) The second macro, SWAP, seems to work, as the third line printed by the program seems reasonable. (Does it to you?) But after the second SWAP, that is the line SWAP(i,j), why doesn't the fourth line of the printout show the values of i and j are changed? That's it, you're all done. The rest of this file contains extra credit problems only. 3. (extra credit only) The program ieee.c contains a real monster. What happens when you *compile* it? What's the problem? 4. (extra credit only) The program aargh.c is syntactically correct (compile it!) But it prints That's odd, c < b < a is true! Why doesn't it print the following message? Of course, it's not true that c < b < a 5. Floating point numbers are not the same as the mathematical reals. They have only limited precision.... (a) Look at the file wraith.c; what do you think it will print? What does the program actually print? (b) When you've written down your answer, look in the file .wraith.hint, in $PUB/lab-data/lab7, and follow the directions there. But don't do that until you think you know the answer to part (a). (Remember, this is an extra credit problem only.)