NASutils Knowledge BaseUnofficialPolski

BASIC · lesson 13 of 30 · Data, functions and the command lineMany values, one name

dim makes an array, arraysize gives its length. A loop finds the sum and the highest value.

Programming on the NAS › BASIC in 30 lessons

13-arrays.yab Download 13-arrays.yab

rem An array keeps many values under one name
dim temp(4)
temp(1) = 38
temp(2) = 39
temp(3) = 53
temp(4) = 44

sum = 0
hot = 1
for i = 1 to arraysize(temp(), 1)
  print "disk ", i, ": ", temp(i), " C"
  sum = sum + temp(i)
  if temp(i) > temp(hot) hot = i
next i

print "average: ", sum / arraysize(temp(), 1)
print "the warmest is disk ", hot

Terminal

$ yabasic 13-arrays.yab
disk 1: 38 C
disk 2: 39 C
disk 3: 53 C
disk 4: 44 C
average: 43.5
the warmest is disk 3

Exercise

Find the lowest temperature too.