44 posts
K&R exercises sometime are wierd, or maybe not?
Edited by rizoma on
I'm working on my c skills to better undestand the Hmh stream. I'm doing math(with khan academy) and c (with the famous K&R book).

Sometimes I really don't get the questions in the exercises(not the actual solution but just the meaning of what is expected me to do!) anyway I managed to get to the 4.4 exercise doing all of them with minor struggles but sometimes overdoing things a bit...

The exercise is about a polish reverse calculator and the given code is this one:

http://pastebin.com/DNxHxcG2

It's not really the given code I added the % operator and negative numbers implementation(that was the previous exercise and that one made perfectly sense), now the next one is asking:

Add commands to print the top element of the stack without popping, to duplicate it, and to swap the top two elements. Add a command to clear the stack.

The stack is just the "val" array, and for what I can undestand it gets cleared every line of input since every time a value gets popped the index decrese, so basically every new line the new values are pushed always from the beginning of what they call "stack"(the val array). So the old values are overwritten.
I don't get it.
Really!
I find myself tempted to not pop the values but just read them so that the "stack" can grow then I can operate on previous stored data, but I know the exercise is probably just asking me to do operations on just the single line of input.

I'm not looking for a solution just wondering if it's me or it's the textbook a bit weird sometimes!

54 posts
K&R exercises sometime are wierd, or maybe not?
Yes that book can definitely be weird. I read that book and think it's a good but fairly advanced. I really liked "C Programming: A Modern Approach" by K.N. King when I was first learning C, and I didn't find a single example weird :)
Mattie
35 posts
K&R exercises sometime are wierd, or maybe not?
rizoma
The stack is just the "val" array, and for what I can undestand it gets cleared every line of input since every time a value gets popped the index decrese, so basically every new line the new values are pushed always from the beginning of what they call "stack"(the val array). So the old values are overwritten.

That's not correct. Only the top element of the stack is popped once the line ends. The rest of the stack contents below it are still there.

For example, if you input:
 1 1 2 3 4 5 +[enter] 

the output is 9, and the stack array contains [1, 2, 3].
44 posts
K&R exercises sometime are wierd, or maybe not?
Edited by rizoma on
Sizik
Only the top element of the stack is popped once the line ends. The rest of the stack contents below it are still there.

True, I was thinking only about binary operations, you are totally right.
Thanks!
Now the exercise make little more sense. Even if I don't see the the pratical purpose...
Maybe I should surrender to the fact that sometimes the goal is just Learning the c Language!
44 posts
K&R exercises sometime are wierd, or maybe not?
Randy Gaul
Yes that book can definitely be weird. I read that book and think it's a good but fairly advanced. I really liked "C Programming: A Modern Approach" by K.N. King when I was first learning C, and I didn't find a single example weird :)

I will check it, thanks!
Bryan Taylor
55 posts
K&R exercises sometime are wierd, or maybe not?
rizoma
Now the exercise make little more sense. Even if I don't see the the pratical purpose...
Maybe I should surrender to the fact that sometimes the goal is just Learning the c Language!

The point of the exercise (I assume, at least -- I didn't write it) is to look at the API versus implementation.

You have two different ways of implementing those operations. Directly, using knowledge of the implementation, and indirectly by composing API operations. For example, "print top without popping" could be implemented directly as:
 1 2 double top_val = val[sp - 1]; printf("%f", top_val); 

Or indirectly as:
 1 2 3 double top_val = pop(); printf("%f", top_val); push(top_val); 

Note that this version neither knows nor cares that the stack is implemented as an array. It also handles the boundary conditions properly, without any extra effort, whereas the direct implementation above does not.

The example here is trivial, so why you'd care about this tradeoff may not be entirely clear, but it is a decision that comes up often.
44 posts
K&R exercises sometime are wierd, or maybe not?
Edited by rizoma on
btaylor2401

The example here is trivial, so why you'd care about this tradeoff may not be entirely clear, but it is a decision that comes up often.

Yes, I think I should look at those exercises taking into account coding exploration first, then the effective pratical use of the resulting code.

Anyway thanks guys, all the comments are really valuable, sometime I really need some external input.

:-)