Results 1 to 3 of 3

Thread: Example: passing vector or array to same function

  1. #1
    Junior Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    17

    Example: passing vector or array to same function

    [code=thinbasic]
    '
    ' 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

    [/code]

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736

    Re: Example: passing vector or array to same function

    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:
    [code=thinbasic]
    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
    [/code]

    UPDATED
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    thinBasic MVPs Michael Hartlef's Avatar
    Join Date
    Sep 2006
    Location
    Germany
    Age
    58
    Posts
    3,299
    Rep Power
    348

    Re: Example: passing vector or array to same function

    Thanks e90712 for sharing this code.

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •