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))
int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); }
If you call this function in your program, for example as factorial(3), it will return the value 6. There will be a total of four activations of this function: first your own call with the value 3, then it calls itself with the value 2, then it calls itself again with the value 1, and finally it calls itself again with the value 0. Since the variable n has the value 0, the condition in the if statement is true, and the recursion ends.
Now, try to do the same, but with a preprocessor macro.
Instead of an if statement,
you'll need to use the ?: operator,
which has this syntax:
condition-expression ? expression-if-true : expression-if-false
For example, 2<3?4+5:6+7 will evaluate to 9.
You can read more in Wikipedia.
Will this macro work? Why, or why not?
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 SQUARE(n) n*n #define CUBE(n) n*SQUARE(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.