Ovviamente questa riga

for (i=1;i<=RIG;j++){

e' sbagliata ...