Page 1 of 2 12>
Topic Options
#166475 - 2006-08-26 05:47 PM Math Help
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Okay I am not the best person at math, I never got past algerbra, so I am requesting what is most likely a simple question.

I have an array with elements that contain 0 or -1, and I want to see if all elements are = -1, and I thought that the best way to do this would be a bitwise operation but not sure which one would be correct

Code:

$array = 0,0,0,-1,0,-1; example of what the array would contain.
;Option 1
For $ = 0 to 5
$2 = $2 | $array[$]
Next

;Option 2
For $ = 0 to 5
$2 = $2 ^ $array[$]
Next

;Option 3

For $ = 0 to 5
$2 = $2 ~ $array[$]
Next



I just am not sure which operator I would use.

Thanks
_________________________
Today is the tomorrow you worried about yesterday.

Top
#166476 - 2006-08-26 06:03 PM Re: Math Help
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I do not know what your goal is, but I would loop through an array like this
Code:

Break On
$array = 0,0,0,-1,0,-1
For $n = 0 to Ubound($array)
If $array[$n] = -1
? 'Element ' + $n ' is -1'
EndIf
Next


Top
#166477 - 2006-08-26 06:16 PM Re: Math Help
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
In the end I need to know if all the elements are -1 and be able to set a variable to say all = -1 (true) or all elements do not = - 1 (false)

so to continue the examples above

Code:

$array = 0,0,0,-1,0,-1; example of what the array would contain.
;Option 1
For $ = 0 to 5
$2 = $2 | $array[$]
Next
If $2 = -1
$Flag = "True"
Else
$Flag = "False"
EndIf
;Option 2
For $ = 0 to 5
$2 = $2 ^ $array[$]
Next
If $2 = -1
$Flag = "True"
Else
$Flag = "False"
EndIf
;Option 3

For $ = 0 to 5
$2 = $2 ~ $array[$]
Next
If $2 = -1
$Flag = "True"
Else
$Flag = "False"
EndIf


Top
#166478 - 2006-08-26 06:25 PM Re: Math Help
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Code:

Break On
$array = 0,0,0,-1,0,-1
$flag = 'true'
For $n = 0 to Ubound($array)
If $array[$n] = -1
? 'Element ' + $n ' is -1'
Else
$flag = "false"
$n = Ubound($array)
EndIf
Next
? "All elements were -1 = " + $flag


Top
#166479 - 2006-08-26 06:34 PM Re: Math Help
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Or as my wife just pointed out...

Code:

If Ascan($array,"0") = -1
$Flag = True
Else
$Flag = False
EndIf
Top
#166480 - 2006-08-26 06:40 PM Re: Math Help
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
By assumption is easy as long as the rules don't change.
Top
#166481 - 2006-08-26 06:45 PM Re: Math Help
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
And since I am making the rules....
Top
#166482 - 2006-08-26 06:48 PM Re: Math Help
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Gargoyle, that is even better. Like you said, the array only contains 0 or -1

Edited by Witto (2006-08-26 06:51 PM)

Top
#166483 - 2006-08-26 08:01 PM Re: Math Help
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
Your wife? Some type of inside humor or does she actually code too?
Top
#166484 - 2006-08-26 08:06 PM Re: Math Help
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
No actually she is an accountant and looks at the logic differently. She has some basic programming skills (as in MSBasic) but that is it.

So by talking it through with her, sometimes she sees a different way of doing it.

Top
#166485 - 2006-08-27 12:47 AM Re: Math Help
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
woman had a better logic than you... scary.
Top
#166486 - 2006-08-27 08:57 PM Re: Math Help
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
Great help for the next KiXGolf tournament
Top
#166487 - 2006-08-27 10:21 PM Re: Math Help
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Here's my GOLFed down version:
Code:

$a=-1,-1,-1,0,-1
if ubound($a)*3+2=len(join(split(join($a),'-')))
? 'Array contains only -1s'
else
? 'Array does not only contain -1s'
endif


for a score of 45 for the "-1" check
_________________________
There are two types of vessels, submarines and targets.

Top
#166488 - 2006-08-27 10:46 PM Re: Math Help
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11629
Loc: CA
Not sure how you get 45 with that. Including spaces it should be 149


Including spaces this one is: 73

$=-1,-1,-1,0,-1
IIf(UBound($)*3+2=Len(Join(Split(Join($),'-'))),'T','F')

Top
#166489 - 2006-08-27 10:49 PM Re: Math Help
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
Code:

$a=-1,-1,-1,0,-1
? 'Array does '
if ubound($a)*3+2<>len(join(split(join($a),'-')))
'not '
endif
'only contain -1s'




Top
#166490 - 2006-08-27 10:50 PM Re: Math Help
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
actually....
Code:

$a=-1,-1,-1,0,-1
? 'Array does '
if 0<len(join(split(join($a),'-1'),''))
'not '
endif
'only contain -1s'


Top
#166491 - 2006-08-27 11:38 PM Re: Math Help
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Knew you'd golf it down, NTDOC thought you were already sleeping
_________________________
There are two types of vessels, submarines and targets.

Top
#166492 - 2006-08-28 01:43 AM Re: Math Help
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
All's fair in a game of golf.

Code:
$=-1,-1,-1,0,-1
IIf(UBound($)*3+2=Len(Join(Split(Join($),-))),T,F)


Top
#166493 - 2006-08-28 02:06 AM Re: Math Help
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
well, this should still be shorter:
Code:

$=-1,-1,-1,0,-1
IIf(len(join(split(join($a),'-1'),'')),F,T)


Top
#166494 - 2006-08-28 02:09 AM Re: Math Help
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
actually, this is even shorter:
Code:

$=-1,-1,-1,0,-1
IIf(join(split(join($a),'-1'),''),F,T)


Top
Page 1 of 2 12>


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 811 anonymous users online.
Newest Members
batdk82, StuTheCoder, M_Moore, BeeEm, min_seow
17885 Registered Users

Generated in 0.075 seconds in which 0.027 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org