Computer Science Canada

Array help

Author:  mike200015 [ Sat Feb 26, 2005 7:00 pm ]
Post subject:  Array help

Ok... so my program is like a store(you can buy things, and choose the quantity that you want for the item), and i have an array for the quantity of each item and i set the quantity to 0 at the begining, so what i want to do is display a message only if there is at least 1 item with a quantity higher than 0. So how do i make it go through all the quantities in the array, and chek if there is a value higher than 0, and once it finds at least 1 value higher than 0, then it will display the message Question

Author:  person [ Sat Feb 26, 2005 7:27 pm ]
Post subject: 

for x:1..%watever
if %var% =1 then
..................
end if
end for

Author:  mike200015 [ Sat Feb 26, 2005 7:38 pm ]
Post subject: 

but then if it goes through the loop and find a value >0 then it will display message, and then it will continue through the loop and if it finds another value >0 it will display again.. i want it to stop when it find value >0 and then display message


EDIT

Nevermind, I got it.

Author:  Cervantes [ Sat Feb 26, 2005 8:35 pm ]
Post subject: 

You got it, but did you get it the easy way? I'm guessing you did some stuff with boolean variables to make it work. The easier way is to use a function. If the condition you are looking for is true, result true. Go through the for loop like that, and when you finish the for loop, result false. As soon as a function reaches a result line, it results the value and terminates. So, as soon as your condition is true for any element of your array, the function will result true and exit.

code:

function checkQuantity : boolean  %the : boolean means the function returns a boolean value
  for i : 1 .. uper (yourArray)
    if yourArray (i) > 0 then
      result true
    end if
  end for
  result false
end checkQuantity

If you don't know functions, check the tutorial on this site. Search for it, I believe tony wrote it. Smile

Author:  mike200015 [ Sat Feb 26, 2005 9:54 pm ]
Post subject: 

Cervantes wrote:
You got it, but did you get it the easy way? I'm guessing you did some stuff with boolean variables to make it work.


lol haha Laughing .. yea howd u know Smile


: