Obs! Innan man får lämna in eller redovisa uppgifter i den här kursen ska man fylla i en särskild fusk-enkät! |
If you are using Visual Studio, you might want to look at some instructions for Visual Studio 2008.
Fix this large, meaningless program that doesn't work:
On a Linux or Unix system, the file debug-exercise.tar.gz can be unpacked using the command tar xvzf debug-exercise.tar.gz.
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 (old) course book:
Both programs translate from infix to postfix notation. They are text-only programs, which read text from the standard input (usually the keyboard) and write text to the standard output (usually a window on the screen), but other than that it is a part of the exercise to find out how to use them, and what input they expect.
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. Read more in Wikipedia.) 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; |
Det kan vara upplysande att titta på resultatet av preprocessningen:
Discuss:
Even if you don't send a report by e-mail, we advise that you write down your answers, to facilitate communication and for your own later use.