CS/CE218 --- Unix and C Programming. 16 October 91 Name: TA: Section: HOMEWORK 4: C Expressions and Statements Due: 21 October 1991 In these problems, you are to write C code and functions. You will have to write extra code to test your functions, but you should turn in only the code the problems ask for. 1. (4 points) Rewrite the following code WITHOUT using increment or decrement operators. Your code should compute the same function. Don't worry about simplifying; just eliminate the operators. int compute(int a, int b, int c) { a++; b=--a; /* understand prefix? */ printf("A"); if (--b) /* can you deal with an embedded expression ? */ b=++c; printf("B"); while (a=c--) /* can you deal with it in a while loop? */ b += --a; printf("C"); return (--b + c); } 2. (8 points) Write a function: void binary(unsigned int n) such that binary(x) prints the binary numeral that represents x. For example, binary(17) should print the digits ``10001'' on standard output. (Hint: use putchar.) 3. (10 points) Write a function: unsigned int count(unsigned long x) such that count(x) returns the number of times that the bit pattern 101 (i.e., 5 in base 2), appears in the binary representation of x. Note that this function does not write any output. For example, 00010100 holds the pattern once 00010101 holds the pattern twice (overlapping) 10101010 holds the pattern three times 11001110 does not hold the pattern (Hint: you may wish to look at the function bitcount on page 50 of the text.) 4. (extra credit only) Here we extend the notion of patterns so that an arbitrary pattern that can have ``holes'' in it, i.e., bits whose setting we don't care about. This ``don't care'' mask will have 1 bits exactly in the places where either a 0 or a 1 will match the template. (So a 1 means the caller doesn't care.) In places where the mask has a 0 bit, the template must be matched exactly. Both the templates and the masks are taken to be 3 bits long. Here are some example patterns (where ``?'' means don't-care): Pattern | template mask --------+---------------------------- 101 | 101 000 1?1 | 101 010 0?1 | 00100 010 Example of how the pattern might be applied to the pattern 1?1: 00001110 holds the pattern 1?1 once 11111111 holds the pattern 1?1 six times You should write a function unsigned int patternmatch(unsigned long x, unsigned template, unsigned mask) for this problem. 5. (Extra credit only) Modify the extended pattern matcher so you can input strings like "1?1" instead of two numbers. Note that the strings may be variable length