CS 223 - Spring 2017. 2/27

Welcome to CS 223!

Logical problem of the day

% Calcx
1234567890123456 + 1
Input: 1234567890123456 + 1
Result: 1234567890123457.00
12345678901234567 + 1
Input: 12345678901234567 + 1
Result: 12345678901234568.00
123456789012345678 + 1
Input: 123456789012345678 + 1
Result: 123456789012345680.00
1234567890123456789 + 1
Input: 1234567890123456789 + 1
Result: 1234567890123456768.00
The above is output from the staff solution to hw4. Things get a a bit crazy after 17 decimal digits. Why? lp0227 tells us why.

Lecture 12: HW4 + Data Structures: stacks


  • Office hours:
    The office hours are Sun/Tue/Thu 8-11 PM at Hillhouse 17 Rm 111.
  • My office hours this week are Wednesday 4-5pm.

  • Mid term exam

  • Mid term questionnaire (anonymous). Please fill this out at your earliest convenience. Your comments will be helpful to the instructor and TA's.

    We had to revise the questionnaire last Thursday. If you filled it out before then, please resubmit. This is an opportunity to provide useful feedback about both what you like and what you do not like.

    Based on preliminary review of the initial survey, there is a bimodal distribution of opinion. Either you think the pace is too slow, or too fast.

    Many of you came from CS 201 with no C experience. Others came from CS 50. You have different needs and expectations. My view is that we have a textbook for C (K&R). I am not going to lecture from that. (This was Alan Perlis' view. I remembering thinking that I would have liked him to do so.)

    Some of you object to my reading from the lecture notes (as I am no doubt doing now.) I prepare the notes to insure that I cover the points that I believe are important. Otherwise, I will likely forget something or make a mistake. I do try to untether myself from the podium and use the blackboard.

    Last term, some students wanted less emphasis on how to solve the homework problems, and more on the theory. This term, it is often the reverse. I try to help you navigate the problem sets - providing milestones and landmarks, telling you what pitfalls to avoid.

    Believe it or not, we have made a special effort this term to make the homework specs more stable and clear. The ULAs have been very helpful in anticipating and correcting ambiguities and unclear language. There has actually been a big improvement as indicated by fewer revisions.

    Individual feedback. Some of you get this during office hours. The automatic grading is not so helpful in this regard. I think that one on one code review is probably the most effective. We might try peer code review after assignments have been graded.

    Other possible mid-term changes: sections on specific topics (UNIX, pointers, valgrind, debugging, etc.) Different office hours schedule. We welcome your suggestions.

    Assignment 4

    Problem set 4

    Lecture Starts Here


  • Discussion of stack.h and stacktest.c Run with valgrind. Use mymalloc() and myfree() to track down memory leaks.

    Why study stacks in the first place? I failed to mention that stacks are pervasive in computer science. Function calls depend on pushing the current execution state on the stack, and then popping the stack upon completion. No stack = no recursion.

    See discussion of stacks, converting infix to postfix, and evaluating a postfix expression.

  • For hw4, we do not ask you to expand the stack. If you try to push onto a full stack, the program halts with an error (exit(1)). It is actually easier to make expandable stacks than hash tables. You can implement stacks using linked array structures.
    struct stack {
      struct stack * next;
      int size;
      int top;
      int *elements;
    Imagine an instance where size = 4, and you have pushed the first 9 positive integers. When a stack is full, allocate a new one and have its next point to the old one.
  • For comparison, here is an object oriented implementation of a stack in python: stack.py Note the use of a stack to reverse a string. Also, in python we do not worry about memory allocation or the stack being full.

  • Here is a stack/queue problem from last term's final exam: Implement a queue using two stacks.