Fix this large, meaningless program that doesn't work:
Use a debugger to find out where the program crashes, and why. It's a rather large and intentionally obscure program, so it's probably hard to find the problem without a debugger.
Fix the problem and recompile the program, so it can execute without crashing. It's sufficient just to comment out lines that don't work.
Discuss:
Compile and try out these two programs from the course book. Make each of them a separate project:
Both programs translate from infix to postfix notation.
Suggestion: When you execute the programs, start them from a command window. If you start text-based programs like these from inside Borland C++, or by clicking on the icon, all output disappears when the program exits and the window closes.
Discuss:
Write a function in C that computes n! by calling itself recursively. To save you some work, I've written it for you:int plus(int x, int y) { return x + y; } #define PLUS(x, y) ((x) + (y))
Now, try to do the same, but with a preprocessor macro. (Hint: instead of an if statement, you'll have to use the ?: operator.) Will this macro work? Why, or why not?int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); }
Later in the course, you will come into contact with context-free grammars (which are recursive) and regular expressions (which are not). In that sense (but only in that sense!) context-free grammars are similar to C functions, and regular expressions are similar to C macros.
Some hints about the C preprocessor:
C file | Preprocessing result |
---|---|
#define FOO 4711 i = FOO; |
i = 4711; |
#define TIMES(x, y) x * y i = TIMES(7, 4); i = TIMES(7, 2 + 2); i = TIMES(++, {); |
i = 7 * 4; i = 7 * 2 + 2; i = ++ * {; |
#define SQR(n) n*n #define CUBE(n) n*SQR(n) i = CUBE(3); |
i = 3*3*3; |
Discuss: