Filter: Difference between revisions
Content deleted Content added
Line 2,989: | Line 2,989: | ||
=={{header|Q}}== |
=={{header|Q}}== |
||
<syntaxhighlight lang="q">x where 0=x mod 2</syntaxhighlight> |
<syntaxhighlight lang="q">x where 0=x mod 2</syntaxhighlight> |
||
=={{header|QBASIC}}== |
|||
{{works with|QuickBasic|4.x}} |
|||
{{works with|Visual Basic for DOS|1.0}} |
|||
{{works with|PDS|7.x}} |
|||
<syntaxhighlight lang="qbasic"> |
|||
' OPTION EXPLICIT |
|||
' Filter |
|||
' This program selects certain elements from an array into a new array in a generic way |
|||
' SUBs |
|||
DECLARE SUB getNewArray (iGetWhat AS INTEGER) |
|||
' Var |
|||
' $DYNAMIC |
|||
CONST Even = 2 |
|||
CONST Uneven = 1 |
|||
DIM t AS INTEGER |
|||
DIM i AS INTEGER |
|||
DIM iArray%(1 TO 1) |
|||
DIM iArray2%(1 TO 1) |
|||
' Register |
|||
TYPE regSub |
|||
aNum AS INTEGER |
|||
END TYPE |
|||
' Main program cycle |
|||
CLS |
|||
RANDOMIZE TIMER |
|||
t = INT(RND * 300) + 1 |
|||
REDIM iArray%(1 TO t) |
|||
PRINT "Select items in an array into a new array in a generic way." |
|||
PRINT "Base array:" |
|||
FOR i = 1 TO t |
|||
iArray%(i) = INT(RND * 2000) + 1 |
|||
PRINT iArray%(i); |
|||
NEXT i |
|||
PRINT |
|||
' Gets the new array of evens |
|||
getNewArray Even |
|||
' Shows the result |
|||
t = UBOUND(iArray2%) |
|||
IF t > 0 THEN |
|||
PRINT "Selected items from the array (total:"; t; "of"; UBOUND(iArray%); "):" |
|||
FOR i = 1 TO t |
|||
PRINT iArray2%(i); |
|||
NEXT i |
|||
END IF |
|||
PRINT |
|||
PRINT "End of program." |
|||
END |
|||
REM $STATIC |
|||
SUB getNewArray (iGetWhat AS INTEGER) |
|||
' Var |
|||
DIM f AS INTEGER |
|||
DIM t AS INTEGER |
|||
DIM i AS INTEGER |
|||
DIM aFlag AS INTEGER |
|||
DIM rSub AS regSub |
|||
SHARED iArray%() |
|||
SHARED iArray2%() |
|||
CONST aName$ = "DUMMY$$$.$$$" |
|||
CONST False = 0, True = NOT False |
|||
f = FREEFILE |
|||
OPEN aName$ FOR OUTPUT AS #f |
|||
CLOSE |
|||
OPEN aName$ FOR RANDOM AS #f LEN = LEN(rSub) |
|||
t = UBOUND(iArray%) |
|||
aFlag = False |
|||
FOR i = 1 TO t |
|||
IF (iArray%(i) MOD 2) = 0 AND iGetWhat = Even THEN |
|||
aFlag = True |
|||
ELSEIF (iArray%(i) MOD 2) <> 0 AND iGetWhat = Uneven THEN |
|||
aFlag = True |
|||
END IF |
|||
IF aFlag THEN |
|||
rSub.aNum = iArray%(i) |
|||
PUT #f, , rSub |
|||
aFlag = False |
|||
END IF |
|||
NEXT i |
|||
' Redims the 2nd array |
|||
t = LOF(f) / LEN(rSub) |
|||
REDIM iArray2%(1 TO t) |
|||
FOR i = 1 TO t |
|||
GET #f, i, rSub |
|||
iArray2%(i) = rSub.aNum |
|||
NEXT i |
|||
CLOSE #f |
|||
KILL aName$ |
|||
END SUB |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
The output can change as the size of the base array and its values varies on each run. |
|||
<pre> |
|||
Select items in an array into a new array in a generic way. |
|||
Base array: |
|||
1426 770 1686 1472 385 1212 909 656 776 707 918 1646 1258 1406 887 42 |
|||
Selected items from the array (total: 12 of 16 ): |
|||
1426 770 1686 1472 1212 656 776 918 1646 1258 1406 42 |
|||
End of program. |
|||
</pre> |
|||
==={{header|QBASIC}}=== |
==={{header|QBASIC}}=== |