Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 ARRAYS
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Neo




PostPosted: 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
Sponsor
sponsor
wtd




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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


Embarassed I stand corrected.
wtd




PostPosted: 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".
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: