use a for loop instead of allocating array