Computer Science Canada

stack demo

Author:  swiffer [ Wed Apr 21, 2004 9:10 pm ]
Post subject:  stack demo

I'm making a program that demonstrates how a stack works. The program is a series of rectangles stacked on top of each other. When the program performs a 'push', a rectangle is colored and each rectangle continues to be colored when the user hits a certain key. And the program performs a 'pop' when the user enters another key and makes the current rectangle disappear. Here's my code, which doesn't work. Can anyone help me/point out my mistakes? Thanks Shocked


import GUI
GUI.SetBackgroundColor (purple)
setscreen ("nocursor")

var x := 280
var y := 320
var vx, vy := 40
var top : int := 1
var contents : array 1 .. 8 of int := init (0, 0, 0, 0, 0, 0, 0, 0)
var character : string (1)

var font : int
font := Font.New ("mono:9")
Draw.Text ("Press A to push or D to pop", 20, 380, font, yellow)

procedure push
    var s : int := 8
    top := top + 1
    contents (top) := s
end push

procedure pop
    var s : int := 8
    s := contents (top)
    top := top - 1
end pop

/*Drawing the squares */
for Squares : 1 .. 8
    drawbox (x, y, x + 90, y + 40, 3)   % Duplicate original square 8 times
    y -= vy     % Create a vertical pattern for squares
end for

    getch (character)
    if character = "a" or character = "A" then
        drawfillbox (x, y, x + 90, y + 40, 3)
    elsif character = "d" or character = "D" then
        drawfillbox (x, y, x + 90, y + 40, purple)
    end if
end loop

Author:  AsianSensation [ Wed Apr 21, 2004 9:27 pm ]
Post subject: 

ok, you declared a couple of local variables in your procedure. I don't see how they work, because as soon as you call push or pop, s resets, and I don't see where you are using s any where else.

you get the array subscript out of range error, that's because you never checked to see if your subscript for the array is too low or too high. You should include this before you add or subtract 1 from top.

if top - 1 <= 0 then
end if

another thing is that you are not updating your squares. You only drew the square at some location, but forgot to increase or decrease the y value.

Author:  swiffer [ Mon Apr 26, 2004 6:54 am ]
Post subject:  ....

another thing is that you are not updating your squares. You only drew the square at some location, but forgot to increase or decrease the y value

OK, I thought I did.. ? Isn't this part of the code supposed to do that?

push    % Initiate 'push' procedure if condition is true
drawfillbox (x, y, x + 90, y + 40, blue) % Highlight the box yellow

What do I do to update the squares?

Author:  AsianSensation [ Mon Apr 26, 2004 7:26 am ]
Post subject: 

yeah, but you are not changing the y values of the squares. That's why the square is not updating.

Author:  swiffer [ Tue Apr 27, 2004 7:34 am ]
Post subject: 

OK. I don't understand. That part of the code I posted earlier doesn't do anything when I change the y-values. It only changes the size of the rectangle, which I don't want. I want each square to move up and down and be highlighted when the user presses a key. There must be something else I have to change. I'm thinking of a counter or some kind of increment procedure but I'm not really sure how it works. I looked it up at the turing reference but it wasn't much help. Sorry about all the hassle, this turing stuff is new and difficult for me:(