Subscript expect bar indexes (0 to BarCount-1) but not return of DayofYear. So what you do is incorrect.
Also to get output of upper example quote all you need is two lines of code and just array processing.
(To disable QuickAFL you may have to use SetBarsRequired().)
SetBarsRequired(1000);
doy = DayOfYear(); // doy = {5, 6, 7, 8, 9, 12, 13, ..., 365, 4, 5, ... }
Sum_of_dayValue = Cum(IIf(doy==5,C,0)); // Sum of the Close on the same Days of the year