Code: Select all
for i = 1 to 5
iValue = i * 2
pushValue stack(), top, iValue
print "Stack top : ";ubound(stack),"Value : ";iValue
next
Code: Select all
for i = 1 to 5
iValue = i * 2
pushValue stack(), top, iValue
print "Stack top : ";ubound(stack),"Value : ";iValue
next
Code: Select all
dim stack(0)as integer
dim top as integer
dim i as integer
dim iValue as integer
sub pushValue(aStack() as integer, stackTop as integer, value as Integer)
stacktop += 1
redim preserve aStack(stackTop)
''Element data can be preserved during
''resizing of variable-length arrays with
''the new Preserve specifier
aStack(stackTop) = value
end sub
function popValue(aStack()as integer, stackTop as integer) as integer
dim ret as integer
if stackTop = 0 then
ret = 0
else
ret = aStack(stackTop)
stackTop -= 1
redim preserve aStack(stackTop)
end if
return ret
end function
print "pushing values onto the stack"
for i = 1 to 5
iValue = i * 2
pushValue stack(), top, iValue
print "Stack top : ";ubound(stack),"Value : ";iValue
''I don't understand the ubound staying 0 all the time
next
print
print "size before popping : "; ubound(stack)
print
iValue = popValue(stack(),top)
do while iValue > 0
print "stack value : ";iValue
iValue = popValue(stack(),top)
loop
print "size after the popping : "; ubound(stack)
sleep
What is the reference of this book ?ivory348 wrote:"No way of succeeding right from the start" - this is an example in a theory book on freeBasic; so it cannot be total garbage, I would say.
Code: Select all
sub pushValue(aStack() as integer, byref stackTop as integer, value as Integer)
function popValue(aStack()as integer, byref stackTop as integer) as integer
Well, first of all, the concept of push/pop comes from assembly language (not really BASIC).Then also: what is the use of all this, in a program, given the existence of arrays.
Code: Select all
Dim As Integer stack(1 To 10), top = 10, iValue
Sub pushValue(aStack() as integer, ByRef stackTop as integer, ByVal value as Integer)
aStack(stackTop) = value
stacktop -= 1
End sub
function popValue(aStack() As integer, ByRef stackTop as integer) as Integer
if stackTop < LBound(astack) OrElse stacktop > UBound(astacK) Then
Return 0
Else
stackTop += 1
end if
return aStack(stackTop)
end function
print "pushing values onto the stack"
for i As UInteger = 1 to 5
iValue = i * 2
pushValue(stack(), top, iValue)
print "pushed Value: "; iValue, "current top: "; top
Next
Print : Print "array size before popping : "; ubound(stack)
Print : print "poping values from the stack"
For j As UInteger = UBound(stack) To UBound(stack) - 4 Step -1
iValue = popValue(stack(),top)
print "poped value: "; iValue, "current top: "; top
Next
Print
print "array size after the popping: "; ubound(stack)
Sleep
Code: Select all
redim stack(0)as integer '' corrected
dim top as integer
dim i as integer
dim iValue as integer
sub pushValue(aStack() as integer, byref stackTop as integer, value as Integer) '' corrected
stacktop += 1
redim preserve aStack(stackTop)
''Element data can be preserved during
''resizing of variable-length arrays with
''the new Preserve specifier
aStack(stackTop) = value
end sub
function popValue(aStack()as integer, byref stackTop as integer) as integer '' corrected
dim ret as integer
if stackTop = 0 then
ret = 0
else
ret = aStack(stackTop)
stackTop -= 1
redim preserve aStack(stackTop)
end if
return ret
end function
print "pushing values onto the stack"
for i = 1 to 5
iValue = i * 2
pushValue stack(), top, iValue
print "Stack top : ";ubound(stack),"Value : ";iValue
''I don't understand the ubound staying 0 all the time
next
print
print "size before popping : "; ubound(stack)
print
iValue = popValue(stack(),top)
do while iValue > 0
print "stack value : ";iValue
iValue = popValue(stack(),top)
loop
print "size after the popping : "; ubound(stack)
sleep
What is of no use, the example or the concept?ivory348 wrote:Ok so it is of no use, but I still want to understand.
Nobody wants to discuss not working as expected code (aka: buggy code).ivory348 wrote:... my questions remain unanswered.