Search code examples
arraysnim-lang

IndexDefect Issue In Nim


I am trying to learn Nim, so picked up this so, the first problem says:



Create an empty array which can contain ten integers.

    Fill that array with numbers 10, 20, …​, 100. (Hint: use loops)

    Print only the elements of that array that are on odd indices (values 20, 40, …​).

    Multiply elements on even indices by 5. Print the modified array.

I tried,

var a: array[10, int]
for i in countup(0,100,10):
    a[i] = i
echo a

It shows error with IndexEffect, I know am try to index more than 10 which is wrong but I cannot find any new other ways to do this, any help with the code will be highly appreciated.


Solution

  • The most obvious way might be something like this:

    var a: array[10, int]
    for i in 1..10:
      a[i - 1] = i * 10
    echo a