Here is a sample of my input file :
$cat NDVI-bm P01 031.RAW 0.516 0 0 P01 021.RAW 0.449 0 0 P02 045.RAW 0.418 0 0 P03 062.RAW 0.570 0 0 P03 064.RAW 0.
Have a different array where you keep track of the number of entries you have seen for each index, and do the division in the END block.