Posted: Wed Nov 24, 2004 11:08 pm Post subject: ARRAYS
If I had an array with 121 variables of boolean, how would I check to
see if all of them are true?
like this but shortened
if num(1)=true and num(2)=true and num(3)=true...etc
Sponsor Sponsor
wtd
Posted: Wed Nov 24, 2004 11:11 pm Post subject: Re: ARRAYS
Neo wrote:
If I had an array with 121 variables of boolean, how would I check to
see if all of them are true?
like this but shortened
if num(1)=true and num(2)=true and num(3)=true...etc
Use a loop. Remember that checking to see if they're all true is the same as checking to see if any of them are false.
Hikaru79
Posted: Wed Nov 24, 2004 11:47 pm Post subject: (No subject)
code:
var allTrue : boolean := true
for x: 1..121
if num(x) = false then
allTrue := false
exit
end if
end for
wtd
Posted: Thu Nov 25, 2004 12:02 am Post subject: (No subject)
Hikaru79 wrote:
code:
var allTrue : boolean := true
for x: 1..121
if num(x) = false then
allTrue := false
exit
end if
end for
Close, but you don't need the variable. And as long as you posted code...
code:
function allTrue (arr : array 1 .. * of boolean) : boolean
for i : 1 .. upper (arr)
if not arr (i) then
result false
end if
end for
result true
end allTrue
Hikaru79
Posted: Thu Nov 25, 2004 12:09 am Post subject: (No subject)
wtd wrote:
Hikaru79 wrote:
code:
var allTrue : boolean := true
for x: 1..121
if num(x) = false then
allTrue := false
exit
end if
end for
Close, but you don't need the variable. And as long as you posted code...
code:
function allTrue (arr : array 1 .. * of boolean) : boolean
for i : 1 .. upper (arr)
if not arr (i) then
result false
end if
end for
result true
end allTrue
I stand corrected.
wtd
Posted: Thu Nov 25, 2004 12:29 am Post subject: (No subject)
You had the basic idea down fine. You just weren't taking full advantage of "result".