Don't initialize variables inside loops. Also, you could reuse the temp1 variable in place of the temp2 one.