next up previous contents
Next: Solution Up: Arrays Previous: Solution

 

Array Masked Array Assignment

Using an array constructor and the WHERE statement, implement the following algorithm for finding prime numbers:

  1. define a vector, Prime, of size n,
  2. initialise Prime such that Prime(i) = i for i = 1,n
  3. set i = 2
  4. for all tex2html_wrap_inline3286 , ( tex2html_wrap_inline3288 ) if Prime(j) is exactly divisible by i then set Prime(j) = 0, [hint: use the MOD (remainder) intrinsic in conjunction with a WHERE statement.]
  5. increment i,
  6. if i equals n then exit
  7. if Prime(i) is zero then goto step 5
  8. goto step 4

Print out all non-zero entries of the vector (the prime numbers).

Hint: the WHERE statement is an array assignment statement and not a control construct therefore it cannot contain a PRINT statement. The PACK intrinsic can accept an array argument and a conformable MASK and will return a 1D vector of all the elements of the array where the corresponding mask elements are .TRUE..

  Print*, PACK(Array,Mask)

Go back to Notes gif




next up previous contents
Next: Solution Up: Arrays Previous: Solution

©University of Liverpool, 1997
Thu May 29 10:11:26 BST 1997
Not for commercial use.