PDA

View Full Version : Example: passing vector or array to same function



e90712
18-07-2008, 20:02
'
' Example of function to handle both one and two dimensional arrays.
' Error message when one dimensional array passed as two dimensional:
' "Missing Close Parens ')'. Expected a ')' but found something else."
'

USES "console"

ALIAS CONSOLE_WRITE AS write
ALIAS CONSOLE_READLINE AS read

CONST NL AS STRING * 2 VALUE $crlf

DIM array2( 9, 9 ) AS INTEGER VALUE 7
DIM array1( 9 ) AS INTEGER VALUE 2

array1( 1 ) = 3
array1( 6 ) = 9
array2( 5, 5 ) = 4
array2( 2, 6 ) = 8

write NL, " A nine-vector displayed as a 3x3 matrix:", _
NL,"CALL digitarrayshow( array1, 9, 0, 3 )", NL

CALL digitarrayshow( array1, 9, 0, 3 )

write NL, "A 9x9 matrix displayed by the same function: ", _
NL,"CALL digitarrayshow( array2, 9, 9, 9 )",NL

CALL digitarrayshow( array2, 9, 9, 9 )

write NL, "A nine-vector shown as a row vector:", _
NL,"CALL digitarrayshow( array1, 9, 0, 9 )",NL

CALL digitarrayshow( array1, 9, 0, 9 )

write NL, "A nine-vector shown as a column vector:", _
NL, "CALL digitarrayshow( array1, 9, 0, 0 )"

CALL digitarrayshow( array1, 9, 0, 0 )

write NL

'"Missing Close Parens ')'. Expected a ')' but found something else."
'Uncomment next line to generate error code 20 message.
'call digitarrayshow(array1,9,9,9)


write "press enter to close window"

read( )

STOP

FUNCTION digitarrayshow( data_array( ) AS INTEGER, maxdim1 AS INTEGER, maxdim2 AS INTEGER, breakcount AS INTEGER )
LOCAL idim1 AS INTEGER
LOCAL idim2 AS INTEGER
'
IF maxdim1 < 1 THEN
EXIT FUNCTION
ENDIF
FOR idim1 = 1 TO maxdim1
IF maxdim2 > 0 THEN
FOR idim2 = 1 TO maxdim2
IF MOD ( idim2, breakcount ) = 1 OR breakcount < 2 THEN
write NL
ENDIF
write data_array( idim1, idim2 ), " "
NEXT
ELSE
IF MOD ( idim1, breakcount ) = 1 OR breakcount < 2 THEN
write NL
ENDIF
write data_array( idim1 ), " "
ENDIF
NEXT
write NL
END FUNCTION

Petr Schreiber
18-07-2008, 21:29
Hi e90712,

thanks for sharing very nice code, I, as a matrix lover, appreciate it a lot :)

Just one thing - ThinBasic allows programmer to determine dimensions of array using UBOUND keyword.
So it is possible to avoid run time error 20, you can handle incorrect input on your own.

See modified function:


FUNCTION digitarrayshow( data_array( ) AS INTEGER, maxdim1 AS INTEGER, maxdim2 AS INTEGER, breakcount AS INTEGER ) as long
LOCAL idim1 AS INTEGER
LOCAL idim2 AS INTEGER

' -- Error checking
select case maxdim1
case < 1
write NL & "[!] MaxDim1 must be positive" & NL
return %FALSE ' -- Not success, RETURN is the same as FUNCTION = ... : EXIT FUNCTION

case > ubound(data_array, 1)
write NL & "[!] MaxDim1 bigger than array can index ("+STR$(ubound(data_array, 1))+" )" & NL
return %FALSE ' -- Not success, RETURN is the same as FUNCTION = ... : EXIT FUNCTION
end select

select case maxdim2
case <> 0
if ubound(data_array, 2) = 0 then
write NL & "[!] MaxDim2 specified for one dimensional array" & NL
return %FALSE ' -- Not success, RETURN is the same as FUNCTION = ... : EXIT FUNCTION
end if

case < 1
if ubound(data_array, 2) <> 0 then
write NL & "[!] MaxDim2 must be positive" & NL
return %FALSE ' -- Not success, RETURN is the same as FUNCTION = ... : EXIT FUNCTION
end if

case > ubound(data_array, 2)
write NL & "[!] MaxDim2 bigger than array can index ("+STR$(ubound(data_array, 2))+" )" & NL
return %FALSE ' -- Not success, RETURN is the same as FUNCTION = ... : EXIT FUNCTION

end select

' -- Functionality
FOR idim1 = 1 TO maxdim1
IF maxdim2 > 0 THEN
FOR idim2 = 1 TO maxdim2
IF MOD ( idim2, breakcount ) = 1 OR breakcount < 2 THEN
write NL
ENDIF
write data_array( idim1, idim2 ), " "
NEXT
ELSE
IF MOD ( idim1, breakcount ) = 1 OR breakcount < 2 THEN
write NL
ENDIF
write data_array( idim1 ), " "
ENDIF
NEXT

write NL

function = %TRUE ' -- Success

END FUNCTION


UPDATED

Michael Hartlef
19-07-2008, 00:33
Thanks e90712 for sharing this code.