Think about it - your variable
i
will run through every valid index within the array, so
i * 10
is going to be an invalid index most of the time.
If the valid indices are
0
to
4
, you're trying to display the items at
0
,
10
,
20
,
30
, and
40
. Only the first one will succeed.
To display every 10th item in the array, change your loop to increment by
10
instead of
1
:
for (i = 0; i < arr.Length; i += 10)
{
Console.Write(arr[i] + " ");
}
NB: As per
a discussion in the Lounge[
^], there is potentially some confusion over your requirements. The code in your question suggests that you want to start with the first element of the array. But
"every 10th element" could be interpreted to mean that you start with the tenth element of the array.
If you want to start with the 10th element in the array, then you simply need to change the starting position of the loop:
for (i = 9; i < arr.Length; i += 10)