# iterative program to sum elements of an array

# store in a file such as "sum.mpd"
# compile by executing "mpd sum.mpd"
# run by executing "a.out size"

resource sum()
  # declare and read first command-line argument
  int size; getarg(1,size);

  # declare and initialize array a
  int a[1:size];         # same as int a[size]
  for [i = 1 to size] {
    a[i] = i;
  }

  # calculate then print a[1] + ... + a[size]
  int total = 0;
  for [i = 1 to size] {
    total = total + a[i];   # same as total += a[i]
  }
  write("the sum of 1 ...", size, "is", total);

end
