Hello all. I've been practicing with arrays and was wondering if anyone can tell me why my code is spitting out so many indexes as well as not the correct numbers? Thanks in advance.
Note that if line 12 condition is true, then tmp is 0 and there is no point setting it 0 on line 13.
The loop on lines 16-20 does not do anything with the list and will always iterate 10 times. You could remove the line 19 and add outside of the loop: list += 10;
Therefore, what does the loop on lines 10-22 effectively do?
1. If user gives a non-0 number for N times, then the value of 'list' is N*10 after the loop. That can be larger than 'max'.
2. The last non-0 input value is written to the 10 first elements of the array 'arr'. Elements 10-49 are never written to. They remain uninitialized. Can contain anything.
The loop on lines 23-26 does thus repeat for 10*N+1 times.
If you did give at least 5 non-0 numbers, then at least element arr[50] will be accessed. Trouble is, the arr has only 50 elements and the last of them is arr[49]. The arr[50] is an out-of-range error.
If you give just one number and then 0, the loop will still access element arr[10], but the do..while loop did not write anything to that array element.
If you start with 0, then the last loop will still print arr[0], which was never witten to, because loop on 16-20 was never executed.